質問編集履歴

2

コードの変更

2017/11/25 18:07

投稿

Alpa
Alpa

スコア80

test CHANGED
File without changes
test CHANGED
@@ -1,16 +1,438 @@
1
1
  ```java
2
2
 
3
- JPanel panel = new JPanel();
4
-
5
-
6
-
7
- ImageIcon icon = new ImageIcon("./xxx");
8
-
9
- Jlabel block = new JLabel(icon);
10
-
11
-
12
-
13
- panel.add(block);
3
+ import javax.swing.*;//swingのimport
4
+
5
+ import java.awt.*;//awtのimport
6
+
7
+ import java.awt.event.*;//awtのeventのimport
8
+
9
+
10
+
11
+ public class action extends JFrame implements ActionListener{
12
+
13
+
14
+
15
+ static action Frame;//Frame
16
+
17
+
18
+
19
+ JPanel Panel;//Panel
20
+
21
+
22
+
23
+ JLabel Player;//プレイヤーの画像が入ったJLabel
24
+
25
+ JLabel Block;//ブロックの画像が入ったJLabel
26
+
27
+
28
+
29
+ ImageIcon PlayerIcon;//プレイヤーの画像読み込み
30
+
31
+ ImageIcon BlockIcon;//ブロックの画像読み込み
32
+
33
+
34
+
35
+ Timer timer;
36
+
37
+
38
+
39
+ int PlayerX = 20;//プレイヤーのX座標
40
+
41
+ int PlayerY = 40;//プレイヤーのY座標
42
+
43
+
44
+
45
+ int BlockX = 0;//ブロックのX座標
46
+
47
+ int BlockY = 442;//ブロックのY座標
48
+
49
+
50
+
51
+ static boolean Jflag = false;//ジャンプ判定フラグ
52
+
53
+ static boolean UKflag = false;//ジャンプキー判定フラグ
54
+
55
+ static boolean LKflag = false;//左キー判定フラグ
56
+
57
+ static boolean RKflag = false;//右キー判定フラグ
58
+
59
+ static boolean Dflag = false;//デバック許可判定フラグ
60
+
61
+
62
+
63
+ static boolean f = true;//TESTフラグ
64
+
65
+ int start = 0;
66
+
67
+
68
+
69
+ public static void main(String args[]){
70
+
71
+
72
+
73
+ Frame = new action();
74
+
75
+
76
+
77
+ Frame.setSize(900, 500);//ウィンドウのサイズ設定(X, Y)
78
+
79
+ Frame.setTitle("アクションゲーム");//ウィンドウのタイトル
80
+
81
+ Frame.setLocationRelativeTo(null);//ウィンドウを画面の中央に表示する
82
+
83
+ Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//xで閉じるようにする
84
+
85
+ Frame.setVisible(true);//ウィンドウを可視化
86
+
87
+
88
+
89
+ }
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+ action(){
98
+
99
+
100
+
101
+ timer = new Timer(10, this);//タイマー間隔設定
102
+
103
+
104
+
105
+ Panel = new JPanel();
106
+
107
+ Panel.setLayout(null);//レイアウトマネージャー無効化
108
+
109
+
110
+
111
+ PlayerIcon = new ImageIcon("./PlayerIcon.png");//プレイヤーの画像指定
112
+
113
+ BlockIcon = new ImageIcon("./BlockIcon.png");//ブロックの画像指定
114
+
115
+
116
+
117
+ Player = new JLabel(PlayerIcon);//プレイヤー画像設定
118
+
119
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標とサイズ設定
120
+
121
+
122
+
123
+ Block = new JLabel(BlockIcon);//ブロック画像設定
124
+
125
+ Block.setBounds(BlockX, BlockY, 20, 20);//ブロックの座標とサイズ設定
126
+
127
+
128
+
129
+ Panel.add(Player);//プレイヤー画像追加
130
+
131
+ Panel.add(Block);//ブロック画像追加
132
+
133
+
134
+
135
+ Container contentPane = getContentPane();//???
136
+
137
+ contentPane.add(Panel, BorderLayout.CENTER);//???
138
+
139
+
140
+
141
+ timer.start();//タイマースタート
142
+
143
+
144
+
145
+ while(f == true){
146
+
147
+
148
+
149
+ start += 1;
150
+
151
+
152
+
153
+ System.out.println(start);
154
+
155
+
156
+
157
+ if(start >= 1000){
158
+
159
+
160
+
161
+ break;
162
+
163
+
164
+
165
+ }
166
+
167
+
168
+
169
+ }
170
+
171
+
172
+
173
+ }
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ public void actionPerformed(ActionEvent e){//タイマーの処理
182
+
183
+
184
+
185
+ if(PlayerY < 399 && UKflag == false){//空中判定処理
186
+
187
+
188
+
189
+ Jflag = true;//ジャンプフラグをtrueにする
190
+
191
+
192
+
193
+ }else if(PlayerY >= 399){
194
+
195
+
196
+
197
+ Jflag = false;//ジャンプフラグをfalseにする
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ if(Jflag == true){//重力処理
206
+
207
+
208
+
209
+ PlayerY += 6;//プレイヤーのY座標を+3
210
+
211
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定
212
+
213
+ Panel.repaint();//パネルを更新
214
+
215
+
216
+
217
+ }
218
+
219
+
220
+
221
+ if(UKflag == true){//ジャンプ処理
222
+
223
+
224
+
225
+ PlayerY -= 40;//プレイヤーのY座標を-40
226
+
227
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定
228
+
229
+ Panel.repaint();//パネルを更新
230
+
231
+
232
+
233
+ if(PlayerY <= 349){//もしPlayerYが359以下なら
234
+
235
+
236
+
237
+ Jflag = true;//ジャンプフラグをtrueにする
238
+
239
+ UKflag = false;//ジャンプキー判定フラグをfalseにする
240
+
241
+
242
+
243
+ }
244
+
245
+
246
+
247
+ }
248
+
249
+
250
+
251
+ if(LKflag == true){//左移動処理
252
+
253
+
254
+
255
+ PlayerX -= 4;//プレイヤーのX座標を-4
256
+
257
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定
258
+
259
+ Panel.repaint();//パネルを更新
260
+
261
+
262
+
263
+ if(PlayerX < 0){//もしPlayerXが0未満なら
264
+
265
+
266
+
267
+ PlayerX = 0;//プレイヤーのX座標を0に設定
268
+
269
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定
270
+
271
+ Panel.repaint();//パネルを更新
272
+
273
+
274
+
275
+ }
276
+
277
+
278
+
279
+ LKflag = false;//左キー判定フラグをfalseにする
280
+
281
+
282
+
283
+ }
284
+
285
+
286
+
287
+ if(RKflag == true){//右移動処理
288
+
289
+
290
+
291
+ PlayerX += 4;//プレイヤーのX座標を+4
292
+
293
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定
294
+
295
+ Panel.repaint();//パネルを更新
296
+
297
+
298
+
299
+ if(PlayerX >= 864){//もしPlayerXが864以上なら
300
+
301
+
302
+
303
+ PlayerX = 864;//プレイヤーのX座標を864に設定
304
+
305
+ Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定
306
+
307
+ Panel.repaint();//パネルを更新
308
+
309
+
310
+
311
+ }
312
+
313
+
314
+
315
+ RKflag = false;//右キー判定フラグをfalseにする
316
+
317
+
318
+
319
+ }
320
+
321
+
322
+
323
+ }
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+ protected void processKeyEvent(KeyEvent e){//キー入力の処理
332
+
333
+
334
+
335
+ if(e.getID() == KeyEvent.KEY_PRESSED){
336
+
337
+
338
+
339
+ if(e.getKeyCode() == KeyEvent.VK_UP && Jflag == false){//ジャンプキー判定
340
+
341
+
342
+
343
+ UKflag = true;//ジャンプキー判定フラグをfalseにする
344
+
345
+
346
+
347
+ }
348
+
349
+
350
+
351
+ if(e.getKeyCode() == KeyEvent.VK_LEFT){//左キー判定
352
+
353
+
354
+
355
+ LKflag = true;//左キー判定フラグをfalseにする
356
+
357
+
358
+
359
+ }
360
+
361
+
362
+
363
+ if(e.getKeyCode() == KeyEvent.VK_RIGHT){//右キー判定
364
+
365
+
366
+
367
+ RKflag = true;//右キー判定フラグをfalseにする
368
+
369
+
370
+
371
+ }
372
+
373
+
374
+
375
+ if(e.getKeyCode() == KeyEvent.VK_D && Dflag == true){//デバックキー判定
376
+
377
+
378
+
379
+ System.out.println("\nJflag:" + Jflag);
380
+
381
+ System.out.println("UKflag:" + UKflag);
382
+
383
+ System.out.println("LKflag:" + LKflag);
384
+
385
+ System.out.println("RKflag:" + RKflag);
386
+
387
+ System.out.println("Dflag:" + Dflag);
388
+
389
+ System.out.println("PlayerX座標:" + PlayerX);
390
+
391
+ System.out.println("PlayerY座標:" + PlayerY + "\n");
392
+
393
+
394
+
395
+ }
396
+
397
+
398
+
399
+ if(e.getKeyCode() == KeyEvent.VK_S && Dflag == false){//デバック許可キー判定
400
+
401
+
402
+
403
+ Dflag = true;//デバック許可キー判定フラグをtrueにする
404
+
405
+ System.out.println("デバック機能を有効にしました");//デバック許可メッセージ出力
406
+
407
+
408
+
409
+ }
410
+
411
+
412
+
413
+ if(e.getKeyCode() == KeyEvent.VK_E && Dflag == true){//終了キー判定
414
+
415
+
416
+
417
+ Frame.dispose();//Frame削除
418
+
419
+ System.out.println("正常に終了しました");//終了メッセージ表示
420
+
421
+
422
+
423
+ }
424
+
425
+
426
+
427
+ }
428
+
429
+
430
+
431
+ }
432
+
433
+
434
+
435
+ }
14
436
 
15
437
 
16
438
 

1

コードの追記

2017/11/25 18:07

投稿

Alpa
Alpa

スコア80

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,20 @@
1
+ ```java
2
+
3
+ JPanel panel = new JPanel();
4
+
5
+
6
+
1
7
  ImageIcon icon = new ImageIcon("./xxx");
2
8
 
3
9
  Jlabel block = new JLabel(icon);
10
+
11
+
12
+
13
+ panel.add(block);
14
+
15
+
16
+
17
+ ```
4
18
 
5
19
 
6
20