回答編集履歴
3
逆リファクタリング
answer
CHANGED
@@ -139,7 +139,7 @@
|
|
139
139
|
|
140
140
|
// 色をランダムに変える
|
141
141
|
void changeColor() {
|
142
|
-
_color =int(random(2)) == 0 ? CYAN : MAGENTA;
|
142
|
+
_color = int(random(2)) == 0 ? CYAN : MAGENTA;
|
143
143
|
}
|
144
144
|
}
|
145
145
|
|
@@ -244,4 +244,155 @@
|
|
244
244
|
しかし機能を変更や追加をしようとしたときに、
|
245
245
|
「Playerのあの関数を見ればいいんだ」
|
246
246
|
「Playerに新たに関数を追加しよう」
|
247
|
-
等、どこを変えればいいのかが明瞭になり、作っていくうちにだんだんメリットを感じてくると思います。
|
247
|
+
等、どこを変えればいいのかが明瞭になり、作っていくうちにだんだんメリットを感じてくると思います。
|
248
|
+
|
249
|
+
---
|
250
|
+
|
251
|
+
```Processing
|
252
|
+
final boolean RANDOM_CHANGE = false;
|
253
|
+
final color CYAN = color(0, 255, 255);
|
254
|
+
final color MAGENTA = color(255, 0, 255);
|
255
|
+
final int MARU_RADIUS = 15;
|
256
|
+
final int MARU_COUNT = 20;
|
257
|
+
|
258
|
+
int playerX = 400;
|
259
|
+
int playerY = 500;
|
260
|
+
int playerWidth = 60;
|
261
|
+
int playerHeight = 30;
|
262
|
+
color playerColor = CYAN;
|
263
|
+
|
264
|
+
int[] marusX = new int[MARU_COUNT];
|
265
|
+
int[] marusY = new int[MARU_COUNT];
|
266
|
+
color[] marusColor = new color[MARU_COUNT];
|
267
|
+
int[] marusSpeed = new int[MARU_COUNT];
|
268
|
+
boolean[] marusAlive = new boolean[MARU_COUNT];
|
269
|
+
|
270
|
+
int score;
|
271
|
+
|
272
|
+
void setup() {
|
273
|
+
size(800, 600);
|
274
|
+
noStroke();
|
275
|
+
|
276
|
+
for (int i = 0; i < MARU_COUNT; i++) {
|
277
|
+
marusX[i] = i * 40 + 20;
|
278
|
+
marusY[i] = -int(random(height));
|
279
|
+
|
280
|
+
if (int(random(2)) == 0) {
|
281
|
+
marusColor[i] = CYAN;
|
282
|
+
} else {
|
283
|
+
marusColor[i] = MAGENTA;
|
284
|
+
}
|
285
|
+
|
286
|
+
marusSpeed[i] = int(random(1, 4));
|
287
|
+
marusAlive[i] = true;
|
288
|
+
}
|
289
|
+
}
|
290
|
+
|
291
|
+
void draw() {
|
292
|
+
background(0);
|
293
|
+
|
294
|
+
// player更新
|
295
|
+
playerX = mouseX - (playerWidth / 2);
|
296
|
+
if (RANDOM_CHANGE) {
|
297
|
+
if (frameCount % (60 * 5) == 0) {
|
298
|
+
if (int(random(2)) == 0) {
|
299
|
+
playerColor = CYAN;
|
300
|
+
} else {
|
301
|
+
playerColor = MAGENTA;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
}
|
305
|
+
|
306
|
+
// player描画
|
307
|
+
if (mousePressed) {
|
308
|
+
fill(playerColor);
|
309
|
+
rect(playerX, playerY, playerWidth, playerHeight, 5);
|
310
|
+
}
|
311
|
+
|
312
|
+
|
313
|
+
for (int i = 0; i < MARU_COUNT; i++) {
|
314
|
+
// maru更新
|
315
|
+
marusY[i] += marusSpeed[i];
|
316
|
+
if (height < marusY[i]) {
|
317
|
+
marusY[i] = -MARU_RADIUS * 2;
|
318
|
+
marusAlive[i] = true;
|
319
|
+
marusSpeed[i] =int(random(1, 4));
|
320
|
+
|
321
|
+
if (int(random(2)) == 0) {
|
322
|
+
marusColor[i] = CYAN;
|
323
|
+
} else {
|
324
|
+
marusColor[i] = MAGENTA;
|
325
|
+
}
|
326
|
+
}
|
327
|
+
|
328
|
+
// 当たり判定
|
329
|
+
if (mousePressed) {
|
330
|
+
if (marusAlive[i]) {
|
331
|
+
if (circleRect(marusX[i], marusY[i], MARU_RADIUS, playerX, playerY, playerWidth, playerHeight)) {
|
332
|
+
marusAlive[i] = false;
|
333
|
+
|
334
|
+
if (playerColor == marusColor[i]) {
|
335
|
+
score += 10;
|
336
|
+
} else {
|
337
|
+
score -= 100;
|
338
|
+
}
|
339
|
+
}
|
340
|
+
}
|
341
|
+
}
|
342
|
+
|
343
|
+
// maru描画
|
344
|
+
if (marusAlive[i]) {
|
345
|
+
fill(marusColor[i]);
|
346
|
+
ellipse(marusX[i], marusY[i], MARU_RADIUS * 2, MARU_RADIUS * 2);
|
347
|
+
}
|
348
|
+
}
|
349
|
+
|
350
|
+
fill(255);
|
351
|
+
textSize(20);
|
352
|
+
text("SCORE", 10, 30);
|
353
|
+
text(score, 100, 30);
|
354
|
+
|
355
|
+
int seconds = frameCount / 60;
|
356
|
+
text("TIME", 10, 60);
|
357
|
+
text(seconds, 100, 60);
|
358
|
+
}
|
359
|
+
|
360
|
+
void mousePressed() {
|
361
|
+
if (!RANDOM_CHANGE) {
|
362
|
+
if (playerColor == CYAN) {
|
363
|
+
playerColor = MAGENTA;
|
364
|
+
} else {
|
365
|
+
playerColor = CYAN;
|
366
|
+
}
|
367
|
+
}
|
368
|
+
}
|
369
|
+
|
370
|
+
|
371
|
+
// 以下参考コードをちょい変更
|
372
|
+
// http://www.jeffreythompson.org/collision-detection/circle-rect.php
|
373
|
+
// CIRCLE/RECTANGLE
|
374
|
+
private boolean circleRect(int cx, int cy, int radius, int rx, int ry, int rw, int rh) {
|
375
|
+
|
376
|
+
// temporary variables to set edges for testing
|
377
|
+
int testX = cx;
|
378
|
+
int testY = cy;
|
379
|
+
|
380
|
+
// which edge is closest?
|
381
|
+
if (cx < rx) testX = rx; // test left edge
|
382
|
+
else if (cx > rx + rw) testX = rx + rw; // right edge
|
383
|
+
if (cy < ry) testY = ry; // top edge
|
384
|
+
else if (cy > ry + rh) testY = ry + rh; // bottom edge
|
385
|
+
|
386
|
+
// get distance from closest edges
|
387
|
+
// float distX = cx - testX;
|
388
|
+
// float distY = cy - testY;
|
389
|
+
// float distance = sqrt((distX * distX) + (distY * distY));
|
390
|
+
float distance = dist(cx, cy, testX, testY);
|
391
|
+
|
392
|
+
// if the distance is less than the radius, collision!
|
393
|
+
if (distance <= radius) {
|
394
|
+
return true;
|
395
|
+
}
|
396
|
+
return false;
|
397
|
+
}
|
398
|
+
```
|
2
スペース
answer
CHANGED
@@ -191,7 +191,7 @@
|
|
191
191
|
// 色を変える
|
192
192
|
void changeColor() {
|
193
193
|
if (RANDOM_CHANGE) { // 5秒おきモードの場合。。。
|
194
|
-
_color =int(random(2)) == 0 ? CYAN : MAGENTA; // ランダムに変える
|
194
|
+
_color = int(random(2)) == 0 ? CYAN : MAGENTA; // ランダムに変える
|
195
195
|
} else {
|
196
196
|
_color = _color == CYAN ? MAGENTA : CYAN; // 交互に変える
|
197
197
|
}
|
1
スペース
answer
CHANGED
@@ -67,7 +67,7 @@
|
|
67
67
|
|
68
68
|
// マウスを押した瞬間
|
69
69
|
void mousePressed() {
|
70
|
-
if (!RANDOM_CHANGE) { //
|
70
|
+
if (!RANDOM_CHANGE) { // 5秒おきモードでなければ色を変える
|
71
71
|
player.changeColor();
|
72
72
|
}
|
73
73
|
}
|