回答編集履歴

1

y座標ランダム+ループ

2020/01/08 21:23

投稿

退会済みユーザー
test CHANGED
@@ -57,3 +57,305 @@
57
57
  ```
58
58
 
59
59
  if文が途切れていて詳しくはわかりませんが、変数 y が使われていないようです
60
+
61
+
62
+
63
+
64
+
65
+ 追記:
66
+
67
+ ```Java
68
+
69
+ Bullet bullet;
70
+
71
+ private Boss boss = new Boss(75, 35);
72
+
73
+ private ArrayList<Bullet> danmaku = new ArrayList<Bullet>();
74
+
75
+
76
+
77
+ float[] z = new float[500];
78
+
79
+ float[] y = new float[500]; //左から流れる玉のy座標
80
+
81
+
82
+
83
+ void setup() {
84
+
85
+ size(420, 580);
86
+
87
+
88
+
89
+ frameRate(40);
90
+
91
+
92
+
93
+ noCursor();
94
+
95
+
96
+
97
+ bullet = new Bullet(width/2, height/2, 10, 0, 0 );
98
+
99
+
100
+
101
+ for (int r = 0; r < z.length; r++) {
102
+
103
+ z[r] = random(-1000,300);
104
+
105
+ y[r] = random(height); //乱数(0~height)で初期化
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ void ship(int x, int y) {
114
+
115
+
116
+
117
+ noStroke();
118
+
119
+
120
+
121
+ fill(255, 0, 0);
122
+
123
+
124
+
125
+ ellipse(mouseX, mouseY, 7.5, 7.5);
126
+
127
+ }
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+ class Boss {
136
+
137
+ private int tx, ty;
138
+
139
+ private int dx, dy;
140
+
141
+ private long routine = 0;
142
+
143
+ Boss(int x, int y) {
144
+
145
+ tx = x;
146
+
147
+ ty = y;
148
+
149
+ dx = 1;
150
+
151
+ dy = -5;
152
+
153
+ }
154
+
155
+ void move() {
156
+
157
+ tx += dx;
158
+
159
+ ty += dy;
160
+
161
+ if (tx < 0 || tx > width) {
162
+
163
+ dx*= -1;
164
+
165
+ }
166
+
167
+ if (ty < 0 || ty > height) {
168
+
169
+ dy*= -1;
170
+
171
+ }
172
+
173
+ }
174
+
175
+ void doShinking() {
176
+
177
+ routine++;
178
+
179
+ if (routine % 10 == 0) {
180
+
181
+ move();
182
+
183
+ }
184
+
185
+ if (routine % 30 == 0) {
186
+
187
+ danmaku.addAll(this.shot());
188
+
189
+ println("#shot" +routine + ":" + tx + "," + ty);
190
+
191
+ }
192
+
193
+ }
194
+
195
+ ArrayList<Bullet> shot() {
196
+
197
+ ArrayList<Bullet> danmaku = new ArrayList();
198
+
199
+ for (int i = 0; i < 360; i+= 11.5) {
200
+
201
+ double rad = radians(i);
202
+
203
+ danmaku.add(new Bullet(this.tx, this.ty, 10, Math.cos(rad), Math.sin(rad)));
204
+
205
+ }
206
+
207
+ return danmaku;
208
+
209
+ }
210
+
211
+ }
212
+
213
+
214
+
215
+ class Bullet {
216
+
217
+
218
+
219
+ private double tx, ty;
220
+
221
+ private final double tr;
222
+
223
+ private double dx, dy;
224
+
225
+ private boolean is_alive = true;
226
+
227
+ Bullet(double x, double y, double r, double temp_dx, double temp_dy ) {
228
+
229
+ tx = x;
230
+
231
+ ty = y;
232
+
233
+ tr = r;
234
+
235
+ dx = temp_dx;
236
+
237
+ dy = temp_dy;
238
+
239
+ }
240
+
241
+ boolean isAlive() {
242
+
243
+ return is_alive;
244
+
245
+ }
246
+
247
+ void update() {
248
+
249
+ tx += dx;
250
+
251
+ ty += dy;
252
+
253
+ if (Math.min(tx, ty) < 0) {
254
+
255
+ is_alive = false;
256
+
257
+ return ;
258
+
259
+ }
260
+
261
+ if (tx > width || ty > height) {
262
+
263
+ is_alive = false;
264
+
265
+ return ;
266
+
267
+ }
268
+
269
+ stroke(255, 0, 255);
270
+
271
+
272
+
273
+ fill(0, 0, 255);
274
+
275
+
276
+
277
+ ellipse((float)tx, (float)ty, (float)tr, (float)tr);
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+ void draw() {
290
+
291
+ background(0);
292
+
293
+ /*不要?なのでコメントアウト
294
+
295
+ for (int r = 0; r < z.length; r++) {
296
+
297
+ z[r] += 2;
298
+
299
+ //float y = r * 0.5;
300
+
301
+ ellipse(z[r], y[r], 10, 10);
302
+
303
+ }
304
+
305
+ */
306
+
307
+
308
+
309
+ ship(mouseX, mouseY);
310
+
311
+ boss.doShinking();
312
+
313
+
314
+
315
+ for (int i = danmaku.size() -1; i >= 0; i--) {
316
+
317
+ Bullet b = (Bullet)danmaku.get(i);
318
+
319
+ if (!b.isAlive()) {
320
+
321
+ danmaku.remove(i);
322
+
323
+ continue;
324
+
325
+ }
326
+
327
+ b.update();
328
+
329
+ }
330
+
331
+
332
+
333
+
334
+
335
+ for (int r = 0; r < z.length; r++) {
336
+
337
+ z[r] += 2;
338
+
339
+ if(z[r] > width + 10){ //玉が画面外に出たら
340
+
341
+ z[r] = random(-1000,-10); //画面左外に戻す
342
+
343
+ y[r] = random(height);
344
+
345
+ }
346
+
347
+ stroke(255, 0, 255); //輪郭線の色
348
+
349
+ fill(0,0,255); //玉の色
350
+
351
+ ellipse(z[r], y[r], 10, 10); //y座標に y[r]を使う
352
+
353
+ }
354
+
355
+ }
356
+
357
+ ```
358
+
359
+ 左から流れる玉のy座標をランダムにしたいのであれば玉1つ1つのy座標を覚えておく必要があります。
360
+
361
+ ループさせるには x座標( z[r] )が width(画面横幅) + 10(玉の半径) を超えたら画面外に出ているので画面左外に戻す必要があります。