回答編集履歴

2

少し修正

2018/01/04 16:03

投稿

退会済みユーザー
test CHANGED
@@ -280,6 +280,8 @@
280
280
 
281
281
  callCounter++;
282
282
 
283
+ if (hitObject) hitObject = false;
284
+
283
285
  long pastTime = System.currentTimeMillis() - startTime;
284
286
 
285
287
  if (pastTime >= TIMEUP) {

1

コードの追加

2018/01/04 16:02

投稿

退会済みユーザー
test CHANGED
@@ -19,3 +19,503 @@
19
19
 
20
20
 
21
21
  楽しいゲームを作ってくださいね。
22
+
23
+ ---
24
+
25
+ 我慢できず作ってみました。コメントも少し入れました。
26
+
27
+ 調整不十分ですけど、だいたいこんな感じでいかがですか。
28
+
29
+ あとは適当にいじり倒してください。
30
+
31
+ ```java
32
+
33
+ import java.awt.Dimension;
34
+
35
+ import java.awt.Graphics;
36
+
37
+ import java.awt.Image;
38
+
39
+ import java.awt.Point;
40
+
41
+ import java.awt.Rectangle;
42
+
43
+ import java.awt.event.ActionEvent;
44
+
45
+ import java.awt.event.ActionListener;
46
+
47
+ import java.awt.event.MouseAdapter;
48
+
49
+ import java.awt.event.MouseEvent;
50
+
51
+ import java.io.FileInputStream;
52
+
53
+ import java.io.IOException;
54
+
55
+ import java.util.ArrayList;
56
+
57
+ import java.util.Random;
58
+
59
+
60
+
61
+ import javax.imageio.ImageIO;
62
+
63
+ import javax.swing.JFrame;
64
+
65
+ import javax.swing.JPanel;
66
+
67
+ import javax.swing.Timer;
68
+
69
+
70
+
71
+ public class WaniWaniPanic extends JFrame {
72
+
73
+ public WaniWaniPanic() {
74
+
75
+ super("わにわにパニック");
76
+
77
+ setContentPane(new WaniPaniCanvas());
78
+
79
+ pack();
80
+
81
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
82
+
83
+ setResizable(false);
84
+
85
+ setVisible(true);
86
+
87
+ }
88
+
89
+
90
+
91
+ public static void main(String[] args) {
92
+
93
+ new WaniWaniPanic();
94
+
95
+ }
96
+
97
+ }
98
+
99
+
100
+
101
+ class WaniPaniCanvas extends JPanel {
102
+
103
+
104
+
105
+ private static final long serialVersionUID = 1L;
106
+
107
+
108
+
109
+ static final int START_WAITING = 0;
110
+
111
+ static final int PLAYING = 1;
112
+
113
+ static final int ENDING = 2;
114
+
115
+
116
+
117
+ static final int IMG_TITLE = 0;
118
+
119
+ static final int IMG_WANI_NORMAL = 1;
120
+
121
+ static final int IMG_WANI_DAMAGED = 2;
122
+
123
+ static final int IMG_BOMB_NORMAL = 3;
124
+
125
+ static final int IMG_BOMB_BOMB = 4;
126
+
127
+ static final int IMG_ENDING = 5;
128
+
129
+
130
+
131
+ static final int WANI_SELECTED = 0;
132
+
133
+ static final int BOMB_SELECTED = 1;
134
+
135
+
136
+
137
+ static final int SPEED_LOW = 3;
138
+
139
+ static final int SPEED_MIDDLE = 2;
140
+
141
+ static final int SPEED_HIGH = 1;
142
+
143
+
144
+
145
+ static final String IMG_NAMES[] = { // 適当に画像作ってください。
146
+
147
+ "title.png", //640 x 480
148
+
149
+ "wani_normal.png", //70 x 70
150
+
151
+ "wani_damaged.png", //70 x 70
152
+
153
+ "bomb_normal.png", //70 x 70
154
+
155
+ "bomb_bomb.png", //70 x 70
156
+
157
+ "ending.png" //640 x 480
158
+
159
+ };
160
+
161
+
162
+
163
+ static final int TIMER_INTERVAL = 400; //タイマー処理の間隔 0.4秒
164
+
165
+ static final int TIMEUP = 60000; //タイムアップまでの時間 60秒
166
+
167
+
168
+
169
+ static final int CANVAS_WIDTH = 640;
170
+
171
+ static final int CANVAS_HEIGHT = 480;
172
+
173
+
174
+
175
+ static final int OBJECT_WIDTH = 70; // ワニやボム画像の幅
176
+
177
+ static final int OBJECT_HEIGHT = 70; // ワニやボム画像の高さ
178
+
179
+
180
+
181
+ static final Point LOCATION[] = { // 出現位置
182
+
183
+ new Point( 50, 150),
184
+
185
+ new Point(150, 150),
186
+
187
+ new Point(250, 150),
188
+
189
+ new Point(350, 150),
190
+
191
+ new Point(450, 150),
192
+
193
+ new Point(550, 150)
194
+
195
+ };
196
+
197
+
198
+
199
+ static final ArrayList<Image> images = new ArrayList<Image>();
200
+
201
+
202
+
203
+ static {
204
+
205
+ loadGameImages();
206
+
207
+ }
208
+
209
+
210
+
211
+ static void loadGameImages() {
212
+
213
+ for (String name : IMG_NAMES) {
214
+
215
+ try {
216
+
217
+ FileInputStream in = new FileInputStream(name);
218
+
219
+ Image img = ImageIO.read(in);
220
+
221
+ images.add(img);
222
+
223
+ in.close();
224
+
225
+ } catch (IOException ex) {
226
+
227
+ System.err.println("画像ファイル " + name + " が存在しません。");
228
+
229
+ System.exit(1);
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ final GameState gameStates[] = {
240
+
241
+ new StartWaiting(),
242
+
243
+ new Playing(),
244
+
245
+ new Ending()
246
+
247
+ };
248
+
249
+
250
+
251
+ private int currentState = START_WAITING;
252
+
253
+ private int bestScore = 0;
254
+
255
+ private int score = 0;
256
+
257
+ private boolean overBest = false; // ハイスコアをとったらtrue
258
+
259
+ private long startTime = 0L;
260
+
261
+ private int selectedLocation = 0; // ランダムで選択した出現位置
262
+
263
+ private int selectedObject = 0; // ランダムで選択した出現オブジェクト
264
+
265
+ private int callCounter = 0;
266
+
267
+ private boolean hitObject = false;
268
+
269
+
270
+
271
+ private Random random = new Random(System.currentTimeMillis());
272
+
273
+
274
+
275
+ private Timer timer = new Timer(TIMER_INTERVAL, new ActionListener() {
276
+
277
+ @Override
278
+
279
+ public void actionPerformed(ActionEvent e) {
280
+
281
+ callCounter++;
282
+
283
+ long pastTime = System.currentTimeMillis() - startTime;
284
+
285
+ if (pastTime >= TIMEUP) {
286
+
287
+ timer.stop();
288
+
289
+ currentState = ENDING;
290
+
291
+ if (score > bestScore) {
292
+
293
+ bestScore = score;
294
+
295
+ overBest = true;
296
+
297
+ }
298
+
299
+ } else if (pastTime <= TIMEUP * 0.2) {
300
+
301
+ if (callCounter % SPEED_LOW != 0) return;
302
+
303
+ } else if (pastTime <= TIMEUP * 0.85) {
304
+
305
+ if (callCounter % SPEED_MIDDLE != 0) return;
306
+
307
+ }
308
+
309
+
310
+
311
+ int loc = 0;
312
+
313
+ while ((loc = selectLocation()) == selectedLocation);
314
+
315
+
316
+
317
+ selectedLocation = loc;
318
+
319
+ selectedObject = selectObject();
320
+
321
+
322
+
323
+ WaniPaniCanvas.this.repaint();
324
+
325
+ }
326
+
327
+
328
+
329
+ private int selectLocation() {
330
+
331
+ return Math.abs(random.nextInt()) % LOCATION.length;
332
+
333
+ }
334
+
335
+
336
+
337
+ private int selectObject() {
338
+
339
+ int n = Math.abs(random.nextInt()) % 10;
340
+
341
+ if (n == 0) return BOMB_SELECTED;
342
+
343
+ else return WANI_SELECTED;
344
+
345
+ }
346
+
347
+ });
348
+
349
+
350
+
351
+ public WaniPaniCanvas() {
352
+
353
+ setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
354
+
355
+ addMouseListener(new MouseAdapter() {
356
+
357
+ @Override
358
+
359
+ public void mouseClicked(MouseEvent e) {
360
+
361
+ gameStates[currentState].mouseClicked(e);
362
+
363
+ }
364
+
365
+ });
366
+
367
+ }
368
+
369
+
370
+
371
+ public void paint(Graphics g) {
372
+
373
+ gameStates[currentState].paint(g);
374
+
375
+ }
376
+
377
+
378
+
379
+ interface GameState {
380
+
381
+ public void paint(Graphics g);
382
+
383
+ public void mouseClicked(MouseEvent e);
384
+
385
+ }
386
+
387
+
388
+
389
+ class StartWaiting implements GameState {
390
+
391
+ public void paint(Graphics g) {
392
+
393
+ g.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
394
+
395
+ Image img = images.get(IMG_TITLE);
396
+
397
+ g.drawImage(img, 0, 0, WaniPaniCanvas.this);
398
+
399
+ }
400
+
401
+
402
+
403
+ public void mouseClicked(MouseEvent e) {
404
+
405
+ // タイトル画面をクリックしたらゲームスタート
406
+
407
+ score = 0;
408
+
409
+ overBest = false;
410
+
411
+ selectedLocation = 0;
412
+
413
+ selectedObject = 0;
414
+
415
+ callCounter = 0;
416
+
417
+ hitObject = false;
418
+
419
+ startTime = System.currentTimeMillis();
420
+
421
+ currentState = PLAYING;
422
+
423
+ timer.start();
424
+
425
+ WaniPaniCanvas.this.repaint();
426
+
427
+ }
428
+
429
+ }
430
+
431
+
432
+
433
+ class Playing implements GameState {
434
+
435
+ public void paint(Graphics g) {
436
+
437
+ g.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
438
+
439
+ int index = IMG_WANI_NORMAL;
440
+
441
+ if (selectedObject == WANI_SELECTED && hitObject) {
442
+
443
+ index = IMG_WANI_DAMAGED;
444
+
445
+ } else if (selectedObject == BOMB_SELECTED) {
446
+
447
+ if (hitObject) index = IMG_BOMB_BOMB;
448
+
449
+ else index = IMG_BOMB_NORMAL;
450
+
451
+ }
452
+
453
+ Image img = images.get(index);
454
+
455
+ int x = LOCATION[selectedLocation].x;
456
+
457
+ int y = LOCATION[selectedLocation].y;
458
+
459
+ g.drawImage(img, x, y, WaniPaniCanvas.this);
460
+
461
+ g.drawString("得点: " + score, 10, 10);
462
+
463
+ }
464
+
465
+
466
+
467
+ public void mouseClicked(MouseEvent e) {
468
+
469
+ // 当たり判定
470
+
471
+ int x = LOCATION[selectedLocation].x;
472
+
473
+ int y = LOCATION[selectedLocation].y;
474
+
475
+ Rectangle rect = new Rectangle(x, y, OBJECT_WIDTH, OBJECT_HEIGHT);
476
+
477
+ if (rect.contains(e.getPoint())) score += 100;
478
+
479
+ WaniPaniCanvas.this.repaint();
480
+
481
+ }
482
+
483
+ }
484
+
485
+
486
+
487
+ class Ending implements GameState {
488
+
489
+ public void paint(Graphics g) {
490
+
491
+ g.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
492
+
493
+ Image img = images.get(IMG_ENDING);
494
+
495
+ g.drawImage(img, 0, 0, WaniPaniCanvas.this);
496
+
497
+ g.drawString("現在の最高得点: " + bestScore, 200, 200);
498
+
499
+ g.drawString("今回の得点: " + score, 200, 230);
500
+
501
+ if (overBest) g.drawString("ハイスコアです。おめでとう", 200, 260);
502
+
503
+ }
504
+
505
+
506
+
507
+ public void mouseClicked(MouseEvent e) {
508
+
509
+ currentState = START_WAITING;
510
+
511
+ WaniPaniCanvas.this.repaint();
512
+
513
+ }
514
+
515
+ }
516
+
517
+ }
518
+
519
+
520
+
521
+ ```