回答編集履歴
3
逆リファクタリング
test
CHANGED
@@ -280,7 +280,7 @@
|
|
280
280
|
|
281
281
|
void changeColor() {
|
282
282
|
|
283
|
-
_color =int(random(2)) == 0 ? CYAN : MAGENTA;
|
283
|
+
_color = int(random(2)) == 0 ? CYAN : MAGENTA;
|
284
284
|
|
285
285
|
}
|
286
286
|
|
@@ -491,3 +491,305 @@
|
|
491
491
|
「Playerに新たに関数を追加しよう」
|
492
492
|
|
493
493
|
等、どこを変えればいいのかが明瞭になり、作っていくうちにだんだんメリットを感じてくると思います。
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
---
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
```Processing
|
502
|
+
|
503
|
+
final boolean RANDOM_CHANGE = false;
|
504
|
+
|
505
|
+
final color CYAN = color(0, 255, 255);
|
506
|
+
|
507
|
+
final color MAGENTA = color(255, 0, 255);
|
508
|
+
|
509
|
+
final int MARU_RADIUS = 15;
|
510
|
+
|
511
|
+
final int MARU_COUNT = 20;
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
int playerX = 400;
|
516
|
+
|
517
|
+
int playerY = 500;
|
518
|
+
|
519
|
+
int playerWidth = 60;
|
520
|
+
|
521
|
+
int playerHeight = 30;
|
522
|
+
|
523
|
+
color playerColor = CYAN;
|
524
|
+
|
525
|
+
|
526
|
+
|
527
|
+
int[] marusX = new int[MARU_COUNT];
|
528
|
+
|
529
|
+
int[] marusY = new int[MARU_COUNT];
|
530
|
+
|
531
|
+
color[] marusColor = new color[MARU_COUNT];
|
532
|
+
|
533
|
+
int[] marusSpeed = new int[MARU_COUNT];
|
534
|
+
|
535
|
+
boolean[] marusAlive = new boolean[MARU_COUNT];
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
int score;
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
void setup() {
|
544
|
+
|
545
|
+
size(800, 600);
|
546
|
+
|
547
|
+
noStroke();
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
for (int i = 0; i < MARU_COUNT; i++) {
|
552
|
+
|
553
|
+
marusX[i] = i * 40 + 20;
|
554
|
+
|
555
|
+
marusY[i] = -int(random(height));
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
if (int(random(2)) == 0) {
|
560
|
+
|
561
|
+
marusColor[i] = CYAN;
|
562
|
+
|
563
|
+
} else {
|
564
|
+
|
565
|
+
marusColor[i] = MAGENTA;
|
566
|
+
|
567
|
+
}
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
marusSpeed[i] = int(random(1, 4));
|
572
|
+
|
573
|
+
marusAlive[i] = true;
|
574
|
+
|
575
|
+
}
|
576
|
+
|
577
|
+
}
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
void draw() {
|
582
|
+
|
583
|
+
background(0);
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
// player更新
|
588
|
+
|
589
|
+
playerX = mouseX - (playerWidth / 2);
|
590
|
+
|
591
|
+
if (RANDOM_CHANGE) {
|
592
|
+
|
593
|
+
if (frameCount % (60 * 5) == 0) {
|
594
|
+
|
595
|
+
if (int(random(2)) == 0) {
|
596
|
+
|
597
|
+
playerColor = CYAN;
|
598
|
+
|
599
|
+
} else {
|
600
|
+
|
601
|
+
playerColor = MAGENTA;
|
602
|
+
|
603
|
+
}
|
604
|
+
|
605
|
+
}
|
606
|
+
|
607
|
+
}
|
608
|
+
|
609
|
+
|
610
|
+
|
611
|
+
// player描画
|
612
|
+
|
613
|
+
if (mousePressed) {
|
614
|
+
|
615
|
+
fill(playerColor);
|
616
|
+
|
617
|
+
rect(playerX, playerY, playerWidth, playerHeight, 5);
|
618
|
+
|
619
|
+
}
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
for (int i = 0; i < MARU_COUNT; i++) {
|
626
|
+
|
627
|
+
// maru更新
|
628
|
+
|
629
|
+
marusY[i] += marusSpeed[i];
|
630
|
+
|
631
|
+
if (height < marusY[i]) {
|
632
|
+
|
633
|
+
marusY[i] = -MARU_RADIUS * 2;
|
634
|
+
|
635
|
+
marusAlive[i] = true;
|
636
|
+
|
637
|
+
marusSpeed[i] =int(random(1, 4));
|
638
|
+
|
639
|
+
|
640
|
+
|
641
|
+
if (int(random(2)) == 0) {
|
642
|
+
|
643
|
+
marusColor[i] = CYAN;
|
644
|
+
|
645
|
+
} else {
|
646
|
+
|
647
|
+
marusColor[i] = MAGENTA;
|
648
|
+
|
649
|
+
}
|
650
|
+
|
651
|
+
}
|
652
|
+
|
653
|
+
|
654
|
+
|
655
|
+
// 当たり判定
|
656
|
+
|
657
|
+
if (mousePressed) {
|
658
|
+
|
659
|
+
if (marusAlive[i]) {
|
660
|
+
|
661
|
+
if (circleRect(marusX[i], marusY[i], MARU_RADIUS, playerX, playerY, playerWidth, playerHeight)) {
|
662
|
+
|
663
|
+
marusAlive[i] = false;
|
664
|
+
|
665
|
+
|
666
|
+
|
667
|
+
if (playerColor == marusColor[i]) {
|
668
|
+
|
669
|
+
score += 10;
|
670
|
+
|
671
|
+
} else {
|
672
|
+
|
673
|
+
score -= 100;
|
674
|
+
|
675
|
+
}
|
676
|
+
|
677
|
+
}
|
678
|
+
|
679
|
+
}
|
680
|
+
|
681
|
+
}
|
682
|
+
|
683
|
+
|
684
|
+
|
685
|
+
// maru描画
|
686
|
+
|
687
|
+
if (marusAlive[i]) {
|
688
|
+
|
689
|
+
fill(marusColor[i]);
|
690
|
+
|
691
|
+
ellipse(marusX[i], marusY[i], MARU_RADIUS * 2, MARU_RADIUS * 2);
|
692
|
+
|
693
|
+
}
|
694
|
+
|
695
|
+
}
|
696
|
+
|
697
|
+
|
698
|
+
|
699
|
+
fill(255);
|
700
|
+
|
701
|
+
textSize(20);
|
702
|
+
|
703
|
+
text("SCORE", 10, 30);
|
704
|
+
|
705
|
+
text(score, 100, 30);
|
706
|
+
|
707
|
+
|
708
|
+
|
709
|
+
int seconds = frameCount / 60;
|
710
|
+
|
711
|
+
text("TIME", 10, 60);
|
712
|
+
|
713
|
+
text(seconds, 100, 60);
|
714
|
+
|
715
|
+
}
|
716
|
+
|
717
|
+
|
718
|
+
|
719
|
+
void mousePressed() {
|
720
|
+
|
721
|
+
if (!RANDOM_CHANGE) {
|
722
|
+
|
723
|
+
if (playerColor == CYAN) {
|
724
|
+
|
725
|
+
playerColor = MAGENTA;
|
726
|
+
|
727
|
+
} else {
|
728
|
+
|
729
|
+
playerColor = CYAN;
|
730
|
+
|
731
|
+
}
|
732
|
+
|
733
|
+
}
|
734
|
+
|
735
|
+
}
|
736
|
+
|
737
|
+
|
738
|
+
|
739
|
+
|
740
|
+
|
741
|
+
// 以下参考コードをちょい変更
|
742
|
+
|
743
|
+
// http://www.jeffreythompson.org/collision-detection/circle-rect.php
|
744
|
+
|
745
|
+
// CIRCLE/RECTANGLE
|
746
|
+
|
747
|
+
private boolean circleRect(int cx, int cy, int radius, int rx, int ry, int rw, int rh) {
|
748
|
+
|
749
|
+
|
750
|
+
|
751
|
+
// temporary variables to set edges for testing
|
752
|
+
|
753
|
+
int testX = cx;
|
754
|
+
|
755
|
+
int testY = cy;
|
756
|
+
|
757
|
+
|
758
|
+
|
759
|
+
// which edge is closest?
|
760
|
+
|
761
|
+
if (cx < rx) testX = rx; // test left edge
|
762
|
+
|
763
|
+
else if (cx > rx + rw) testX = rx + rw; // right edge
|
764
|
+
|
765
|
+
if (cy < ry) testY = ry; // top edge
|
766
|
+
|
767
|
+
else if (cy > ry + rh) testY = ry + rh; // bottom edge
|
768
|
+
|
769
|
+
|
770
|
+
|
771
|
+
// get distance from closest edges
|
772
|
+
|
773
|
+
// float distX = cx - testX;
|
774
|
+
|
775
|
+
// float distY = cy - testY;
|
776
|
+
|
777
|
+
// float distance = sqrt((distX * distX) + (distY * distY));
|
778
|
+
|
779
|
+
float distance = dist(cx, cy, testX, testY);
|
780
|
+
|
781
|
+
|
782
|
+
|
783
|
+
// if the distance is less than the radius, collision!
|
784
|
+
|
785
|
+
if (distance <= radius) {
|
786
|
+
|
787
|
+
return true;
|
788
|
+
|
789
|
+
}
|
790
|
+
|
791
|
+
return false;
|
792
|
+
|
793
|
+
}
|
794
|
+
|
795
|
+
```
|
2
スペース
test
CHANGED
@@ -384,7 +384,7 @@
|
|
384
384
|
|
385
385
|
if (RANDOM_CHANGE) { // 5秒おきモードの場合。。。
|
386
386
|
|
387
|
-
_color =int(random(2)) == 0 ? CYAN : MAGENTA; // ランダムに変える
|
387
|
+
_color = int(random(2)) == 0 ? CYAN : MAGENTA; // ランダムに変える
|
388
388
|
|
389
389
|
} else {
|
390
390
|
|
1
スペース
test
CHANGED
@@ -136,7 +136,7 @@
|
|
136
136
|
|
137
137
|
void mousePressed() {
|
138
138
|
|
139
|
-
if (!RANDOM_CHANGE) { //5
|
139
|
+
if (!RANDOM_CHANGE) { // 5秒おきモードでなければ色を変える
|
140
140
|
|
141
141
|
player.changeColor();
|
142
142
|
|