回答編集履歴

3

コードにコメント移植

2023/01/30 06:23

投稿

jimbe
jimbe

スコア12625

test CHANGED
@@ -88,33 +88,33 @@
88
88
  }
89
89
 
90
90
  private ImageTurtle createCat() {
91
- ImageTurtle cat = new ImageTurtle("cat.png");
91
+ ImageTurtle cat = new ImageTurtle("cat.png"); //キャラ生成
92
92
  cat.warp(random(640),0); //初期値のワープ
93
93
  return cat;
94
94
  }
95
95
 
96
96
  private ImageTurtle createCheese(int x) {
97
- ImageTurtle cheese = new ImageTurtle("cheese.png");
97
+ ImageTurtle cheese = new ImageTurtle("cheese.png"); //キャラ生成
98
98
  cheese.warp(x,0); //初期値のワープ
99
99
  return cheese;
100
100
  }
101
101
 
102
102
  private void moveCat(ImageTurtle cat, int ymax, ImageTurtle nezumi, SoundTurtle hitcat) {
103
- cat.warp(cat.x(),cat.y() + 5);
103
+ cat.warp(cat.x(),cat.y() + 5); //ワープ先の設定
104
- if(cat.y() > ymax){
104
+ if(cat.y() > ymax){ //下まで行ったら上に行く
105
105
  cat.warp(cat.x(),0);
106
106
  }
107
- if(cat.intersects(nezumi)){
107
+ if(cat.intersects(nezumi)){ //ネズミが当たった時
108
108
  hitcat.play();
109
109
  }
110
110
  }
111
111
 
112
112
  private void moveCheese(ImageTurtle cheese, int ymax, ImageTurtle nezumi, SoundTurtle hitc) {
113
- cheese.warp(cheese.x(),cheese.y() + 5);
113
+ cheese.warp(cheese.x(),cheese.y() + 5); //ワープ先の設定
114
- if(cheese.y() > ymax){
114
+ if(cheese.y() > ymax){ //下まで行ったら上に行く
115
115
  cheese.warp(cheese.x(),0);
116
116
  }
117
- if(cheese.intersects(nezumi)){
117
+ if(cheese.intersects(nezumi)){ //ネズミが当たった時
118
118
  score++;
119
119
  hitc.play();
120
120
  }

2

サウンドもメソッド化、パッケージ文削除

2023/01/30 06:16

投稿

jimbe
jimbe

スコア12625

test CHANGED
@@ -13,8 +13,6 @@
13
13
  以下は、こちらに実行環境が無いのでコンパイルだけ通したコードです。
14
14
 
15
15
  ```java
16
- package teratail_java.q_uokwz02t7umhs2;
17
-
18
16
  public class CatAndRat extends Turtle {
19
17
  public static void main(String[] args) {
20
18
  Turtle.startTurtle(new CatAndRat(), args);
@@ -49,10 +47,8 @@
49
47
  }
50
48
  //音楽
51
49
  makesound();
52
- SoundTurtle hitc = new SoundTurtle("hitc.mp3");
50
+ SoundTurtle hitc = createSound("hitc.mp3");
53
- hitc.loadOnMemory();
54
- SoundTurtle hitcat = new SoundTurtle("hitcat.mp3");
51
+ SoundTurtle hitcat = createSound("hitcat.mp3");
55
- hitcat.loadOnMemory();
56
52
 
57
53
  while(true){
58
54
  ke(nezumi);
@@ -123,5 +119,11 @@
123
119
  hitc.play();
124
120
  }
125
121
  }
122
+
123
+ private SoundTurtle createSound(String filename) {
124
+ SoundTurtle sound = new SoundTurtle(filename);
125
+ sound.loadOnMemory();
126
+ return sound;
127
+ }
126
128
  }
127
129
  ```

1

追加

2023/01/30 06:08

投稿

jimbe
jimbe

スコア12625

test CHANGED
@@ -5,3 +5,123 @@
5
5
  ke(nezumi);
6
6
 
7
7
  では?
8
+
9
+ ---
10
+ 一番行数を取っている cat や cheese の処理をまずメソッドにするべきでしょう。
11
+ その際、「//キャラ生成」「//初期値のワープ」とか「//ワープ先の設定」「//下まで行ったら上に行く」といった、処理毎に各オブジェクトを使うのではなく、 cat1 の生成とワープの設定、 cat2 の生成とワープの設定、・・・、cat1 のワープ先の設定と下まで行ったら上に行く処理、 cat2 のワープ先の設定と下まで行ったら上に行く処理、・・・と各オブジェクト毎に処理を纏めると、それがメソッドとして分ける単位に出来ます。
12
+ そして、配列を習っていれば、 cat や cheese を配列にして for で回すことが出来ます。
13
+ 以下は、こちらに実行環境が無いのでコンパイルだけ通したコードです。
14
+
15
+ ```java
16
+ package teratail_java.q_uokwz02t7umhs2;
17
+
18
+ public class CatAndRat extends Turtle {
19
+ public static void main(String[] args) {
20
+ Turtle.startTurtle(new CatAndRat(), args);
21
+ }
22
+
23
+ private int score = 0;
24
+
25
+ public void start() {
26
+ System.out.println("CatAndRat: Version 8");
27
+ hide();
28
+ //得点版生成
29
+ //int score = 0;
30
+ TextTurtle scoreBoard = new TextTurtle("");
31
+ scoreBoard.warp(600,50);
32
+ //画面サイズ設定
33
+ window();
34
+ //キャラ生成
35
+ ImageTurtle nezumi = new ImageTurtle("nezumi.png");
36
+ nezumi.warp(320,350);
37
+
38
+ int[] catYMaxs = { 480, 600, 500, 700 };
39
+ ImageTurtle[] cats = new ImageTurtle[catYMaxs.length];
40
+ for(int i=0; i<cats.length; i++) {
41
+ cats[i] = createCat();
42
+ }
43
+
44
+ int[] cheesesInitXs = { 250, 320, 420, 520, 150, 80, 600 };
45
+ int[] cheesesYMaxs = { 600, 500, 700, 800, 480, 900, 550 };
46
+ ImageTurtle[] cheeses = new ImageTurtle[cheesesInitXs.length];
47
+ for(int i=0; i<cheeses.length; i++) {
48
+ cheeses[i] = createCheese(cheesesInitXs[i]);
49
+ }
50
+ //音楽
51
+ makesound();
52
+ SoundTurtle hitc = new SoundTurtle("hitc.mp3");
53
+ hitc.loadOnMemory();
54
+ SoundTurtle hitcat = new SoundTurtle("hitcat.mp3");
55
+ hitcat.loadOnMemory();
56
+
57
+ while(true){
58
+ ke(nezumi);
59
+
60
+ //待機
61
+ sleep(0.025);
62
+
63
+ for(int i=0; i<cats.length; i++) {
64
+ moveCat(cats[i], catYMaxs[i], nezumi, hitcat);
65
+ }
66
+
67
+ for(int i=0; i<cheeses.length; i++) {
68
+ moveCheese(cheeses[i], cheesesYMaxs[i], nezumi, hitc);
69
+ }
70
+
71
+ scoreBoard.text(score);
72
+ update();
73
+ }
74
+ }
75
+
76
+ public void window() {
77
+ window.size(640, 480);
78
+ window.warp(100, 100);
79
+ }
80
+
81
+ void makesound() {
82
+ SoundTurtle bgm = new SoundTurtle("pakkuwoman.mp3");
83
+ bgm.play();
84
+ }
85
+
86
+ void ke(ImageTurtle nezumi) {
87
+ if (key() == 37) {
88
+ nezumi.warp(nezumi.x() - 5, nezumi.y());
89
+ } else if (key() == 39) {
90
+ nezumi.warp(nezumi.x() + 5, nezumi.y());
91
+ }
92
+ }
93
+
94
+ private ImageTurtle createCat() {
95
+ ImageTurtle cat = new ImageTurtle("cat.png");
96
+ cat.warp(random(640),0); //初期値のワープ
97
+ return cat;
98
+ }
99
+
100
+ private ImageTurtle createCheese(int x) {
101
+ ImageTurtle cheese = new ImageTurtle("cheese.png");
102
+ cheese.warp(x,0); //初期値のワープ
103
+ return cheese;
104
+ }
105
+
106
+ private void moveCat(ImageTurtle cat, int ymax, ImageTurtle nezumi, SoundTurtle hitcat) {
107
+ cat.warp(cat.x(),cat.y() + 5);
108
+ if(cat.y() > ymax){
109
+ cat.warp(cat.x(),0);
110
+ }
111
+ if(cat.intersects(nezumi)){
112
+ hitcat.play();
113
+ }
114
+ }
115
+
116
+ private void moveCheese(ImageTurtle cheese, int ymax, ImageTurtle nezumi, SoundTurtle hitc) {
117
+ cheese.warp(cheese.x(),cheese.y() + 5);
118
+ if(cheese.y() > ymax){
119
+ cheese.warp(cheese.x(),0);
120
+ }
121
+ if(cheese.intersects(nezumi)){
122
+ score++;
123
+ hitc.play();
124
+ }
125
+ }
126
+ }
127
+ ```