質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/28 15:00

投稿

TN8001
TN8001

スコア10166

answer CHANGED
@@ -1,244 +1,244 @@
1
- [Java - シューティングゲームで、当たり判定を実装したい|teratail](https://teratail.com/questions/351596)
2
- から1週間だいぶ試行錯誤しておられるようですね。
3
- しかし残念ながら方向が違うような気がします。
4
-
5
- Processingは仕組み上クラス内からグローバル変数にアクセスし放題なのですが、そのせいでスパゲッティコードにもなりやすいです(ありがたい時もあるんですが^^;
6
- 例えば`ENEMY`の`x`・`y`が外に出てしまって(`ex`・`ey`)、前回より退化してしまっています。
7
-
8
- > 敵や弾の速度が不安定になってしまっています。
9
-
10
- ちゃんとは追っていませんが、おそらく`ex`・`ey`・`sx`・`sy`のせいです。
11
-
12
- > boolean でhitflagを立てて、衝突範囲内に入った時にtrueを返すような
13
-
14
- 敵が複数いるのですから、フラグも複数あるべきでは?
15
- ひとつでもできないことはないでしょうが、ややこしくなるしすでにクラスがあるんですからそっちに定義すればいいと思います。
16
-
17
-
18
- 個人的に気になった点を変更しました。
19
- * 命名規則を標準的なものに
20
- Javaではクラス名は通常パスカルケース(例:PascalCase)です。
21
- 大文字だけ(例:UPPER_SNAKE_CASE)は定数に使用します。
22
- (まあ「個人で作っているものは好きにつけりゃいいじゃん」と思っていますが、標準のもの(`ArrayList`等)と混じると読みにくいので^^;
23
-
24
- * 同じような意味の`move`・`display`と、`update`・`draw`があるのは気持ち悪いので統一
25
- 2つのプログラムを合体させた感じ?(いろいろ参考にされていると思いますが
26
-
27
- * つど`loadImage`してしまっているのは大変無駄
28
- せっかくクラスを作ったのだから、メンバ変数に入れときましょう。
29
-
30
- * `N`や`n`はわかりにくいので、`length`や拡張for文に置き換える
31
- 特にグローバル変数は多いほど読みにくくなるので、使わずに済む場合は積極的に減らします。
32
-
33
-
34
- ```Processing
35
- Player player;
36
- Enemy[] enemys = new Enemy[5];
37
- Star[] stars = new Star[100];
38
- ArrayList<Shot> shotList = new ArrayList<Shot>();
39
-
40
-
41
- void setup() {
42
- size(300, 600);
43
- frameRate(30);
44
- rectMode(CENTER);
45
- imageMode(CENTER);
46
- noCursor();
47
-
48
- player = new Player(loadImage("hikou.png"));
49
-
50
- PImage png = loadImage("enemy.png");
51
- for (int i = 0; i < enemys.length; i++) {
52
- enemys[i] = new Enemy(png);
53
- }
54
-
55
- for (int i = 0; i < stars.length; i++) {
56
- stars[i] = new Star();
57
- }
58
- }
59
-
60
- void draw() {
61
- background(0);
62
-
63
- player.update();
64
-
65
- for (Star star : stars) {
66
- star.update();
67
- star.draw();
68
- }
69
-
70
- boolean gameOver = false;
71
- for (Enemy enemy : enemys) {
72
- enemy.update();
73
-
74
- // 弾に当たっているか確認のため全弾ループ
75
- for (int i = shotList.size () - 1; i >= 0; i--) {
76
- Shot shot = shotList.get(i);
77
- if (enemy.isHit(shot.x, shot.y)) { // shotの座標と接触していれば...
78
- enemy.isDead = true; // フラグで管理するならこう
79
- //enemy.init(); // でも敵がいなくなってしまうので単に初期化でいい気が^^;
80
- shotList.remove(i);
81
- println("hit");
82
- }
83
- }
84
-
85
- // ついでに自機との接触も
86
- if (enemy.isHit(player.x, player.y)) { // playerの座標と接触していれば...
87
- gameOver = true;
88
- }
89
-
90
- enemy.draw();
91
- }
92
-
93
- for (int i = shotList.size () - 1; i >= 0; i--) {
94
- Shot shot = shotList.get(i);
95
- shot.update();
96
- if (shot.isAlive()) {
97
- shot.draw();
98
- } else {
99
- shotList.remove(i);
100
- }
101
- }
102
-
103
- player.draw();
104
-
105
- if (gameOver) {
106
- fill(255);
107
- textSize(40);
108
- textAlign(CENTER, CENTER);
109
- text("GAME OVER", width / 2, height / 2);
110
- noLoop();
111
- }
112
- }
113
-
114
- void mousePressed() {
115
- shotList.add(new Shot(player.x, player.y));
116
- }
117
-
118
-
119
- class Player {
120
- PImage image;
121
- float x;
122
- float y;
123
-
124
- Player(PImage _image) {
125
- image = _image;
126
- }
127
-
128
- void update() {
129
- x = mouseX;
130
- y = mouseY;
131
- }
132
-
133
- void draw() {
134
- image(image, x, y);
135
- }
136
- }
137
-
138
- class Enemy {
139
- PImage image;
140
- float x;
141
- float y;
142
- float vy = 5;
143
- float size = 40;
144
- boolean isDead; // フラグを作るならココ
145
-
146
- Enemy(PImage _image) {
147
- image = _image;
148
- init();
149
- y = random(-2 * height, -height);
150
- }
151
-
152
- void init() {
153
- isDead = false;
154
- y = random(-20);
155
- x = random(20, width - 20);
156
- }
157
-
158
- void draw() {
159
- if (isDead) return;
160
-
161
- image(image, x, y);
162
- }
163
-
164
- void update() {
165
- if (isDead) return;
166
-
167
- y += vy;
168
- if (height + size < y) {
169
- init();
170
- }
171
- }
172
-
173
- // Shot#isAliveと同じような感じで指定座標(_x・_y)との接触判定
174
- boolean isHit(float _x, float _y) {
175
- if (isDead) return false;
176
-
177
- float r = size / 2;
178
- return x - r < _x && _x < x + r && y - r < _y && _y < y + r;
179
- }
180
- }
181
-
182
- class Star {
183
- float x;
184
- float y;
185
- float vy;
186
- float len;
187
-
188
- Star() {
189
- init();
190
- y = random(-height, height);
191
- }
192
-
193
- void init() {
194
- y = random(-2 * height, -1);
195
- x = random(0, width);
196
- vy = random(10, 18);
197
- len = random(15, 25);
198
- }
199
-
200
- void update() {
201
- y += vy;
202
- if (height < y + len) {
203
- init();
204
- }
205
- }
206
-
207
- void draw() {
208
- stroke(200);
209
- line(x, y, x, y + len);
210
- }
211
- }
212
-
213
- class Shot {
214
- float x;
215
- float y;
216
- float vy = -15;
217
- float size = 6;
218
-
219
- Shot(float _x, float _y) {
220
- x = _x;
221
- y = _y;
222
- }
223
-
224
- void update() {
225
- y += vy;
226
- }
227
-
228
- void draw() {
229
- stroke(0);
230
- fill(255, 0, 0);
231
- rect(x, y, size, size);
232
- }
233
-
234
- boolean isAlive() {
235
- float r = size / 2;
236
- return 0 <= x + r && x - r <= width && 0 <= y + r && y - r <= height;
237
- }
238
- }
239
- ```
240
-
241
- ---
242
-
243
- 別にこれが正解というわけではありません。
1
+ [Java - シューティングゲームで、当たり判定を実装したい|teratail](https://teratail.com/questions/351596)
2
+ から1週間だいぶ試行錯誤しておられるようですね。
3
+ しかし残念ながら方向が違うような気がします。
4
+
5
+ Processingは仕組み上クラス内からグローバル変数にアクセスし放題なのですが、そのせいでスパゲッティコードにもなりやすいです(ありがたい時もあるんですが^^;
6
+ 例えば`ENEMY`の`x`・`y`が外に出てしまって(`ex`・`ey`)、前回より退化してしまっています。
7
+
8
+ > 敵や弾の速度が不安定になってしまっています。
9
+
10
+ ちゃんとは追っていませんが、おそらく`ex`・`ey`・`sx`・`sy`のせいです。
11
+
12
+ > boolean でhitflagを立てて、衝突範囲内に入った時にtrueを返すような
13
+
14
+ 敵が複数いるのですから、フラグも複数あるべきでは?
15
+ ひとつでもできないことはないでしょうが、ややこしくなるしすでにクラスがあるんですからそっちに定義すればいいと思います。
16
+
17
+
18
+ 個人的に気になった点を変更しました。
19
+ * 命名規則を標準的なものに
20
+ Javaではクラス名は通常パスカルケース(例:PascalCase)です。
21
+ 大文字だけ(例:UPPER_SNAKE_CASE)は定数に使用します。
22
+ (まあ「個人で作っているものは好きにつけりゃいいじゃん」と思っていますが、標準のもの(`ArrayList`等)と混じると読みにくいので^^;
23
+
24
+ * 同じような意味の`move`・`display`と、`update`・`draw`があるのは気持ち悪いので統一
25
+ 2つのプログラムを合体させた感じ?(いろいろ参考にされていると思いますが
26
+
27
+ * つど`loadImage`してしまっているのは大変無駄
28
+ せっかくクラスを作ったのだから、メンバ変数に入れときましょう。
29
+
30
+ * `N`や`n`はわかりにくいので、`length`や拡張for文に置き換える
31
+ 特にグローバル変数は多いほど読みにくくなるので、使わずに済む場合は積極的に減らします。
32
+
33
+
34
+ ```Processing
35
+ Player player;
36
+ Enemy[] enemies = new Enemy[5];
37
+ Star[] stars = new Star[100];
38
+ ArrayList<Shot> shotList = new ArrayList<Shot>();
39
+
40
+
41
+ void setup() {
42
+ size(300, 600);
43
+ frameRate(30);
44
+ rectMode(CENTER);
45
+ imageMode(CENTER);
46
+ noCursor();
47
+
48
+ player = new Player(loadImage("hikou.png"));
49
+
50
+ PImage png = loadImage("enemy.png");
51
+ for (int i = 0; i < enemies.length; i++) {
52
+ enemies[i] = new Enemy(png);
53
+ }
54
+
55
+ for (int i = 0; i < stars.length; i++) {
56
+ stars[i] = new Star();
57
+ }
58
+ }
59
+
60
+ void draw() {
61
+ background(0);
62
+
63
+ player.update();
64
+
65
+ for (Star star : stars) {
66
+ star.update();
67
+ star.draw();
68
+ }
69
+
70
+ boolean gameOver = false;
71
+ for (Enemy enemy : enemies) {
72
+ enemy.update();
73
+
74
+ // 弾に当たっているか確認のため全弾ループ
75
+ for (int i = shotList.size () - 1; i >= 0; i--) {
76
+ Shot shot = shotList.get(i);
77
+ if (enemy.isHit(shot.x, shot.y)) { // shotの座標と接触していれば...
78
+ enemy.isDead = true; // フラグで管理するならこう
79
+ //enemy.init(); // でも敵がいなくなってしまうので単に初期化でいい気が^^;
80
+ shotList.remove(i);
81
+ println("hit");
82
+ }
83
+ }
84
+
85
+ // ついでに自機との接触も
86
+ if (enemy.isHit(player.x, player.y)) { // playerの座標と接触していれば...
87
+ gameOver = true;
88
+ }
89
+
90
+ enemy.draw();
91
+ }
92
+
93
+ for (int i = shotList.size () - 1; i >= 0; i--) {
94
+ Shot shot = shotList.get(i);
95
+ shot.update();
96
+ if (shot.isAlive()) {
97
+ shot.draw();
98
+ } else {
99
+ shotList.remove(i);
100
+ }
101
+ }
102
+
103
+ player.draw();
104
+
105
+ if (gameOver) {
106
+ fill(255);
107
+ textSize(40);
108
+ textAlign(CENTER, CENTER);
109
+ text("GAME OVER", width / 2, height / 2);
110
+ noLoop();
111
+ }
112
+ }
113
+
114
+ void mousePressed() {
115
+ shotList.add(new Shot(player.x, player.y));
116
+ }
117
+
118
+
119
+ class Player {
120
+ PImage image;
121
+ float x;
122
+ float y;
123
+
124
+ Player(PImage _image) {
125
+ image = _image;
126
+ }
127
+
128
+ void update() {
129
+ x = mouseX;
130
+ y = mouseY;
131
+ }
132
+
133
+ void draw() {
134
+ image(image, x, y);
135
+ }
136
+ }
137
+
138
+ class Enemy {
139
+ PImage image;
140
+ float x;
141
+ float y;
142
+ float vy = 5;
143
+ float size = 40;
144
+ boolean isDead; // フラグを作るならココ
145
+
146
+ Enemy(PImage _image) {
147
+ image = _image;
148
+ init();
149
+ y = random(-2 * height, -height);
150
+ }
151
+
152
+ void init() {
153
+ isDead = false;
154
+ y = random(-20);
155
+ x = random(20, width - 20);
156
+ }
157
+
158
+ void draw() {
159
+ if (isDead) return;
160
+
161
+ image(image, x, y);
162
+ }
163
+
164
+ void update() {
165
+ if (isDead) return;
166
+
167
+ y += vy;
168
+ if (height + size < y) {
169
+ init();
170
+ }
171
+ }
172
+
173
+ // Shot#isAliveと同じような感じで指定座標(_x・_y)との接触判定
174
+ boolean isHit(float _x, float _y) {
175
+ if (isDead) return false;
176
+
177
+ float r = size / 2;
178
+ return x - r < _x && _x < x + r && y - r < _y && _y < y + r;
179
+ }
180
+ }
181
+
182
+ class Star {
183
+ float x;
184
+ float y;
185
+ float vy;
186
+ float len;
187
+
188
+ Star() {
189
+ init();
190
+ y = random(-height, height);
191
+ }
192
+
193
+ void init() {
194
+ y = random(-2 * height, -1);
195
+ x = random(0, width);
196
+ vy = random(10, 18);
197
+ len = random(15, 25);
198
+ }
199
+
200
+ void update() {
201
+ y += vy;
202
+ if (height < y + len) {
203
+ init();
204
+ }
205
+ }
206
+
207
+ void draw() {
208
+ stroke(200);
209
+ line(x, y, x, y + len);
210
+ }
211
+ }
212
+
213
+ class Shot {
214
+ float x;
215
+ float y;
216
+ float vy = -15;
217
+ float size = 6;
218
+
219
+ Shot(float _x, float _y) {
220
+ x = _x;
221
+ y = _y;
222
+ }
223
+
224
+ void update() {
225
+ y += vy;
226
+ }
227
+
228
+ void draw() {
229
+ stroke(0);
230
+ fill(255, 0, 0);
231
+ rect(x, y, size, size);
232
+ }
233
+
234
+ boolean isAlive() {
235
+ float r = size / 2;
236
+ return 0 <= x + r && x - r <= width && 0 <= y + r && y - r <= height;
237
+ }
238
+ }
239
+ ```
240
+
241
+ ---
242
+
243
+ 別にこれが正解というわけではありません。
244
244
  10人いたら10通りのコードになるでしょうし、あまりいじらないようにしたので「ちょっとどうかな?」という部分もあります。