質問編集履歴

1

gameLoopを別スレッドで実行しました

2016/08/31 08:28

投稿

退会済みユーザー
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
- MainPanel p = new MainPanel();
53
+ MainPanel p = new MainPanel();
54
-
55
-
56
-
57
-
58
-
54
+
55
+
56
+
57
+
58
+
59
- public static void main(String[] args) {
59
+ public static void main(String[] args) {
60
-
60
+
61
- Main m = new Main();
61
+ Main m = new Main();
62
-
62
+
63
- m.setVisible(true);
63
+ m.setVisible(true);
64
-
64
+
65
- }
65
+ }
66
-
67
-
68
-
66
+
67
+
68
+
69
- Main() {
69
+ Main() {
70
-
70
+
71
- setBounds(100, 150, 708, 532);
71
+ setBounds(100, 150, 708, 532);
72
-
72
+
73
- setDefaultCloseOperation(EXIT_ON_CLOSE);
73
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
74
-
75
-
76
-
74
+
75
+
76
+
77
- add(p);
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
- 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
- levelsetter.setMajorTickSpacing(5);
138
-
139
- levelsetter.setPaintTicks(true);
140
-
141
- levelsetter.setSnapToTicks(true);
142
-
143
- levelsetter.setLabelTable(levelsetter.createStandardLabels(10));
144
-
145
- levelsetter.setPaintLabels(true);
146
-
147
- levelsetter.setFont(new Font("MSゴシック", Font.BOLD, 18));
148
-
149
- levelsetter.setForeground(Color.BLUE);
150
-
151
- levelsetter.setBounds(525, 70, 150, 50);
152
-
153
- levelsetter.setOpaque(false);
154
-
155
- start.setContentAreaFilled(false);
156
-
157
- start.setBorderPainted(false);
158
-
159
- start.setFont(new Font("MSゴシック",Font.BOLD,20));
160
-
161
- start.setForeground(Color.WHITE);
162
-
163
- start.setText("CLICKtoPLAY");
164
-
165
- start.setBounds(490,400,190,40);
166
-
167
- start.addActionListener(this);
168
-
169
- add(levelsetter);
170
-
171
- add(start);
172
-
173
- gameLoop();
174
-
175
- }
176
-
177
-
178
-
179
- public void gameLoop() {
180
-
181
- while(true){
182
-
183
- if(!goRightKey.isPressed()&&!goLeftKey.isPressed())
184
-
185
- pp.horizontalAccelaration(0);
186
-
187
- else if(goRightKey.isPressed())
188
-
189
- pp.horizontalAccelaration(1);
190
-
191
- else if(goLeftKey.isPressed())
192
-
193
- pp.horizontalAccelaration(2);
194
-
195
-
196
-
197
- if(!goUpKey.isPressed()&&!goDownKey.isPressed())
198
-
199
- pp.horizontalAccelaration(0);
200
-
201
- else if(goUpKey.isPressed())
202
-
203
- pp.horizontalAccelaration(1);
204
-
205
- else if(goDownKey.isPressed())
206
-
207
- pp.horizontalAccelaration(2);
208
-
209
- x = pp.getX();
210
-
211
- y = pp.getY();
212
-
213
- pp.update();
214
-
215
- }
216
-
217
- }
218
-
219
-
220
-
221
- public void paintComponent(Graphics g){
222
-
223
- Graphics2D g2 = (Graphics2D)g;
224
-
225
- g2.setBackground(Color.WHITE);
226
-
227
- g2.clearRect(0, 0, getWidth(), getHeight());
228
-
229
- g2.setColor(Color.GRAY);
230
-
231
- g2.fillRect(500, 0,200,600);
232
-
233
- g2.setColor(Color.BLACK);
234
-
235
- g2.setFont(new Font("MSゴシック",Font.PLAIN,15));
236
-
237
- g2.drawString("難易度(高いほど難しい)", 510, 50);
238
-
239
- g2.setFont(new Font("MSゴシック",Font.BOLD,30));
240
-
241
- g2.drawString("SCORE", 520, 300);
242
-
243
- g2.setFont(new Font("MSゴシック",Font.PLAIN,30));
244
-
245
- g2.drawString(String.valueOf(score), 520, 340);
246
-
247
- g2.fillRect((int)x,(int)y,PlayerPixel.WIDTH,PlayerPixel.HEIGHT);
248
-
249
-
250
-
251
- }
252
-
253
- @Override
254
-
255
- public void actionPerformed(ActionEvent e) {
256
-
257
- Object o = e.getSource();
258
-
259
- if(o == start){
260
-
261
- pp.setspeed(levelsetter.getValue());
262
-
263
- }
264
-
265
- }
266
-
267
- @Override
268
-
269
- public void keyPressed(KeyEvent e) {
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
- goDownKey.press();
297
+ goDownKey.press();
290
-
298
+
291
- }
299
+ }
292
-
300
+
293
- @Override
301
+ @Override
294
-
302
+
295
- public void keyReleased(KeyEvent e) {
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
- goDownKey.release();
323
+ goDownKey.release();
316
-
324
+
317
- }
325
+ }
318
-
326
+
319
- @Override
327
+ @Override
320
-
328
+
321
- public void keyTyped(KeyEvent e) {}
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
- public static final int STOP = 0;
334
-
335
- public static final int RIGHT = 1;
336
-
337
- public static final int LEFT = 2;
338
-
339
- public static final int UP = 1;
340
-
341
- public static final int DOWN = 0;
342
-
343
- public static final int WIDTH = 40;
344
-
345
- public static final int HEIGHT = 40;
346
-
347
- private double speed;
348
-
349
- private double vectX;
350
-
351
- private double vectY;
352
-
353
- private double x;
354
-
355
- private double y;
356
-
357
-
358
-
359
- void horizontalAccelaration(int dir){
360
-
361
- if(dir == STOP)
362
-
363
- vectX = 0;
364
-
365
- else if(dir == RIGHT)
366
-
367
- vectX = speed;
368
-
369
- else if(dir == LEFT)
370
-
371
- vectX = -speed;
372
-
373
- }
374
-
375
-
376
-
377
- void varticalAccelaration(int dir){
378
-
379
- if(dir == STOP)
380
-
381
- vectY = 0;
382
-
383
- else if(dir == UP)
384
-
385
- vectY = speed;
386
-
387
- else if(dir == DOWN)
388
-
389
- vectY = -speed;
390
-
391
- }
392
-
393
-
394
-
395
- void update(){
396
-
397
- double newX = x + vectX;
398
-
399
- //x座標の範囲指定
400
-
401
- if(newX >= 500){
402
-
403
- newX = 500;
404
-
405
- vectX = 0;
406
-
407
- }
408
-
409
- if(newX <= 0){
410
-
411
- newX = 0;
412
-
413
- vectX = 0;
414
-
415
- }
416
-
417
- x = newX;
418
-
419
-
420
-
421
- double newY = y + vectY;
422
-
423
- //y座標の範囲指定
424
-
425
- if(newY >= 500){
426
-
427
- newY = 500;
428
-
429
- vectY = 0;
430
-
431
- }
432
-
433
- if(newY <= 0){
434
-
435
- newY = 0;
436
-
437
- vectY = 0;
438
-
439
- }
440
-
441
- y = newY;
442
-
443
-
444
-
445
- }
446
-
447
-
448
-
449
- public void setspeed(int dif) {
450
-
451
- speed = 50 - dif / 5;
452
-
453
- }
454
-
455
-
456
-
457
- public double getX(){
458
-
459
- return x;
460
-
461
- }
462
-
463
-
464
-
465
- public double getY(){
466
-
467
- return y;
468
-
469
- }
470
-
471
-
472
-
473
- PlayerPixel(){
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
- public static final int STATE_PRESSED = 0;
486
-
487
- public static final int STATE_RELEASED = 1;
488
-
489
- private int state;
490
-
491
- private int amount;
492
-
493
-
494
-
495
- ActionKey(){
496
-
497
- reset();
498
-
499
- }
500
-
501
-
502
-
503
- void reset(){
504
-
505
- state = STATE_RELEASED;
506
-
507
- amount = 0;
508
-
509
- }
510
-
511
-
512
-
513
- void press(){
514
-
515
- state = STATE_PRESSED;
516
-
517
- amount++;
518
-
519
- }
520
-
521
-
522
-
523
- void release(){
524
-
525
- state = STATE_RELEASED;
526
-
527
- }
528
-
529
-
530
-
531
- boolean isPressed(){
532
-
533
- if(amount != 0){
534
-
535
- if(state == STATE_RELEASED)
536
-
537
- amount = 0;
538
-
539
- return true;
540
-
541
- }
542
-
543
- return false;
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