質問編集履歴
1
加筆
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Python】ブロック崩しゲームがスタートし
|
1
|
+
【Python】pythonでブロック崩しゲームを作成しようとしています。しかし、ゲームがスタートしません。ご教授願います。
|
test
CHANGED
@@ -1,16 +1,28 @@
|
|
1
|
-
###
|
1
|
+
### ゲーム概要
|
2
|
-
|
3
|
-
|
2
|
+
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
・spaceキー押下でゲーム開始
|
8
|
-
|
4
|
+
|
9
|
-
|
5
|
+
・ゲーム開始まで「Press 'SPACE' to start」とscoreを表示
|
6
|
+
|
10
|
-
|
7
|
+
・ランダムで矢が上から降る。その矢がパドルに当たるとゲームオーバー
|
8
|
+
|
9
|
+
・ブロックにはbc(破壊するためにボールを当てる回数)を設定
|
10
|
+
|
11
|
+
・ブロックにボールが当たるとスコアが加算
|
12
|
+
|
13
|
+
・ブロックの上下左右にボールが当たる。
|
14
|
+
|
15
|
+
・パドルにはMULTI_BALL_COUNTという定数で定めた回数ごとに一個ボールを発生させる。ボールの数がBALL_MAX_NUMに達している場合には発生しない。
|
16
|
+
|
11
|
-
|
17
|
+
・最後のボールが底につくとゲームオーバー
|
18
|
+
|
12
|
-
|
19
|
+
・パドルはボールが一回当たるごとに色が変化。
|
20
|
+
|
21
|
+
・全ブロックの破壊でClear!と表示
|
22
|
+
|
23
|
+
|
24
|
+
|
13
|
-
###
|
25
|
+
###ソースコード
|
14
26
|
|
15
27
|
```
|
16
28
|
|
@@ -198,460 +210,456 @@
|
|
198
210
|
|
199
211
|
|
200
212
|
|
213
|
+
class Block(MovingObject):
|
214
|
+
|
215
|
+
def __init__(self,id,x,y,w,h,bc,c):
|
216
|
+
|
217
|
+
super().__init__(id,x,y,w,h,0,0)
|
218
|
+
|
219
|
+
self.bc=bc
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
def delete(self):
|
224
|
+
|
225
|
+
canvas.delete(self.id)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
class Spear(MovingObject):
|
230
|
+
|
231
|
+
def __init__(self,id,x,y,w,h,vy,c):
|
232
|
+
|
233
|
+
super().__init__(id,x,y,w,h,0,vy)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
def delete(self):
|
238
|
+
|
239
|
+
canvas.delete(spear.id)
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
@dataclass
|
244
|
+
|
245
|
+
class Game:
|
246
|
+
|
247
|
+
start:int
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
def game_end(self,message):
|
252
|
+
|
253
|
+
self.run=False
|
254
|
+
|
255
|
+
canvas.create_text(BOX_CENTER,MESSAGE_Y,text=messge,
|
256
|
+
|
257
|
+
font=('FixedSys',16))
|
258
|
+
|
259
|
+
tk.update()
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
@dataclass
|
264
|
+
|
265
|
+
class Box:
|
266
|
+
|
267
|
+
west:int
|
268
|
+
|
269
|
+
north:int
|
270
|
+
|
271
|
+
east:int
|
272
|
+
|
273
|
+
south:int
|
274
|
+
|
275
|
+
balls:list
|
276
|
+
|
277
|
+
paddle:Paddle
|
278
|
+
|
279
|
+
blocks:list
|
280
|
+
|
281
|
+
paddle_v:int
|
282
|
+
|
283
|
+
duration:float
|
284
|
+
|
285
|
+
run:int
|
286
|
+
|
287
|
+
score:int
|
288
|
+
|
289
|
+
paddle_count:int
|
290
|
+
|
291
|
+
spear:Spear
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
def __init__(self,x,y,w,h,duration):
|
296
|
+
|
297
|
+
self.west,self.north=(x,y)
|
298
|
+
|
299
|
+
self.east,self.south=(x+w,y+h)
|
300
|
+
|
301
|
+
self.balls=[]
|
302
|
+
|
303
|
+
self.paddle=None
|
304
|
+
|
305
|
+
self.block=None
|
306
|
+
|
307
|
+
self.spear=None
|
308
|
+
|
309
|
+
self.blocks=[]
|
310
|
+
|
311
|
+
self.duration=duration
|
312
|
+
|
313
|
+
self.paddle_v=5
|
314
|
+
|
315
|
+
self.run=False
|
316
|
+
|
317
|
+
self.score=0
|
318
|
+
|
319
|
+
self.paddle_count=0
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
def make_ball(self,x,y,d,vx,vy):
|
324
|
+
|
325
|
+
id=canvas.create_oval(x,y,x+d,y+d,
|
326
|
+
|
327
|
+
fill="black",outline="black")
|
328
|
+
|
329
|
+
return Ball(id,x,y,d,vx,vy)
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
def make_walls(self):
|
334
|
+
|
335
|
+
canvas.create_rectangle(self.west,self.north,self.east,self.south)
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
def check_wall(self,ball,game):
|
340
|
+
|
341
|
+
if ball.x+ball.vx<=self.west or ball.x+ball.d+ball.vx>=self.east:
|
342
|
+
|
343
|
+
ball.vx=-ball.vx
|
344
|
+
|
345
|
+
if ball.y+ball.vy<=self.north:
|
346
|
+
|
347
|
+
ball.vy=-ball.vy
|
348
|
+
|
349
|
+
return False
|
350
|
+
|
351
|
+
if ball.y+ball.d+ball.vy>=self.south:
|
352
|
+
|
353
|
+
return True
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
def set(self,n):
|
358
|
+
|
359
|
+
self.make_walls()
|
360
|
+
|
361
|
+
self.ball=self.make_ball(BOX_CENTER,PADDLE_Y0-50,10,BALL_VX,BALL_VY)
|
362
|
+
|
363
|
+
self.paddle=self.make_paddle(self.east/2-50,PADDLE_Y0,
|
364
|
+
|
365
|
+
100,20, "green")
|
366
|
+
|
367
|
+
for x in range(n):
|
368
|
+
|
369
|
+
block=self.make_block(BLOCK_X+(BLOCK_W+PAD)*x,BLOCK_Y,BLOCK_W,BLOCK_H,
|
370
|
+
|
371
|
+
BLOCKCOUNT,BLOCK_COLOR)
|
372
|
+
|
373
|
+
self.blocks.append(block)
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
canvas.bind_all("<KeyPress-Left>",self.left_paddle)
|
378
|
+
|
379
|
+
canvas.bind_all("<KeyPress-Right>",self.right_paddle)
|
380
|
+
|
381
|
+
canvas.bind_all("<KeyRelease-Left>",self.stop_paddle)
|
382
|
+
|
383
|
+
canvas.bind_all("<KeyRelease-Right>",self.stop_paddle)
|
384
|
+
|
385
|
+
canvas.bind_all('<KeyPress-space>',self.game_start)
|
386
|
+
|
387
|
+
|
388
|
+
|
201
389
|
|
202
390
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
def d
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
de
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
s
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
self.r
|
246
|
-
|
247
|
-
|
391
|
+
def make_paddle(self,x,y,w,h,c):
|
392
|
+
|
393
|
+
id=canvas.create_rectangle(x,y,x+w,y+h,
|
394
|
+
|
395
|
+
fill=c,outline=c)
|
396
|
+
|
397
|
+
return Paddle(id,x,y,w,h)
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
def check_paddle(self,ball,paddle):
|
402
|
+
|
403
|
+
hit=False
|
404
|
+
|
405
|
+
if(paddle.y+paddle.h>=ball.y+ball.d+ball.vy>=paddle.y
|
406
|
+
|
407
|
+
and paddle.x<=ball.x+ball.d/2+ball.vx<=paddle.x+paddle.w):
|
408
|
+
|
409
|
+
paddle.change_paddle_color(paddle,random.choice(COLORS))
|
410
|
+
|
411
|
+
ball.vy=-ball.vy
|
412
|
+
|
413
|
+
ball.vx=int(10*(ball.x+ball.d/2-paddle.x)/paddle.w)-5
|
414
|
+
|
415
|
+
elif((paddle.x+paddle.w>=ball.x+ball.d+ball.vx>=paddle.x
|
416
|
+
|
417
|
+
or paddle.x+paddle.w>=ball.x+ball.vx>=paddle.x)
|
418
|
+
|
419
|
+
and paddle.y<=ball.y+ball.d/2+ball.vy<=paddle.y+paddle.h):
|
420
|
+
|
421
|
+
paddle.change_paddle_color(paddle,random.choice(COLORS))
|
422
|
+
|
423
|
+
ball.vx=-ball.vx
|
424
|
+
|
425
|
+
if hit:
|
426
|
+
|
427
|
+
self.paddle_count+=1
|
428
|
+
|
429
|
+
if self.paddle_count%MULTI_BALL_COUNT==0:
|
430
|
+
|
431
|
+
if len(self.balls)<BALL_MAX_NUM:
|
432
|
+
|
433
|
+
ball=self.create_ball(
|
434
|
+
|
435
|
+
BALL_X0,
|
436
|
+
|
437
|
+
BALL_Y0,10,
|
438
|
+
|
439
|
+
random.choice(VX0),
|
440
|
+
|
441
|
+
BALL_VY
|
442
|
+
|
443
|
+
)
|
444
|
+
|
445
|
+
slf.balls.apeend(ball)
|
446
|
+
|
447
|
+
self.movingObjs.append(ball)
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
def left_paddle(self,event):
|
452
|
+
|
453
|
+
self.paddle.set_v(-self.paddle_v)
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
def right_paddle(self,event):
|
458
|
+
|
459
|
+
self.paddle.set_v(self.paddle_v)
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
def stop_paddle(self,event):
|
464
|
+
|
465
|
+
self.paddle.stop()
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
def game_start(self,event):
|
470
|
+
|
471
|
+
self.run=True
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
def make_block(self,x,y,w,h,bc,c):
|
476
|
+
|
477
|
+
id=canvas.create_rectangle(x,y,x+w,y+h,
|
478
|
+
|
479
|
+
fill=c,outline=c)
|
480
|
+
|
481
|
+
return Block(id,x,y,w,h,bc,c)
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
def check_blocks(self,ball,block):
|
486
|
+
|
487
|
+
if(block.y<=ball.y+ball.d/2<=block.y+block.h
|
488
|
+
|
489
|
+
and ball.x+ball.vx<=block.x+block.w):
|
490
|
+
|
491
|
+
ball.vx=-ball.vx
|
492
|
+
|
493
|
+
score+=ADD_SCORE
|
494
|
+
|
495
|
+
canvas.itemconfigure(id_score,text="score:"+str(self.score))
|
496
|
+
|
497
|
+
block.bc-=1
|
498
|
+
|
499
|
+
elif(block.x<=ball.x+ball.d/2<=block.x+block.w
|
500
|
+
|
501
|
+
and(block.y<=ball.y<=block.y+block.h
|
502
|
+
|
503
|
+
or block.y<=ball.y+ball.d<=block.y+block.h)):
|
504
|
+
|
505
|
+
ball.vy=-ball.vy
|
506
|
+
|
507
|
+
score+=ADD_SCORE
|
508
|
+
|
509
|
+
canvas.itemconfigure(id_score,text="score:"+str(self.score))
|
510
|
+
|
511
|
+
block.bc-=1
|
512
|
+
|
513
|
+
if block.bc==0:
|
514
|
+
|
515
|
+
delete_block(block)
|
516
|
+
|
517
|
+
blocks.remove(block)
|
518
|
+
|
519
|
+
return True
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
def make_spear(self,x,y,w=SPEAR_WIDTH,h=SPEAR_HEIGHT,c=SPEAR_COLOR):
|
524
|
+
|
525
|
+
id=canvas.create_rectangle(x,y,x+w,y+h,fill=c,outline=c)
|
526
|
+
|
527
|
+
return Spear(id,x,y,w,h,SPEAR_VY,c)
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
def check_spear(self,spear,paddle):
|
532
|
+
|
533
|
+
if(paddle.x<=spear.x<=paddle.x+paddle.w
|
534
|
+
|
535
|
+
and spear.y+spear.h>=paddle.y
|
536
|
+
|
537
|
+
and spear.y<=paddle.y+paddle.h):
|
538
|
+
|
539
|
+
return True
|
540
|
+
|
541
|
+
else:
|
542
|
+
|
543
|
+
return False
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
def clear(self,blocks):
|
548
|
+
|
549
|
+
if len(blocks)==0:
|
550
|
+
|
551
|
+
canvas.create_text(BOX_CENTER,200,text="Clear!",
|
248
552
|
|
249
553
|
font=('FixedSys',16))
|
250
554
|
|
555
|
+
|
556
|
+
|
557
|
+
def wait_start(self):
|
558
|
+
|
559
|
+
id_score=canvas.create_text(10,10,text=("score:"+str(self.score)),
|
560
|
+
|
561
|
+
font=("FixedSys",16),
|
562
|
+
|
563
|
+
justify="left",anchor=NW)
|
564
|
+
|
565
|
+
id_text=canvas.create_text(BOX_CENTER,200,text="Press 'SPACE' to start",
|
566
|
+
|
567
|
+
font=('FixedSys',16))
|
568
|
+
|
251
569
|
tk.update()
|
252
570
|
|
253
571
|
|
254
572
|
|
255
|
-
|
573
|
+
while not self.game_start:
|
256
|
-
|
257
|
-
|
574
|
+
|
258
|
-
|
259
|
-
west:int
|
260
|
-
|
261
|
-
north:int
|
262
|
-
|
263
|
-
east:int
|
264
|
-
|
265
|
-
|
575
|
+
tk.update()
|
266
|
-
|
267
|
-
|
576
|
+
|
268
|
-
|
269
|
-
|
577
|
+
time.sleep(DURATION)
|
270
|
-
|
271
|
-
|
578
|
+
|
272
|
-
|
273
|
-
|
579
|
+
|
274
|
-
|
275
|
-
|
580
|
+
|
276
|
-
|
277
|
-
run:int
|
278
|
-
|
279
|
-
score:int
|
280
|
-
|
281
|
-
|
581
|
+
canvas.delete(id_text)
|
282
|
-
|
582
|
+
|
283
|
-
|
583
|
+
tk.update()
|
284
584
|
|
285
585
|
|
286
586
|
|
287
|
-
def
|
288
|
-
|
289
|
-
self.
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
self.b
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
self.bl
|
298
|
-
|
299
|
-
self.
|
300
|
-
|
301
|
-
sel
|
302
|
-
|
303
|
-
self.
|
304
|
-
|
305
|
-
self.
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
self.
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
if
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
if
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
self.m
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
self.
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
block=self.make_block(BLOCK_X+(BLOCK_W+PAD)*x,BLOCK_Y,BLOCK_W,BLOCK_H,
|
362
|
-
|
363
|
-
BLOCKCOUNT,BLOCK_COLOR)
|
364
|
-
|
365
|
-
self.blocks.append(block)
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
canvas.bind_all("<KeyPress-Left>",self.left_paddle)
|
370
|
-
|
371
|
-
canvas.bind_all("<KeyPress-Right>",self.right_paddle)
|
372
|
-
|
373
|
-
canvas.bind_all("<KeyRelease-Left>",self.stop_paddle)
|
374
|
-
|
375
|
-
canvas.bind_all("<KeyRelease-Right>",self.stop_paddle)
|
376
|
-
|
377
|
-
canvas.bind_all('<KeyPress-space>',self.game_start)
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
def make_paddle(self,x,y,w,h,c):
|
384
|
-
|
385
|
-
id=canvas.create_rectangle(x,y,x+w,y+h,
|
386
|
-
|
387
|
-
fill=c,outline=c)
|
388
|
-
|
389
|
-
return Paddle(id,x,y,w,h)
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
def check_paddle(self,ball,paddle):
|
394
|
-
|
395
|
-
hit=False
|
396
|
-
|
397
|
-
if(paddle.y+paddle.h>=ball.y+ball.d+ball.vy>=paddle.y
|
398
|
-
|
399
|
-
and paddle.x<=ball.x+ball.d/2+ball.vx<=paddle.x+paddle.w):
|
400
|
-
|
401
|
-
paddle.change_paddle_color(paddle,random.choice(COLORS))
|
402
|
-
|
403
|
-
ball.vy=-ball.vy
|
404
|
-
|
405
|
-
ball.vx=int(10*(ball.x+ball.d/2-paddle.x)/paddle.w)-5
|
406
|
-
|
407
|
-
elif((paddle.x+paddle.w>=ball.x+ball.d+ball.vx>=paddle.x
|
408
|
-
|
409
|
-
or paddle.x+paddle.w>=ball.x+ball.vx>=paddle.x)
|
410
|
-
|
411
|
-
and paddle.y<=ball.y+ball.d/2+ball.vy<=paddle.y+paddle.h):
|
412
|
-
|
413
|
-
paddle.change_paddle_color(paddle,random.choice(COLORS))
|
414
|
-
|
415
|
-
ball.vx=-ball.vx
|
416
|
-
|
417
|
-
if hit:
|
418
|
-
|
419
|
-
self.paddle_count+=1
|
420
|
-
|
421
|
-
if self.paddle_count%MULTI_BALL_COUNT==0:
|
422
|
-
|
423
|
-
if len(self.balls)<BALL_MAX_NUM:
|
424
|
-
|
425
|
-
ball=self.create_ball(
|
426
|
-
|
427
|
-
BALL_X0,
|
428
|
-
|
429
|
-
BALL_Y0,10,
|
430
|
-
|
431
|
-
random.choice(VX0),
|
432
|
-
|
433
|
-
BALL_VY
|
434
|
-
|
435
|
-
)
|
436
|
-
|
437
|
-
slf.balls.apeend(ball)
|
438
|
-
|
439
|
-
self.movingObjs.append(ball)
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
def left_paddle(self,event):
|
444
|
-
|
445
|
-
self.paddle.set_v(-self.paddle_v)
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
def right_paddle(self,event):
|
450
|
-
|
451
|
-
self.paddle.set_v(self.paddle_v)
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
def stop_paddle(self,event):
|
456
|
-
|
457
|
-
self.paddle.stop()
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
def game_start(self,event):
|
462
|
-
|
463
|
-
self.run=True
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
def make_block(self,x,y,w,h,bc,c):
|
468
|
-
|
469
|
-
id=canvas.create_rectangle(x,y,x+w,y+h,
|
470
|
-
|
471
|
-
fill=c,outline=c)
|
472
|
-
|
473
|
-
return Block(id,x,y,w,h,bc,c)
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
def check_blocks(self,ball,block):
|
478
|
-
|
479
|
-
if(block.y<=ball.y+ball.d/2<=block.y+block.h
|
480
|
-
|
481
|
-
and ball.x+ball.vx<=block.x+block.w):
|
482
|
-
|
483
|
-
ball.vx=-ball.vx
|
484
|
-
|
485
|
-
score+=ADD_SCORE
|
486
|
-
|
487
|
-
canvas.itemconfigure(id_score,text="score:"+str(self.score))
|
488
|
-
|
489
|
-
block.bc-=1
|
490
|
-
|
491
|
-
elif(block.x<=ball.x+ball.d/2<=block.x+block.w
|
492
|
-
|
493
|
-
and(block.y<=ball.y<=block.y+block.h
|
494
|
-
|
495
|
-
or block.y<=ball.y+ball.d<=block.y+block.h)):
|
496
|
-
|
497
|
-
ball.vy=-ball.vy
|
498
|
-
|
499
|
-
score+=ADD_SCORE
|
500
|
-
|
501
|
-
canvas.itemconfigure(id_score,text="score:"+str(self.score))
|
502
|
-
|
503
|
-
block.bc-=1
|
504
|
-
|
505
|
-
if block.bc==0:
|
506
|
-
|
507
|
-
delete_block(block)
|
508
|
-
|
509
|
-
blocks.remove(block)
|
510
|
-
|
511
|
-
return True
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
def make_spear(self,x,y,w=SPEAR_WIDTH,h=SPEAR_HEIGHT,c=SPEAR_COLOR):
|
516
|
-
|
517
|
-
id=canvas.create_rectangle(x,y,x+w,y+h,fill=c,outline=c)
|
518
|
-
|
519
|
-
return Spear(id,x,y,w,h,SPEAR_VY,c)
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
def check_spear(self,spear,paddle):
|
524
|
-
|
525
|
-
if(paddle.x<=spear.x<=paddle.x+paddle.w
|
526
|
-
|
527
|
-
and spear.y+spear.h>=paddle.y
|
528
|
-
|
529
|
-
and spear.y<=paddle.y+paddle.h):
|
530
|
-
|
531
|
-
return True
|
532
|
-
|
533
|
-
else:
|
534
|
-
|
535
|
-
return False
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
def clear(self,blocks):
|
540
|
-
|
541
|
-
if len(blocks)==0:
|
542
|
-
|
543
|
-
canvas.create_text(BOX_CENTER,200,text="Clear!",
|
544
|
-
|
545
|
-
font=('FixedSys',16))
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
def wait_start(self):
|
550
|
-
|
551
|
-
id_score=canvas.create_text(10,10,text=("score:"+str(self.score)),
|
552
|
-
|
553
|
-
font=("FixedSys",16),
|
554
|
-
|
555
|
-
justify="left",anchor=NW)
|
556
|
-
|
557
|
-
id_text=canvas.create_text(BOX_CENTER,200,text="Press 'SPACE' to start",
|
558
|
-
|
559
|
-
font=('FixedSys',16))
|
560
|
-
|
561
|
-
tk.update()
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
while not self.game_start:
|
587
|
+
def animate(self):
|
588
|
+
|
589
|
+
self.movingObjs=[self.paddle]+self.balls
|
590
|
+
|
591
|
+
while self.run==True:
|
592
|
+
|
593
|
+
for obj in self.movingObjs:
|
594
|
+
|
595
|
+
obj.move()
|
596
|
+
|
597
|
+
for ball in self.balls:
|
598
|
+
|
599
|
+
if self.check_wall(ball):
|
600
|
+
|
601
|
+
canvas.delete(ball.id)
|
602
|
+
|
603
|
+
self.balls.remove(ball)
|
604
|
+
|
605
|
+
self.movingObjs.remove(ball)
|
606
|
+
|
607
|
+
if len(balls)==0:
|
608
|
+
|
609
|
+
self.game_end("Game Over")
|
610
|
+
|
611
|
+
break
|
612
|
+
|
613
|
+
self.check_paddle(self.ball,self.paddle)
|
614
|
+
|
615
|
+
for obj in self.movingObjs:
|
616
|
+
|
617
|
+
obj.redraw()
|
618
|
+
|
619
|
+
for block in self.blocks:
|
620
|
+
|
621
|
+
self.check_blocks(self.ball,block)
|
622
|
+
|
623
|
+
block.redraw()
|
624
|
+
|
625
|
+
if self.spear:
|
626
|
+
|
627
|
+
if self.check_spear(self.spear,self.paddle):
|
628
|
+
|
629
|
+
self.game_end("Game Over")
|
630
|
+
|
631
|
+
break
|
632
|
+
|
633
|
+
if self.spear==None and random.random()<0.005:
|
634
|
+
|
635
|
+
self.spear=self.make_spear(
|
636
|
+
|
637
|
+
random.randint(self.west,self.east),
|
638
|
+
|
639
|
+
self.north)
|
640
|
+
|
641
|
+
self.movingObjs.append(self.spear)
|
642
|
+
|
643
|
+
if self.spear and self.spear.y+self.spear.h>=self.south:
|
644
|
+
|
645
|
+
canvas.delete_spear(self.spear.id)
|
646
|
+
|
647
|
+
self.movingObjs.remove(self.spear)
|
648
|
+
|
649
|
+
self.spear=None
|
650
|
+
|
651
|
+
for obj in self.movingObjs:
|
652
|
+
|
653
|
+
obj.redraw()
|
654
|
+
|
655
|
+
self.clear(self.blocks)
|
656
|
+
|
657
|
+
break
|
658
|
+
|
659
|
+
time.sleep(self.duration)
|
566
660
|
|
567
661
|
tk.update()
|
568
662
|
|
569
|
-
time.sleep(DURATION)
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
canvas.delete(id_text)
|
574
|
-
|
575
|
-
tk.update()
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
def animate(self):
|
580
|
-
|
581
|
-
self.movingObjs=[self.paddle]+self.balls
|
582
|
-
|
583
|
-
while self.run==True:
|
584
|
-
|
585
|
-
for obj in self.movingObjs:
|
586
|
-
|
587
|
-
obj.move()
|
588
|
-
|
589
|
-
for ball in self.balls:
|
590
|
-
|
591
|
-
if self.check_wall(ball):
|
592
|
-
|
593
|
-
canvas.delete(ball.id)
|
594
|
-
|
595
|
-
self.balls.remove(ball)
|
596
|
-
|
597
|
-
self.movingObjs.remove(ball)
|
598
|
-
|
599
|
-
if len(balls)==0:
|
600
|
-
|
601
|
-
self.game_end("Game Over")
|
602
|
-
|
603
|
-
break
|
604
|
-
|
605
|
-
self.check_paddle(self.ball,self.paddle)
|
606
|
-
|
607
|
-
for obj in self.movingObjs:
|
608
|
-
|
609
|
-
obj.redraw()
|
610
|
-
|
611
|
-
for block in self.blocks:
|
612
|
-
|
613
|
-
self.check_blocks(self.ball,block)
|
614
|
-
|
615
|
-
block.redraw()
|
616
|
-
|
617
|
-
if self.spear:
|
618
|
-
|
619
|
-
if self.check_spear(self.spear,self.paddle):
|
620
|
-
|
621
|
-
self.game_end("Game Over")
|
622
|
-
|
623
|
-
break
|
624
|
-
|
625
|
-
if self.spear==None and random.random()<0.005:
|
626
|
-
|
627
|
-
self.spear=self.make_spear(
|
628
|
-
|
629
|
-
random.randint(self.west,self.east),
|
630
|
-
|
631
|
-
self.north)
|
632
|
-
|
633
|
-
self.movingObjs.append(self.spear)
|
634
|
-
|
635
|
-
if self.spear and self.spear.y+self.spear.h>=self.south:
|
636
|
-
|
637
|
-
canvas.delete_spear(self.spear.id)
|
638
|
-
|
639
|
-
self.movingObjs.remove(self.spear)
|
640
|
-
|
641
|
-
self.spear=None
|
642
|
-
|
643
|
-
for obj in self.movingObjs:
|
644
|
-
|
645
|
-
obj.redraw()
|
646
|
-
|
647
|
-
self.clear(self.blocks)
|
648
|
-
|
649
|
-
break
|
650
|
-
|
651
|
-
time.sleep(self.duration)
|
652
|
-
|
653
|
-
tk.update()
|
654
|
-
|
655
663
|
|
656
664
|
|
657
665
|
box=Box(BOX_TOP_X,BOX_TOP_Y,BOX_WIDTH,BOX_HEIGHT,DURATION)
|