質問編集履歴
3
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,17 +38,17 @@
|
|
38
38
|
|
39
39
|
class App:
|
40
40
|
|
41
|
-
def init(self):
|
41
|
+
def __init__(self):
|
42
42
|
|
43
43
|
pyxel.init(160, 120, caption="Invadors game",
|
44
44
|
|
45
45
|
palette=[1],quit_key=pyxel.KEY_NONE)
|
46
46
|
|
47
|
-
|
48
|
-
|
49
47
|
#Player
|
50
48
|
|
49
|
+
class player:
|
50
|
+
|
51
|
-
def init(self):
|
51
|
+
def __init__(self):
|
52
52
|
|
53
53
|
pyxel.image(0).load(0, 0, "./player.png")
|
54
54
|
|
@@ -56,9 +56,9 @@
|
|
56
56
|
|
57
57
|
playerX_change = 0
|
58
58
|
|
59
|
-
|
59
|
+
class sound:
|
60
|
-
|
60
|
+
|
61
|
-
def init(self):
|
61
|
+
def __init__(self):
|
62
62
|
|
63
63
|
self.music_player = MusicPlayer('laser.wav')
|
64
64
|
|
@@ -66,9 +66,11 @@
|
|
66
66
|
|
67
67
|
#kadai
|
68
68
|
|
69
|
+
class kadai:
|
70
|
+
|
69
|
-
def init(self):
|
71
|
+
def __init__(self):
|
70
|
-
|
72
|
+
|
71
|
-
|
73
|
+
pyxel.image(1).load(0, 0, "./kadai.png")
|
72
74
|
|
73
75
|
kadaiX = random.randint(0, 736)
|
74
76
|
|
@@ -80,9 +82,11 @@
|
|
80
82
|
|
81
83
|
#Bullet
|
82
84
|
|
85
|
+
class Bullet:
|
86
|
+
|
83
|
-
def ini(self):
|
87
|
+
def __init__(self):
|
84
|
-
|
88
|
+
|
85
|
-
|
89
|
+
pyxel.image(2).load(0, 0, "./bullet.png")
|
86
90
|
|
87
91
|
bulletX, bulletY = 0, 480
|
88
92
|
|
@@ -96,19 +100,315 @@
|
|
96
100
|
|
97
101
|
#Score
|
98
102
|
|
103
|
+
class score:
|
104
|
+
|
105
|
+
score_value = 0
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def player(x, y):
|
110
|
+
|
111
|
+
screen.blit(playerImg, (x, y))
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
def kadai(x, y):
|
116
|
+
|
117
|
+
screen.blit(kadaiImg, (x, y))
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def fire_bullet(x, y):
|
122
|
+
|
123
|
+
global bullet_state
|
124
|
+
|
125
|
+
bullet_state = 'fire'
|
126
|
+
|
127
|
+
screen.blit(bulletImg, (x + 16, y + 10))
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def isCollision(kadaiX, kadaiY, bulletX, bulletY):
|
132
|
+
|
133
|
+
distance = math.sqrt(math.pow(kadaiX - bulletX, 2) + math.pow(kadaiY - bulletY, 2))
|
134
|
+
|
135
|
+
if distance < 27:
|
136
|
+
|
137
|
+
return true
|
138
|
+
|
139
|
+
else:
|
140
|
+
|
141
|
+
return False
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
def __init__(self):
|
146
|
+
|
147
|
+
running = True
|
148
|
+
|
149
|
+
while running:
|
150
|
+
|
151
|
+
screen.fill((0, 0, 0))
|
152
|
+
|
153
|
+
font = pyxel.font.SysFont(None, 80)
|
154
|
+
|
155
|
+
message = font.render('Hello World', False, (255, 255, 255))
|
156
|
+
|
157
|
+
screen.blit(message, (20,50))
|
158
|
+
|
159
|
+
for event in pyxel.event.get():
|
160
|
+
|
161
|
+
if event.type == pyxel.QUIT:
|
162
|
+
|
163
|
+
running = False
|
164
|
+
|
165
|
+
if event.type == pyxel.KEYDOWN:
|
166
|
+
|
167
|
+
if event.key == pyxel.K_LEFT:
|
168
|
+
|
169
|
+
playerX_change = -1.5
|
170
|
+
|
171
|
+
if event.key == pyxel.K_RIGHT:
|
172
|
+
|
173
|
+
playerX_change = 1.5
|
174
|
+
|
175
|
+
if event.key == pyxel.K_SPACE:
|
176
|
+
|
177
|
+
if bullet_state == 'ready':
|
178
|
+
|
179
|
+
bulletX = playerX
|
180
|
+
|
181
|
+
fire_bullet(bulletX, bulletY)
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
if event.type == pyxel.KEYUP:
|
186
|
+
|
187
|
+
if event.key == pyxel.K_LEFT or event.key == pyxel.K_RIGHT:
|
188
|
+
|
189
|
+
playerX_change = 0
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
#Player
|
194
|
+
|
195
|
+
def __init__(player):
|
196
|
+
|
197
|
+
playerX += playerX_change
|
198
|
+
|
199
|
+
if playerX <= 0:
|
200
|
+
|
201
|
+
playerX = 0
|
202
|
+
|
203
|
+
elif playerX >= 736:
|
204
|
+
|
205
|
+
playerX = 736
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
#kadai
|
210
|
+
|
211
|
+
if kadai > 440:
|
212
|
+
|
213
|
+
pass
|
214
|
+
|
215
|
+
kadaiX += kadaiX_change
|
216
|
+
|
217
|
+
if kadaiX <= 0:
|
218
|
+
|
219
|
+
kadaiX_change = 4 #go right if come to the leftend
|
220
|
+
|
221
|
+
kadaiY += kadaiY_change
|
222
|
+
|
223
|
+
elif kadaiX >= 736:
|
224
|
+
|
225
|
+
kadaiX_change = -4 #go left if come to the rightend
|
226
|
+
|
227
|
+
kadaiY += kadaiY_change
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
collision = isCollision(kadaiX, kadaiY, bulletX, bulletY)
|
232
|
+
|
233
|
+
if collosion:
|
234
|
+
|
235
|
+
bulletY = 480
|
236
|
+
|
237
|
+
bullet_state = 'ready'
|
238
|
+
|
239
|
+
score_value += 1
|
240
|
+
|
241
|
+
kadaiX = random.randint(0, 736)
|
242
|
+
|
243
|
+
kadaiY = random.randint(50, 150)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
#bullet Movement
|
248
|
+
|
249
|
+
if bulletY <=0:
|
250
|
+
|
251
|
+
bulletY = 480
|
252
|
+
|
253
|
+
bullet_state = 'ready'
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
if bullet_state == 'fire':
|
258
|
+
|
259
|
+
fire_bullet(bulletX, bulletY)
|
260
|
+
|
261
|
+
bulletY -= bulletY_change
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
#Score
|
270
|
+
|
271
|
+
font = pyxel.font.SysFont(None, 32) #creating fonts 'None' is default
|
272
|
+
|
273
|
+
score = font.render("Score: {str(score_value)}", True, (255,255,255))
|
274
|
+
|
275
|
+
screen.blit(score, (20,50))
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
player(playerX, playerY)
|
282
|
+
|
283
|
+
kadai(kadaiX, kadaiY)
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
pyxel.display.update()
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
App()
|
292
|
+
|
293
|
+
```
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
### 試したこと
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
ここに問題に対して試したことを記載してください。
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
### 補足情報(FW/ツールのバージョンなど)
|
306
|
+
|
307
|
+
最終目標としては、Pygameで書かれたコードを同じ動作をするようにpyxelで書き換えることです。
|
308
|
+
|
309
|
+
```Pygame
|
310
|
+
|
311
|
+
import pygame
|
312
|
+
|
313
|
+
from pygame import mixer
|
314
|
+
|
315
|
+
import random
|
316
|
+
|
317
|
+
import math
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
pygame.init()
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
screen = pygame.display.set_mode((800, 600))
|
330
|
+
|
331
|
+
screen.fill((150,150,150))
|
332
|
+
|
333
|
+
pygame.display.set_caption('Invaders game')
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
#Player
|
340
|
+
|
341
|
+
playerimg = pygame.image.load('player.png')
|
342
|
+
|
343
|
+
playerX,playerY = 370, 480
|
344
|
+
|
345
|
+
playerX_change = 0
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
#mixer.Sound('laser.wav').play()
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
#Enemy
|
358
|
+
|
359
|
+
enemyImg = pygame.image.load('enemy.png')
|
360
|
+
|
361
|
+
enemyX = random.randint(0, 736)
|
362
|
+
|
363
|
+
enemyY = random.randint(50, 150)
|
364
|
+
|
365
|
+
enemyX_change, enemyY_change = 4, 40
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
#Bullet
|
372
|
+
|
373
|
+
bulletImg = pygame.image.load('bullet.png')
|
374
|
+
|
375
|
+
bulletX, bulletY = 0, 480
|
376
|
+
|
377
|
+
bulletX_change, bulletY_change = 0, 3
|
378
|
+
|
379
|
+
bullet_state = 'ready'
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
#Score
|
394
|
+
|
99
395
|
score_value = 0
|
100
396
|
|
101
397
|
|
102
398
|
|
399
|
+
|
400
|
+
|
103
401
|
def player(x, y):
|
104
402
|
|
105
403
|
screen.blit(playerImg, (x, y))
|
106
404
|
|
107
405
|
|
108
406
|
|
407
|
+
|
408
|
+
|
109
|
-
def
|
409
|
+
def enemy(x, y):
|
110
|
-
|
410
|
+
|
111
|
-
screen.blit(
|
411
|
+
screen.blit(enemyImg, (x, y))
|
112
412
|
|
113
413
|
|
114
414
|
|
@@ -122,9 +422,9 @@
|
|
122
422
|
|
123
423
|
|
124
424
|
|
125
|
-
def isCollision(
|
425
|
+
def isCollision(enemyX, enemyY, bulletX, bulletY):
|
126
|
-
|
426
|
+
|
127
|
-
distance = math.sqrt(math.pow(
|
427
|
+
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + math.pow(enemyY - bulletY, 2))
|
128
428
|
|
129
429
|
if distance < 27:
|
130
430
|
|
@@ -136,49 +436,49 @@
|
|
136
436
|
|
137
437
|
|
138
438
|
|
139
|
-
|
439
|
+
|
140
|
-
|
440
|
+
|
141
|
-
|
441
|
+
running = True
|
142
|
-
|
442
|
+
|
143
|
-
|
443
|
+
while running:
|
144
|
-
|
444
|
+
|
145
|
-
|
445
|
+
screen.fill((0, 0, 0))
|
146
|
-
|
446
|
+
|
147
|
-
font = py
|
447
|
+
#font = pygame.font.SysFont(None, 80)
|
148
|
-
|
448
|
+
|
149
|
-
message = font.render('Hello World', False, (255, 255, 255))
|
449
|
+
#message = font.render('Hello World', False, (255, 255, 255))
|
150
|
-
|
450
|
+
|
151
|
-
screen.blit(message, (20,50))
|
451
|
+
#screen.blit(message, (20,50))
|
152
|
-
|
452
|
+
|
153
|
-
|
453
|
+
for event in pygame.event.get():
|
154
|
-
|
454
|
+
|
155
|
-
if event.type == py
|
455
|
+
if event.type == pygame.QUIT:
|
156
456
|
|
157
457
|
running = False
|
158
458
|
|
159
|
-
|
459
|
+
if event.type == pygame.KEYDOWN:
|
160
|
-
|
460
|
+
|
161
|
-
|
461
|
+
if event.key == pygame.K_LEFT:
|
162
|
-
|
462
|
+
|
163
|
-
|
463
|
+
playerX_change = -1.5
|
164
|
-
|
464
|
+
|
165
|
-
|
465
|
+
if event.key == pygame.K_RIGHT:
|
166
|
-
|
466
|
+
|
167
|
-
|
467
|
+
playerX_change = 1.5
|
168
|
-
|
468
|
+
|
169
|
-
|
469
|
+
if event.key == pygame.K_SPACE:
|
170
|
-
|
470
|
+
|
171
|
-
|
471
|
+
if bullet_state is 'ready':
|
172
|
-
|
472
|
+
|
173
|
-
|
473
|
+
bulletX = playerX
|
174
|
-
|
474
|
+
|
175
|
-
|
475
|
+
fire_bullet(bulletX, bulletY)
|
176
476
|
|
177
477
|
|
178
478
|
|
179
|
-
|
479
|
+
if event.type == pygame.KEYUP:
|
180
|
-
|
480
|
+
|
181
|
-
|
481
|
+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
|
182
482
|
|
183
483
|
playerX_change = 0
|
184
484
|
|
@@ -186,8 +486,6 @@
|
|
186
486
|
|
187
487
|
#Player
|
188
488
|
|
189
|
-
def init(player):
|
190
|
-
|
191
489
|
playerX += playerX_change
|
192
490
|
|
193
491
|
if playerX <= 0:
|
@@ -196,33 +494,33 @@
|
|
196
494
|
|
197
495
|
elif playerX >= 736:
|
198
496
|
|
199
|
-
playerX = 736
|
497
|
+
playerX = 736
|
200
498
|
|
201
499
|
|
202
500
|
|
203
|
-
#
|
501
|
+
#Enemy
|
204
|
-
|
502
|
+
|
205
|
-
if
|
503
|
+
if enemy > 440:
|
206
|
-
|
504
|
+
|
207
|
-
|
505
|
+
break
|
208
|
-
|
506
|
+
|
209
|
-
|
507
|
+
enemyX += enemyX_change
|
210
|
-
|
508
|
+
|
211
|
-
if
|
509
|
+
if enemyX <= 0:
|
212
|
-
|
510
|
+
|
213
|
-
|
511
|
+
enemyX_change = 4 #go right if come to the leftend
|
214
|
-
|
512
|
+
|
215
|
-
|
513
|
+
enemyY += enemyY_change
|
216
|
-
|
514
|
+
|
217
|
-
elif
|
515
|
+
elif enemyX >= 736:
|
218
|
-
|
516
|
+
|
219
|
-
|
517
|
+
enemyX_change = -4 #go left if come to the rightend
|
220
|
-
|
518
|
+
|
221
|
-
|
519
|
+
enemyY += enemyY_change
|
222
|
-
|
223
|
-
|
224
|
-
|
520
|
+
|
521
|
+
|
522
|
+
|
225
|
-
collision = isCollision(
|
523
|
+
collision = isCollision(enemyX, enemyY, bulletX, bulletY)
|
226
524
|
|
227
525
|
if collosion:
|
228
526
|
|
@@ -232,9 +530,9 @@
|
|
232
530
|
|
233
531
|
score_value += 1
|
234
532
|
|
235
|
-
|
533
|
+
enemyX = random.randint(0, 736)
|
236
|
-
|
534
|
+
|
237
|
-
|
535
|
+
enemyY = random.randint(50, 150)
|
238
536
|
|
239
537
|
|
240
538
|
|
@@ -248,7 +546,7 @@
|
|
248
546
|
|
249
547
|
|
250
548
|
|
251
|
-
if bullet_state
|
549
|
+
if bullet_state is 'fire':
|
252
550
|
|
253
551
|
fire_bullet(bulletX, bulletY)
|
254
552
|
|
@@ -262,7 +560,7 @@
|
|
262
560
|
|
263
561
|
#Score
|
264
562
|
|
265
|
-
font = py
|
563
|
+
font = pygame.font.SysFont(None, 32) #creating fonts 'None' is default
|
266
564
|
|
267
565
|
score = font.render("Score: {str(score_value)}", True, (255,255,255))
|
268
566
|
|
@@ -274,302 +572,10 @@
|
|
274
572
|
|
275
573
|
player(playerX, playerY)
|
276
574
|
|
277
|
-
|
575
|
+
enemy(enemyX, enemyY)
|
278
|
-
|
279
|
-
|
280
|
-
|
576
|
+
|
577
|
+
|
578
|
+
|
281
|
-
py
|
579
|
+
pygame.display.update()
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
App()
|
286
580
|
|
287
581
|
```
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
### 試したこと
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
ここに問題に対して試したことを記載してください。
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
### 補足情報(FW/ツールのバージョンなど)
|
300
|
-
|
301
|
-
最終目標としては、Pygameで書かれたコードを同じ動作をするようにpyxelで書き換えることです。
|
302
|
-
|
303
|
-
```Pygame
|
304
|
-
|
305
|
-
import pygame
|
306
|
-
|
307
|
-
from pygame import mixer
|
308
|
-
|
309
|
-
import random
|
310
|
-
|
311
|
-
import math
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
pygame.init()
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
screen = pygame.display.set_mode((800, 600))
|
324
|
-
|
325
|
-
screen.fill((150,150,150))
|
326
|
-
|
327
|
-
pygame.display.set_caption('Invaders game')
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
#Player
|
334
|
-
|
335
|
-
playerimg = pygame.image.load('player.png')
|
336
|
-
|
337
|
-
playerX,playerY = 370, 480
|
338
|
-
|
339
|
-
playerX_change = 0
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
#mixer.Sound('laser.wav').play()
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
#Enemy
|
352
|
-
|
353
|
-
enemyImg = pygame.image.load('enemy.png')
|
354
|
-
|
355
|
-
enemyX = random.randint(0, 736)
|
356
|
-
|
357
|
-
enemyY = random.randint(50, 150)
|
358
|
-
|
359
|
-
enemyX_change, enemyY_change = 4, 40
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
#Bullet
|
366
|
-
|
367
|
-
bulletImg = pygame.image.load('bullet.png')
|
368
|
-
|
369
|
-
bulletX, bulletY = 0, 480
|
370
|
-
|
371
|
-
bulletX_change, bulletY_change = 0, 3
|
372
|
-
|
373
|
-
bullet_state = 'ready'
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
#Score
|
388
|
-
|
389
|
-
score_value = 0
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
def player(x, y):
|
396
|
-
|
397
|
-
screen.blit(playerImg, (x, y))
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
def enemy(x, y):
|
404
|
-
|
405
|
-
screen.blit(enemyImg, (x, y))
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
def fire_bullet(x, y):
|
410
|
-
|
411
|
-
global bullet_state
|
412
|
-
|
413
|
-
bullet_state = 'fire'
|
414
|
-
|
415
|
-
screen.blit(bulletImg, (x + 16, y + 10))
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
def isCollision(enemyX, enemyY, bulletX, bulletY):
|
420
|
-
|
421
|
-
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + math.pow(enemyY - bulletY, 2))
|
422
|
-
|
423
|
-
if distance < 27:
|
424
|
-
|
425
|
-
return true
|
426
|
-
|
427
|
-
else:
|
428
|
-
|
429
|
-
return False
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
running = True
|
436
|
-
|
437
|
-
while running:
|
438
|
-
|
439
|
-
screen.fill((0, 0, 0))
|
440
|
-
|
441
|
-
#font = pygame.font.SysFont(None, 80)
|
442
|
-
|
443
|
-
#message = font.render('Hello World', False, (255, 255, 255))
|
444
|
-
|
445
|
-
#screen.blit(message, (20,50))
|
446
|
-
|
447
|
-
for event in pygame.event.get():
|
448
|
-
|
449
|
-
if event.type == pygame.QUIT:
|
450
|
-
|
451
|
-
running = False
|
452
|
-
|
453
|
-
if event.type == pygame.KEYDOWN:
|
454
|
-
|
455
|
-
if event.key == pygame.K_LEFT:
|
456
|
-
|
457
|
-
playerX_change = -1.5
|
458
|
-
|
459
|
-
if event.key == pygame.K_RIGHT:
|
460
|
-
|
461
|
-
playerX_change = 1.5
|
462
|
-
|
463
|
-
if event.key == pygame.K_SPACE:
|
464
|
-
|
465
|
-
if bullet_state is 'ready':
|
466
|
-
|
467
|
-
bulletX = playerX
|
468
|
-
|
469
|
-
fire_bullet(bulletX, bulletY)
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
if event.type == pygame.KEYUP:
|
474
|
-
|
475
|
-
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
|
476
|
-
|
477
|
-
playerX_change = 0
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
#Player
|
482
|
-
|
483
|
-
playerX += playerX_change
|
484
|
-
|
485
|
-
if playerX <= 0:
|
486
|
-
|
487
|
-
playerX = 0
|
488
|
-
|
489
|
-
elif playerX >= 736:
|
490
|
-
|
491
|
-
playerX = 736
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
#Enemy
|
496
|
-
|
497
|
-
if enemy > 440:
|
498
|
-
|
499
|
-
break
|
500
|
-
|
501
|
-
enemyX += enemyX_change
|
502
|
-
|
503
|
-
if enemyX <= 0:
|
504
|
-
|
505
|
-
enemyX_change = 4 #go right if come to the leftend
|
506
|
-
|
507
|
-
enemyY += enemyY_change
|
508
|
-
|
509
|
-
elif enemyX >= 736:
|
510
|
-
|
511
|
-
enemyX_change = -4 #go left if come to the rightend
|
512
|
-
|
513
|
-
enemyY += enemyY_change
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
collision = isCollision(enemyX, enemyY, bulletX, bulletY)
|
518
|
-
|
519
|
-
if collosion:
|
520
|
-
|
521
|
-
bulletY = 480
|
522
|
-
|
523
|
-
bullet_state = 'ready'
|
524
|
-
|
525
|
-
score_value += 1
|
526
|
-
|
527
|
-
enemyX = random.randint(0, 736)
|
528
|
-
|
529
|
-
enemyY = random.randint(50, 150)
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
#bullet Movement
|
534
|
-
|
535
|
-
if bulletY <=0:
|
536
|
-
|
537
|
-
bulletY = 480
|
538
|
-
|
539
|
-
bullet_state = 'ready'
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
if bullet_state is 'fire':
|
544
|
-
|
545
|
-
fire_bullet(bulletX, bulletY)
|
546
|
-
|
547
|
-
bulletY -= bulletY_change
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
#Score
|
556
|
-
|
557
|
-
font = pygame.font.SysFont(None, 32) #creating fonts 'None' is default
|
558
|
-
|
559
|
-
score = font.render("Score: {str(score_value)}", True, (255,255,255))
|
560
|
-
|
561
|
-
screen.blit(score, (20,50))
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
player(playerX, playerY)
|
568
|
-
|
569
|
-
enemy(enemyX, enemyY)
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
pygame.display.update()
|
574
|
-
|
575
|
-
```
|
2
コードの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -297,3 +297,279 @@
|
|
297
297
|
|
298
298
|
|
299
299
|
### 補足情報(FW/ツールのバージョンなど)
|
300
|
+
|
301
|
+
最終目標としては、Pygameで書かれたコードを同じ動作をするようにpyxelで書き換えることです。
|
302
|
+
|
303
|
+
```Pygame
|
304
|
+
|
305
|
+
import pygame
|
306
|
+
|
307
|
+
from pygame import mixer
|
308
|
+
|
309
|
+
import random
|
310
|
+
|
311
|
+
import math
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
pygame.init()
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
screen = pygame.display.set_mode((800, 600))
|
324
|
+
|
325
|
+
screen.fill((150,150,150))
|
326
|
+
|
327
|
+
pygame.display.set_caption('Invaders game')
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
#Player
|
334
|
+
|
335
|
+
playerimg = pygame.image.load('player.png')
|
336
|
+
|
337
|
+
playerX,playerY = 370, 480
|
338
|
+
|
339
|
+
playerX_change = 0
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
#mixer.Sound('laser.wav').play()
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
#Enemy
|
352
|
+
|
353
|
+
enemyImg = pygame.image.load('enemy.png')
|
354
|
+
|
355
|
+
enemyX = random.randint(0, 736)
|
356
|
+
|
357
|
+
enemyY = random.randint(50, 150)
|
358
|
+
|
359
|
+
enemyX_change, enemyY_change = 4, 40
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
#Bullet
|
366
|
+
|
367
|
+
bulletImg = pygame.image.load('bullet.png')
|
368
|
+
|
369
|
+
bulletX, bulletY = 0, 480
|
370
|
+
|
371
|
+
bulletX_change, bulletY_change = 0, 3
|
372
|
+
|
373
|
+
bullet_state = 'ready'
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
#Score
|
388
|
+
|
389
|
+
score_value = 0
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
def player(x, y):
|
396
|
+
|
397
|
+
screen.blit(playerImg, (x, y))
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
def enemy(x, y):
|
404
|
+
|
405
|
+
screen.blit(enemyImg, (x, y))
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
def fire_bullet(x, y):
|
410
|
+
|
411
|
+
global bullet_state
|
412
|
+
|
413
|
+
bullet_state = 'fire'
|
414
|
+
|
415
|
+
screen.blit(bulletImg, (x + 16, y + 10))
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
def isCollision(enemyX, enemyY, bulletX, bulletY):
|
420
|
+
|
421
|
+
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + math.pow(enemyY - bulletY, 2))
|
422
|
+
|
423
|
+
if distance < 27:
|
424
|
+
|
425
|
+
return true
|
426
|
+
|
427
|
+
else:
|
428
|
+
|
429
|
+
return False
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
running = True
|
436
|
+
|
437
|
+
while running:
|
438
|
+
|
439
|
+
screen.fill((0, 0, 0))
|
440
|
+
|
441
|
+
#font = pygame.font.SysFont(None, 80)
|
442
|
+
|
443
|
+
#message = font.render('Hello World', False, (255, 255, 255))
|
444
|
+
|
445
|
+
#screen.blit(message, (20,50))
|
446
|
+
|
447
|
+
for event in pygame.event.get():
|
448
|
+
|
449
|
+
if event.type == pygame.QUIT:
|
450
|
+
|
451
|
+
running = False
|
452
|
+
|
453
|
+
if event.type == pygame.KEYDOWN:
|
454
|
+
|
455
|
+
if event.key == pygame.K_LEFT:
|
456
|
+
|
457
|
+
playerX_change = -1.5
|
458
|
+
|
459
|
+
if event.key == pygame.K_RIGHT:
|
460
|
+
|
461
|
+
playerX_change = 1.5
|
462
|
+
|
463
|
+
if event.key == pygame.K_SPACE:
|
464
|
+
|
465
|
+
if bullet_state is 'ready':
|
466
|
+
|
467
|
+
bulletX = playerX
|
468
|
+
|
469
|
+
fire_bullet(bulletX, bulletY)
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
if event.type == pygame.KEYUP:
|
474
|
+
|
475
|
+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
|
476
|
+
|
477
|
+
playerX_change = 0
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
#Player
|
482
|
+
|
483
|
+
playerX += playerX_change
|
484
|
+
|
485
|
+
if playerX <= 0:
|
486
|
+
|
487
|
+
playerX = 0
|
488
|
+
|
489
|
+
elif playerX >= 736:
|
490
|
+
|
491
|
+
playerX = 736
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
#Enemy
|
496
|
+
|
497
|
+
if enemy > 440:
|
498
|
+
|
499
|
+
break
|
500
|
+
|
501
|
+
enemyX += enemyX_change
|
502
|
+
|
503
|
+
if enemyX <= 0:
|
504
|
+
|
505
|
+
enemyX_change = 4 #go right if come to the leftend
|
506
|
+
|
507
|
+
enemyY += enemyY_change
|
508
|
+
|
509
|
+
elif enemyX >= 736:
|
510
|
+
|
511
|
+
enemyX_change = -4 #go left if come to the rightend
|
512
|
+
|
513
|
+
enemyY += enemyY_change
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
collision = isCollision(enemyX, enemyY, bulletX, bulletY)
|
518
|
+
|
519
|
+
if collosion:
|
520
|
+
|
521
|
+
bulletY = 480
|
522
|
+
|
523
|
+
bullet_state = 'ready'
|
524
|
+
|
525
|
+
score_value += 1
|
526
|
+
|
527
|
+
enemyX = random.randint(0, 736)
|
528
|
+
|
529
|
+
enemyY = random.randint(50, 150)
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
#bullet Movement
|
534
|
+
|
535
|
+
if bulletY <=0:
|
536
|
+
|
537
|
+
bulletY = 480
|
538
|
+
|
539
|
+
bullet_state = 'ready'
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
if bullet_state is 'fire':
|
544
|
+
|
545
|
+
fire_bullet(bulletX, bulletY)
|
546
|
+
|
547
|
+
bulletY -= bulletY_change
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
#Score
|
556
|
+
|
557
|
+
font = pygame.font.SysFont(None, 32) #creating fonts 'None' is default
|
558
|
+
|
559
|
+
score = font.render("Score: {str(score_value)}", True, (255,255,255))
|
560
|
+
|
561
|
+
screen.blit(score, (20,50))
|
562
|
+
|
563
|
+
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
player(playerX, playerY)
|
568
|
+
|
569
|
+
enemy(enemyX, enemyY)
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
pygame.display.update()
|
574
|
+
|
575
|
+
```
|
1
内容の改善、補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
pyxelでインベーダーゲームを作っています。
|
4
|
-
|
4
|
+
|
5
|
+
|
6
|
+
|
5
|
-
実行しても何も起こらなくなってしまいました。アドバイスお願いします。
|
7
|
+
下のエラーメッセージを改善しようと書き換えたところ、実行しても何も起こらなくなってしまいました。アドバイスお願いします。
|
6
8
|
|
7
9
|
|
8
10
|
|
@@ -12,7 +14,13 @@
|
|
12
14
|
|
13
15
|
```
|
14
16
|
|
17
|
+
File "/Users/higashirinako/Downloads/pyxel_me/a.py", line 78
|
18
|
+
|
15
|
-
|
19
|
+
playerX += playerX_change
|
20
|
+
|
21
|
+
^
|
22
|
+
|
23
|
+
IndentationError: unindent does not match any outer indentation level
|
16
24
|
|
17
25
|
```
|
18
26
|
|
@@ -289,7 +297,3 @@
|
|
289
297
|
|
290
298
|
|
291
299
|
### 補足情報(FW/ツールのバージョンなど)
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
ここにより詳細な情報を記載してください。
|