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

質問編集履歴

1

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

2016/08/31 08:28

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -23,129 +23,133 @@
23
23
 
24
24
  @SuppressWarnings("serial")
25
25
  public class Main extends JFrame{
26
-
27
- MainPanel p = new MainPanel();
28
-
29
26
 
30
- public static void main(String[] args) {
31
- Main m = new Main();
27
+ MainPanel p = new MainPanel();
32
- m.setVisible(true);
33
- }
34
28
 
35
- Main() {
36
- setBounds(100, 150, 708, 532);
37
- setDefaultCloseOperation(EXIT_ON_CLOSE);
38
-
39
- add(p);
40
- }
41
29
 
30
+ public static void main(String[] args) {
31
+ Main m = new Main();
32
+ m.setVisible(true);
33
+ }
34
+
35
+ Main() {
36
+ setBounds(100, 150, 708, 532);
37
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
38
+
39
+ add(p);
40
+ }
41
+
42
42
  }
43
43
 
44
44
  @SuppressWarnings("serial")
45
- class MainPanel extends JPanel implements ActionListener , KeyListener {
45
+ class MainPanel extends JPanel implements ActionListener , KeyListener ,Runnable{
46
-
46
+ private Thread gameLoop;
47
- private ActionKey goRightKey;
47
+ private ActionKey goRightKey;
48
- private ActionKey goLeftKey;
48
+ private ActionKey goLeftKey;
49
- private ActionKey goUpKey;
49
+ private ActionKey goUpKey;
50
- private ActionKey goDownKey;
50
+ private ActionKey goDownKey;
51
-
52
- private JSlider levelsetter = new JSlider(0, 50);
53
- private JButton start = new JButton();
54
-
55
- private PlayerPixel pp = new PlayerPixel();
56
-
57
- private int difficultly;
58
- private int score;
59
- double x;
60
- private double y;
61
-
62
- MainPanel(){
63
- goRightKey = new ActionKey();
64
- goLeftKey = new ActionKey();
65
- goUpKey = new ActionKey();
66
- goDownKey = new ActionKey();
67
- setFocusable(true);
68
- setLayout(null);
69
- levelsetter.setMajorTickSpacing(5);
70
- levelsetter.setPaintTicks(true);
71
- levelsetter.setSnapToTicks(true);
72
- levelsetter.setLabelTable(levelsetter.createStandardLabels(10));
73
- levelsetter.setPaintLabels(true);
74
- levelsetter.setFont(new Font("MSゴシック", Font.BOLD, 18));
75
- levelsetter.setForeground(Color.BLUE);
76
- levelsetter.setBounds(525, 70, 150, 50);
77
- levelsetter.setOpaque(false);
78
- start.setContentAreaFilled(false);
79
- start.setBorderPainted(false);
80
- start.setFont(new Font("MSゴシック",Font.BOLD,20));
81
- start.setForeground(Color.WHITE);
82
- start.setText("CLICKtoPLAY");
83
- start.setBounds(490,400,190,40);
84
- start.addActionListener(this);
85
- add(levelsetter);
86
- add(start);
87
- gameLoop();
88
- }
89
-
90
- public void gameLoop() {
91
- while(true){
92
- if(!goRightKey.isPressed()&&!goLeftKey.isPressed())
93
- pp.horizontalAccelaration(0);
94
- else if(goRightKey.isPressed())
95
- pp.horizontalAccelaration(1);
96
- else if(goLeftKey.isPressed())
97
- pp.horizontalAccelaration(2);
98
51
 
99
- if(!goUpKey.isPressed()&&!goDownKey.isPressed())
52
+ private JSlider levelsetter = new JSlider(0, 50);
100
- pp.horizontalAccelaration(0);
53
+ private JButton start = new JButton();
101
- else if(goUpKey.isPressed())
102
- pp.horizontalAccelaration(1);
103
- else if(goDownKey.isPressed())
104
- pp.horizontalAccelaration(2);
105
- x = pp.getX();
106
- y = pp.getY();
107
- pp.update();
108
- }
109
- }
110
-
111
- public void paintComponent(Graphics g){
112
- Graphics2D g2 = (Graphics2D)g;
113
- g2.setBackground(Color.WHITE);
114
- g2.clearRect(0, 0, getWidth(), getHeight());
115
- g2.setColor(Color.GRAY);
116
- g2.fillRect(500, 0,200,600);
117
- g2.setColor(Color.BLACK);
118
- g2.setFont(new Font("MSゴシック",Font.PLAIN,15));
119
- g2.drawString("難易度(高いほど難しい)", 510, 50);
120
- g2.setFont(new Font("MSゴシック",Font.BOLD,30));
121
- g2.drawString("SCORE", 520, 300);
122
- g2.setFont(new Font("MSゴシック",Font.PLAIN,30));
123
- g2.drawString(String.valueOf(score), 520, 340);
124
- g2.fillRect((int)x,(int)y,PlayerPixel.WIDTH,PlayerPixel.HEIGHT);
125
54
 
55
+ private PlayerPixel pp = new PlayerPixel();
56
+
57
+ private int difficultly;
58
+ private int score;
59
+ double x;
60
+ private double y;
61
+
62
+ MainPanel(){
63
+ goRightKey = new ActionKey();
64
+ goLeftKey = new ActionKey();
65
+ goUpKey = new ActionKey();
66
+ goDownKey = new ActionKey();
67
+ setFocusable(true);
68
+ setLayout(null);
69
+ addKeyListener(this);
70
+ levelsetter.setMajorTickSpacing(5);
71
+ levelsetter.setPaintTicks(true);
72
+ levelsetter.setSnapToTicks(true);
73
+ levelsetter.setLabelTable(levelsetter.createStandardLabels(10));
74
+ levelsetter.setPaintLabels(true);
75
+ levelsetter.setFont(new Font("MSゴシック", Font.BOLD, 18));
76
+ levelsetter.setForeground(Color.BLUE);
77
+ levelsetter.setBounds(525, 70, 150, 50);
78
+ levelsetter.setOpaque(false);
79
+ start.setContentAreaFilled(false);
80
+ start.setBorderPainted(false);
81
+ start.setFont(new Font("MSゴシック",Font.BOLD,20));
82
+ start.setForeground(Color.WHITE);
83
+ start.setText("CLICKtoPLAY");
84
+ start.setBounds(490,400,190,40);
85
+ start.addActionListener(this);
86
+ add(levelsetter);
87
+ add(start);
88
+
126
- }
89
+ }
90
+
91
+ public void run() {
92
+ while(true){
93
+ if(!goRightKey.isPressed()&&!goLeftKey.isPressed())
94
+ pp.horizontalAccelaration(0);
95
+ else if(goRightKey.isPressed())
96
+ pp.horizontalAccelaration(1);
97
+ else if(goLeftKey.isPressed())
98
+ pp.horizontalAccelaration(2);
99
+
100
+ if(!goUpKey.isPressed()&&!goDownKey.isPressed())
101
+ pp.horizontalAccelaration(0);
102
+ else if(goUpKey.isPressed())
103
+ pp.horizontalAccelaration(1);
104
+ else if(goDownKey.isPressed())
105
+ pp.horizontalAccelaration(2);
106
+ x = pp.getX();
107
+ y = pp.getY();
108
+ pp.update();
109
+ }
110
+ }
111
+
112
+ public void paintComponent(Graphics g){
113
+ Graphics2D g2 = (Graphics2D)g;
114
+ g2.setBackground(Color.WHITE);
115
+ g2.clearRect(0, 0, getWidth(), getHeight());
116
+ g2.setColor(Color.GRAY);
117
+ g2.fillRect(500, 0,200,600);
118
+ g2.setColor(Color.BLACK);
119
+ g2.setFont(new Font("MSゴシック",Font.PLAIN,15));
120
+ g2.drawString("難易度(高いほど難しい)", 510, 50);
121
+ g2.setFont(new Font("MSゴシック",Font.BOLD,30));
122
+ g2.drawString("SCORE", 520, 300);
123
+ g2.setFont(new Font("MSゴシック",Font.PLAIN,30));
124
+ g2.drawString(String.valueOf(score), 520, 340);
125
+ g2.fillRect((int)x,(int)y,PlayerPixel.WIDTH,PlayerPixel.HEIGHT);
126
+
127
+ }
127
- @Override
128
+ @Override
128
- public void actionPerformed(ActionEvent e) {
129
+ public void actionPerformed(ActionEvent e) {
129
- Object o = e.getSource();
130
+ Object o = e.getSource();
130
- if(o == start){
131
+ if(o == start){
131
- pp.setspeed(levelsetter.getValue());
132
+ pp.setspeed(levelsetter.getValue());
133
+ gameLoop = new Thread(this);
134
+ gameLoop.start();
132
- }
135
+ }
133
- }
136
+ }
134
- @Override
137
+ @Override
135
- public void keyPressed(KeyEvent e) {
138
+ public void keyPressed(KeyEvent e) {
136
139
  int key = e.getKeyCode();
137
140
 
138
141
  if (key == KeyEvent.VK_LEFT)
142
+ System.out.println(1231312);
139
- goLeftKey.press();
143
+ //goLeftKey.press();
140
144
  if (key == KeyEvent.VK_RIGHT)
141
145
  goRightKey.press();
142
146
  if (key == KeyEvent.VK_UP)
143
147
  goUpKey.press();
144
148
  if(key == KeyEvent.VK_DOWN)
145
- goDownKey.press();
149
+ goDownKey.press();
146
- }
150
+ }
147
- @Override
151
+ @Override
148
- public void keyReleased(KeyEvent e) {
152
+ public void keyReleased(KeyEvent e) {
149
153
  int key = e.getKeyCode();
150
154
 
151
155
  if (key == KeyEvent.VK_LEFT)
@@ -155,122 +159,122 @@
155
159
  if (key == KeyEvent.VK_UP)
156
160
  goUpKey.release();
157
161
  if(key == KeyEvent.VK_DOWN)
158
- goDownKey.release();
162
+ goDownKey.release();
159
- }
163
+ }
160
- @Override
164
+ @Override
161
- public void keyTyped(KeyEvent e) {}
165
+ public void keyTyped(KeyEvent e) {}
162
-
166
+
163
167
  }
164
168
 
165
169
 
166
170
  class PlayerPixel {
167
- public static final int STOP = 0;
171
+ public static final int STOP = 0;
168
- public static final int RIGHT = 1;
172
+ public static final int RIGHT = 1;
169
- public static final int LEFT = 2;
173
+ public static final int LEFT = 2;
170
- public static final int UP = 1;
174
+ public static final int UP = 1;
171
- public static final int DOWN = 0;
175
+ public static final int DOWN = 0;
172
- public static final int WIDTH = 40;
176
+ public static final int WIDTH = 40;
173
- public static final int HEIGHT = 40;
177
+ public static final int HEIGHT = 40;
174
- private double speed;
178
+ private double speed;
175
- private double vectX;
179
+ private double vectX;
176
- private double vectY;
180
+ private double vectY;
177
- private double x;
181
+ private double x;
178
- private double y;
182
+ private double y;
179
-
180
- void horizontalAccelaration(int dir){
181
- if(dir == STOP)
182
- vectX = 0;
183
- else if(dir == RIGHT)
184
- vectX = speed;
185
- else if(dir == LEFT)
186
- vectX = -speed;
187
- }
188
-
189
- void varticalAccelaration(int dir){
190
- if(dir == STOP)
191
- vectY = 0;
192
- else if(dir == UP)
193
- vectY = speed;
194
- else if(dir == DOWN)
195
- vectY = -speed;
196
- }
197
-
198
- void update(){
199
- double newX = x + vectX;
200
- //x座標の範囲指定
201
- if(newX >= 500){
202
- newX = 500;
203
- vectX = 0;
204
- }
205
- if(newX <= 0){
206
- newX = 0;
207
- vectX = 0;
208
- }
209
- x = newX;
210
-
211
- double newY = y + vectY;
212
- //y座標の範囲指定
213
- if(newY >= 500){
214
- newY = 500;
215
- vectY = 0;
216
- }
217
- if(newY <= 0){
218
- newY = 0;
219
- vectY = 0;
220
- }
221
- y = newY;
222
-
223
- }
224
183
 
225
- public void setspeed(int dif) {
184
+ void horizontalAccelaration(int dir){
185
+ if(dir == STOP)
186
+ vectX = 0;
226
- speed = 50 - dif / 5;
187
+ else if(dir == RIGHT)
188
+ vectX = speed;
189
+ else if(dir == LEFT)
190
+ vectX = -speed;
227
- }
191
+ }
228
-
229
- public double getX(){
230
- return x;
231
- }
232
-
233
- public double getY(){
234
- return y;
235
- }
236
-
237
- PlayerPixel(){
238
-
239
- }
240
192
 
193
+ void varticalAccelaration(int dir){
194
+ if(dir == STOP)
195
+ vectY = 0;
196
+ else if(dir == UP)
197
+ vectY = speed;
198
+ else if(dir == DOWN)
199
+ vectY = -speed;
200
+ }
201
+
202
+ void update(){
203
+ double newX = x + vectX;
204
+ //x座標の範囲指定
205
+ if(newX >= 500){
206
+ newX = 500;
207
+ vectX = 0;
208
+ }
209
+ if(newX <= 0){
210
+ newX = 0;
211
+ vectX = 0;
212
+ }
213
+ x = newX;
214
+
215
+ double newY = y + vectY;
216
+ //y座標の範囲指定
217
+ if(newY >= 500){
218
+ newY = 500;
219
+ vectY = 0;
220
+ }
221
+ if(newY <= 0){
222
+ newY = 0;
223
+ vectY = 0;
224
+ }
225
+ y = newY;
226
+
227
+ }
228
+
229
+ public void setspeed(int dif) {
230
+ speed = 50 - dif / 5;
231
+ }
232
+
233
+ public double getX(){
234
+ return x;
235
+ }
236
+
237
+ public double getY(){
238
+ return y;
239
+ }
240
+
241
+ PlayerPixel(){
242
+
243
+ }
244
+
241
245
  }
242
246
  class ActionKey{
243
- public static final int STATE_PRESSED = 0;
247
+ public static final int STATE_PRESSED = 0;
244
- public static final int STATE_RELEASED = 1;
248
+ public static final int STATE_RELEASED = 1;
245
- private int state;
249
+ private int state;
246
- private int amount;
250
+ private int amount;
247
-
251
+
248
- ActionKey(){
252
+ ActionKey(){
249
- reset();
253
+ reset();
250
- }
254
+ }
251
-
255
+
252
- void reset(){
256
+ void reset(){
253
- state = STATE_RELEASED;
257
+ state = STATE_RELEASED;
254
- amount = 0;
258
+ amount = 0;
255
- }
259
+ }
256
-
260
+
257
- void press(){
261
+ void press(){
258
- state = STATE_PRESSED;
262
+ state = STATE_PRESSED;
259
- amount++;
263
+ amount++;
260
- }
264
+ }
261
-
265
+
262
- void release(){
266
+ void release(){
263
- state = STATE_RELEASED;
267
+ state = STATE_RELEASED;
264
- }
268
+ }
265
-
269
+
266
- boolean isPressed(){
270
+ boolean isPressed(){
267
- if(amount != 0){
271
+ if(amount != 0){
268
- if(state == STATE_RELEASED)
272
+ if(state == STATE_RELEASED)
269
- amount = 0;
273
+ amount = 0;
270
- return true;
274
+ return true;
271
- }
275
+ }
272
- return false;
276
+ return false;
273
- }
277
+ }
274
278
  }
275
279
  ```
276
280