質問編集履歴
1
gameLoopを別スレッドで実行しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,35 +48,35 @@
|
|
48
48
|
|
49
49
|
public class Main extends JFrame{
|
50
50
|
|
51
|
-
|
52
|
-
|
51
|
+
|
52
|
+
|
53
|
-
|
53
|
+
MainPanel p = new MainPanel();
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
-
|
59
|
+
public static void main(String[] args) {
|
60
|
-
|
60
|
+
|
61
|
-
|
61
|
+
Main m = new Main();
|
62
|
-
|
62
|
+
|
63
|
-
|
63
|
+
m.setVisible(true);
|
64
|
-
|
64
|
+
|
65
|
-
|
65
|
+
}
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
-
|
69
|
+
Main() {
|
70
|
-
|
70
|
+
|
71
|
-
|
71
|
+
setBounds(100, 150, 708, 532);
|
72
|
-
|
72
|
+
|
73
|
-
|
73
|
+
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
74
|
-
|
75
|
-
|
76
|
-
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
-
|
77
|
+
add(p);
|
78
|
-
|
78
|
+
|
79
|
-
|
79
|
+
}
|
80
80
|
|
81
81
|
|
82
82
|
|
@@ -86,187 +86,193 @@
|
|
86
86
|
|
87
87
|
@SuppressWarnings("serial")
|
88
88
|
|
89
|
-
class MainPanel extends JPanel implements ActionListener , KeyListener {
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
89
|
+
class MainPanel extends JPanel implements ActionListener , KeyListener ,Runnable{
|
90
|
+
|
91
|
+
private Thread gameLoop;
|
92
|
+
|
93
|
+
private ActionKey goRightKey;
|
94
|
+
|
95
|
+
private ActionKey goLeftKey;
|
96
|
+
|
97
|
+
private ActionKey goUpKey;
|
98
|
+
|
99
|
+
private ActionKey goDownKey;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
private JSlider levelsetter = new JSlider(0, 50);
|
104
|
+
|
105
|
+
private JButton start = new JButton();
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
private PlayerPixel pp = new PlayerPixel();
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
private int difficultly;
|
114
|
+
|
115
|
+
private int score;
|
116
|
+
|
117
|
+
double x;
|
118
|
+
|
119
|
+
private double y;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
MainPanel(){
|
124
|
+
|
125
|
+
goRightKey = new ActionKey();
|
126
|
+
|
127
|
+
goLeftKey = new ActionKey();
|
128
|
+
|
129
|
+
goUpKey = new ActionKey();
|
130
|
+
|
131
|
+
goDownKey = new ActionKey();
|
132
|
+
|
133
|
+
setFocusable(true);
|
134
|
+
|
135
|
+
setLayout(null);
|
136
|
+
|
137
|
+
addKeyListener(this);
|
138
|
+
|
139
|
+
levelsetter.setMajorTickSpacing(5);
|
140
|
+
|
141
|
+
levelsetter.setPaintTicks(true);
|
142
|
+
|
143
|
+
levelsetter.setSnapToTicks(true);
|
144
|
+
|
145
|
+
levelsetter.setLabelTable(levelsetter.createStandardLabels(10));
|
146
|
+
|
147
|
+
levelsetter.setPaintLabels(true);
|
148
|
+
|
149
|
+
levelsetter.setFont(new Font("MSゴシック", Font.BOLD, 18));
|
150
|
+
|
151
|
+
levelsetter.setForeground(Color.BLUE);
|
152
|
+
|
153
|
+
levelsetter.setBounds(525, 70, 150, 50);
|
154
|
+
|
155
|
+
levelsetter.setOpaque(false);
|
156
|
+
|
157
|
+
start.setContentAreaFilled(false);
|
158
|
+
|
159
|
+
start.setBorderPainted(false);
|
160
|
+
|
161
|
+
start.setFont(new Font("MSゴシック",Font.BOLD,20));
|
162
|
+
|
163
|
+
start.setForeground(Color.WHITE);
|
164
|
+
|
165
|
+
start.setText("CLICKtoPLAY");
|
166
|
+
|
167
|
+
start.setBounds(490,400,190,40);
|
168
|
+
|
169
|
+
start.addActionListener(this);
|
170
|
+
|
171
|
+
add(levelsetter);
|
172
|
+
|
173
|
+
add(start);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public void run() {
|
182
|
+
|
183
|
+
while(true){
|
184
|
+
|
185
|
+
if(!goRightKey.isPressed()&&!goLeftKey.isPressed())
|
186
|
+
|
187
|
+
pp.horizontalAccelaration(0);
|
188
|
+
|
189
|
+
else if(goRightKey.isPressed())
|
190
|
+
|
191
|
+
pp.horizontalAccelaration(1);
|
192
|
+
|
193
|
+
else if(goLeftKey.isPressed())
|
194
|
+
|
195
|
+
pp.horizontalAccelaration(2);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
if(!goUpKey.isPressed()&&!goDownKey.isPressed())
|
200
|
+
|
201
|
+
pp.horizontalAccelaration(0);
|
202
|
+
|
203
|
+
else if(goUpKey.isPressed())
|
204
|
+
|
205
|
+
pp.horizontalAccelaration(1);
|
206
|
+
|
207
|
+
else if(goDownKey.isPressed())
|
208
|
+
|
209
|
+
pp.horizontalAccelaration(2);
|
210
|
+
|
211
|
+
x = pp.getX();
|
212
|
+
|
213
|
+
y = pp.getY();
|
214
|
+
|
215
|
+
pp.update();
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
public void paintComponent(Graphics g){
|
224
|
+
|
225
|
+
Graphics2D g2 = (Graphics2D)g;
|
226
|
+
|
227
|
+
g2.setBackground(Color.WHITE);
|
228
|
+
|
229
|
+
g2.clearRect(0, 0, getWidth(), getHeight());
|
230
|
+
|
231
|
+
g2.setColor(Color.GRAY);
|
232
|
+
|
233
|
+
g2.fillRect(500, 0,200,600);
|
234
|
+
|
235
|
+
g2.setColor(Color.BLACK);
|
236
|
+
|
237
|
+
g2.setFont(new Font("MSゴシック",Font.PLAIN,15));
|
238
|
+
|
239
|
+
g2.drawString("難易度(高いほど難しい)", 510, 50);
|
240
|
+
|
241
|
+
g2.setFont(new Font("MSゴシック",Font.BOLD,30));
|
242
|
+
|
243
|
+
g2.drawString("SCORE", 520, 300);
|
244
|
+
|
245
|
+
g2.setFont(new Font("MSゴシック",Font.PLAIN,30));
|
246
|
+
|
247
|
+
g2.drawString(String.valueOf(score), 520, 340);
|
248
|
+
|
249
|
+
g2.fillRect((int)x,(int)y,PlayerPixel.WIDTH,PlayerPixel.HEIGHT);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
@Override
|
256
|
+
|
257
|
+
public void actionPerformed(ActionEvent e) {
|
258
|
+
|
259
|
+
Object o = e.getSource();
|
260
|
+
|
261
|
+
if(o == start){
|
262
|
+
|
263
|
+
pp.setspeed(levelsetter.getValue());
|
264
|
+
|
265
|
+
gameLoop = new Thread(this);
|
266
|
+
|
267
|
+
gameLoop.start();
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
@Override
|
274
|
+
|
275
|
+
public void keyPressed(KeyEvent e) {
|
270
276
|
|
271
277
|
int key = e.getKeyCode();
|
272
278
|
|
@@ -274,7 +280,9 @@
|
|
274
280
|
|
275
281
|
if (key == KeyEvent.VK_LEFT)
|
276
282
|
|
283
|
+
System.out.println(1231312);
|
284
|
+
|
277
|
-
goLeftKey.press();
|
285
|
+
//goLeftKey.press();
|
278
286
|
|
279
287
|
if (key == KeyEvent.VK_RIGHT)
|
280
288
|
|
@@ -286,13 +294,13 @@
|
|
286
294
|
|
287
295
|
if(key == KeyEvent.VK_DOWN)
|
288
296
|
|
289
|
-
|
297
|
+
goDownKey.press();
|
290
|
-
|
298
|
+
|
291
|
-
|
299
|
+
}
|
292
|
-
|
300
|
+
|
293
|
-
|
301
|
+
@Override
|
294
|
-
|
302
|
+
|
295
|
-
|
303
|
+
public void keyReleased(KeyEvent e) {
|
296
304
|
|
297
305
|
int key = e.getKeyCode();
|
298
306
|
|
@@ -312,15 +320,15 @@
|
|
312
320
|
|
313
321
|
if(key == KeyEvent.VK_DOWN)
|
314
322
|
|
315
|
-
|
323
|
+
goDownKey.release();
|
316
|
-
|
324
|
+
|
317
|
-
|
325
|
+
}
|
318
|
-
|
326
|
+
|
319
|
-
|
327
|
+
@Override
|
320
|
-
|
328
|
+
|
321
|
-
|
329
|
+
public void keyTyped(KeyEvent e) {}
|
322
|
-
|
323
|
-
|
330
|
+
|
331
|
+
|
324
332
|
|
325
333
|
}
|
326
334
|
|
@@ -330,151 +338,151 @@
|
|
330
338
|
|
331
339
|
class PlayerPixel {
|
332
340
|
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
341
|
+
public static final int STOP = 0;
|
342
|
+
|
343
|
+
public static final int RIGHT = 1;
|
344
|
+
|
345
|
+
public static final int LEFT = 2;
|
346
|
+
|
347
|
+
public static final int UP = 1;
|
348
|
+
|
349
|
+
public static final int DOWN = 0;
|
350
|
+
|
351
|
+
public static final int WIDTH = 40;
|
352
|
+
|
353
|
+
public static final int HEIGHT = 40;
|
354
|
+
|
355
|
+
private double speed;
|
356
|
+
|
357
|
+
private double vectX;
|
358
|
+
|
359
|
+
private double vectY;
|
360
|
+
|
361
|
+
private double x;
|
362
|
+
|
363
|
+
private double y;
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
void horizontalAccelaration(int dir){
|
368
|
+
|
369
|
+
if(dir == STOP)
|
370
|
+
|
371
|
+
vectX = 0;
|
372
|
+
|
373
|
+
else if(dir == RIGHT)
|
374
|
+
|
375
|
+
vectX = speed;
|
376
|
+
|
377
|
+
else if(dir == LEFT)
|
378
|
+
|
379
|
+
vectX = -speed;
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
void varticalAccelaration(int dir){
|
386
|
+
|
387
|
+
if(dir == STOP)
|
388
|
+
|
389
|
+
vectY = 0;
|
390
|
+
|
391
|
+
else if(dir == UP)
|
392
|
+
|
393
|
+
vectY = speed;
|
394
|
+
|
395
|
+
else if(dir == DOWN)
|
396
|
+
|
397
|
+
vectY = -speed;
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
void update(){
|
404
|
+
|
405
|
+
double newX = x + vectX;
|
406
|
+
|
407
|
+
//x座標の範囲指定
|
408
|
+
|
409
|
+
if(newX >= 500){
|
410
|
+
|
411
|
+
newX = 500;
|
412
|
+
|
413
|
+
vectX = 0;
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
if(newX <= 0){
|
418
|
+
|
419
|
+
newX = 0;
|
420
|
+
|
421
|
+
vectX = 0;
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
x = newX;
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
double newY = y + vectY;
|
430
|
+
|
431
|
+
//y座標の範囲指定
|
432
|
+
|
433
|
+
if(newY >= 500){
|
434
|
+
|
435
|
+
newY = 500;
|
436
|
+
|
437
|
+
vectY = 0;
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
if(newY <= 0){
|
442
|
+
|
443
|
+
newY = 0;
|
444
|
+
|
445
|
+
vectY = 0;
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
y = newY;
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
public void setspeed(int dif) {
|
458
|
+
|
459
|
+
speed = 50 - dif / 5;
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
public double getX(){
|
466
|
+
|
467
|
+
return x;
|
468
|
+
|
469
|
+
}
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
public double getY(){
|
474
|
+
|
475
|
+
return y;
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
PlayerPixel(){
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
}
|
478
486
|
|
479
487
|
|
480
488
|
|
@@ -482,67 +490,67 @@
|
|
482
490
|
|
483
491
|
class ActionKey{
|
484
492
|
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
493
|
+
public static final int STATE_PRESSED = 0;
|
494
|
+
|
495
|
+
public static final int STATE_RELEASED = 1;
|
496
|
+
|
497
|
+
private int state;
|
498
|
+
|
499
|
+
private int amount;
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
ActionKey(){
|
504
|
+
|
505
|
+
reset();
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
void reset(){
|
512
|
+
|
513
|
+
state = STATE_RELEASED;
|
514
|
+
|
515
|
+
amount = 0;
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
void press(){
|
522
|
+
|
523
|
+
state = STATE_PRESSED;
|
524
|
+
|
525
|
+
amount++;
|
526
|
+
|
527
|
+
}
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
void release(){
|
532
|
+
|
533
|
+
state = STATE_RELEASED;
|
534
|
+
|
535
|
+
}
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
boolean isPressed(){
|
540
|
+
|
541
|
+
if(amount != 0){
|
542
|
+
|
543
|
+
if(state == STATE_RELEASED)
|
544
|
+
|
545
|
+
amount = 0;
|
546
|
+
|
547
|
+
return true;
|
548
|
+
|
549
|
+
}
|
550
|
+
|
551
|
+
return false;
|
552
|
+
|
553
|
+
}
|
546
554
|
|
547
555
|
}
|
548
556
|
|