質問編集履歴
1
コードを読み取れるように変更しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,16 +6,10 @@
|
|
6
6
|
|
7
7
|
お金にあたったらスコアが50増えるとプログラムしたのですがなぜか50ではなく、1450ほど増えてしまいます、またすべてではないですがブロックが1回で消えてしまうことがあります。
|
8
8
|
|
9
|
-
エラーメッセージ
|
9
|
+
エラーメッセージは特に検出されていません
|
10
10
|
|
11
11
|
```
|
12
12
|
|
13
|
-
エラーメッセージは特に検出されていません
|
14
|
-
|
15
|
-
### 該当のソースコード
|
16
|
-
|
17
|
-
|
18
|
-
|
19
13
|
from tkinter import *
|
20
14
|
|
21
15
|
from dataclasses import dataclass
|
@@ -28,39 +22,35 @@
|
|
28
22
|
|
29
23
|
|
30
24
|
|
31
|
-
#初期状態の設定
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
DURATION = 0.01
|
25
|
+
DURATION = 0.01
|
36
|
-
|
26
|
+
|
37
|
-
PADDLE_X0 = 200
|
27
|
+
PADDLE_X0 = 200
|
38
|
-
|
28
|
+
|
39
|
-
PADDLE_Y0 = 550
|
29
|
+
PADDLE_Y0 = 550
|
40
|
-
|
30
|
+
|
41
|
-
BALL_Y0 = 300
|
31
|
+
BALL_Y0 = 300
|
42
|
-
|
43
|
-
|
44
|
-
|
32
|
+
|
33
|
+
|
34
|
+
|
45
|
-
PAD_VX = 10
|
35
|
+
PAD_VX = 10
|
46
|
-
|
36
|
+
|
47
|
-
BALL_VX = 5
|
37
|
+
BALL_VX = 5
|
48
|
-
|
38
|
+
|
49
|
-
BALL_VY = 5
|
39
|
+
BALL_VY = 5
|
50
|
-
|
51
|
-
|
52
|
-
|
40
|
+
|
41
|
+
|
42
|
+
|
53
|
-
BLOCK_X = 0
|
43
|
+
BLOCK_X = 0
|
54
|
-
|
44
|
+
|
55
|
-
BLOCK_Y = 100
|
45
|
+
BLOCK_Y = 100
|
56
|
-
|
46
|
+
|
57
|
-
BLOCK_W = 100
|
47
|
+
BLOCK_W = 100
|
58
|
-
|
48
|
+
|
59
|
-
BLOCK_H = 20
|
49
|
+
BLOCK_H = 20
|
60
|
-
|
61
|
-
|
62
|
-
|
50
|
+
|
51
|
+
|
52
|
+
|
63
|
-
NUM_BLOCKS = 7
|
53
|
+
NUM_BLOCKS = 7
|
64
54
|
|
65
55
|
|
66
56
|
|
@@ -72,8 +62,6 @@
|
|
72
62
|
|
73
63
|
|
74
64
|
|
75
|
-
#変える色を用意する
|
76
|
-
|
77
65
|
COLORS = ["blue", "red", "black", "white", "green", "pink", "orange"]
|
78
66
|
|
79
67
|
|
@@ -132,7 +120,7 @@
|
|
132
120
|
|
133
121
|
h : int
|
134
122
|
|
135
|
-
bc: int
|
123
|
+
bc: int
|
136
124
|
|
137
125
|
c : str
|
138
126
|
|
@@ -186,12 +174,6 @@
|
|
186
174
|
|
187
175
|
|
188
176
|
|
189
|
-
#---------------------------------
|
190
|
-
|
191
|
-
#ball
|
192
|
-
|
193
|
-
#ボールの描画・登録
|
194
|
-
|
195
177
|
def make_ball(x, y, d, vx, vy, c="black"):
|
196
178
|
|
197
179
|
id = canvas.create_oval(x, y, x + d, y + d,
|
@@ -202,8 +184,6 @@
|
|
202
184
|
|
203
185
|
|
204
186
|
|
205
|
-
#ボールの移動(左右)
|
206
|
-
|
207
187
|
def move_ball(ball):
|
208
188
|
|
209
189
|
ball.x += ball.vx
|
@@ -212,8 +192,6 @@
|
|
212
192
|
|
213
193
|
|
214
194
|
|
215
|
-
#ボールの再描画
|
216
|
-
|
217
195
|
def redraw_ball(ball):
|
218
196
|
|
219
197
|
canvas.coords(ball.id, ball.x, ball.y,
|
@@ -222,9 +200,7 @@
|
|
222
200
|
|
223
201
|
|
224
202
|
|
225
|
-
|
203
|
+
|
226
|
-
|
227
|
-
#パドルの描画・登録
|
228
204
|
|
229
205
|
def make_paddle(x, y, w , v, h, c="blue"):
|
230
206
|
|
@@ -236,7 +212,7 @@
|
|
236
212
|
|
237
213
|
|
238
214
|
|
239
|
-
|
215
|
+
|
240
216
|
|
241
217
|
def move_paddle(pad):
|
242
218
|
|
@@ -244,7 +220,7 @@
|
|
244
220
|
|
245
221
|
|
246
222
|
|
247
|
-
|
223
|
+
|
248
224
|
|
249
225
|
def redraw_paddle(pad):
|
250
226
|
|
@@ -254,10 +230,6 @@
|
|
254
230
|
|
255
231
|
|
256
232
|
|
257
|
-
#block
|
258
|
-
|
259
|
-
#ブロックの描画・登録
|
260
|
-
|
261
233
|
def make_block(x, y, m=40, n=300, bc=3, c="green"):
|
262
234
|
|
263
235
|
id = canvas.create_rectangle(x, y, x + m, y + n,
|
@@ -268,8 +240,6 @@
|
|
268
240
|
|
269
241
|
|
270
242
|
|
271
|
-
#複数のブロックを生成する
|
272
|
-
|
273
243
|
def make_blocks(n_rows, x0, y0, w, h, pad=10):
|
274
244
|
|
275
245
|
blocks = []
|
@@ -284,8 +254,6 @@
|
|
284
254
|
|
285
255
|
|
286
256
|
|
287
|
-
#ブロックを消す
|
288
|
-
|
289
257
|
def delete_block(block):
|
290
258
|
|
291
259
|
canvas.delete(block.id)
|
@@ -308,10 +276,6 @@
|
|
308
276
|
|
309
277
|
|
310
278
|
|
311
|
-
#spear
|
312
|
-
|
313
|
-
#槍の描画・登録
|
314
|
-
|
315
279
|
def make_spear(x, y, w=1, h=40, vy=5, c="red"):
|
316
280
|
|
317
281
|
id = canvas.create_rectangle(x, y, x + w, y + h,
|
@@ -322,24 +286,18 @@
|
|
322
286
|
|
323
287
|
|
324
288
|
|
325
|
-
#槍を消す
|
326
|
-
|
327
289
|
def delete_spear(spear):
|
328
290
|
|
329
291
|
canvas.delete(spear.id)
|
330
292
|
|
331
293
|
|
332
294
|
|
333
|
-
#槍の移動(上下)
|
334
|
-
|
335
295
|
def move_spear(spear):
|
336
296
|
|
337
297
|
spear.y += spear.vy
|
338
298
|
|
339
299
|
|
340
300
|
|
341
|
-
#槍の再描画
|
342
|
-
|
343
301
|
def redraw_spear(spear):
|
344
302
|
|
345
303
|
canvas.coords(spear.id, spear.x, spear.y,
|
@@ -348,10 +306,6 @@
|
|
348
306
|
|
349
307
|
|
350
308
|
|
351
|
-
#money
|
352
|
-
|
353
|
-
#お金の描画・登録
|
354
|
-
|
355
309
|
def make_money(x, y, w=10, h=4, vy=3, c="gold"):
|
356
310
|
|
357
311
|
id = canvas.create_rectangle(x, y, x + w, y + h,
|
@@ -362,24 +316,18 @@
|
|
362
316
|
|
363
317
|
|
364
318
|
|
365
|
-
#お金を消す
|
366
|
-
|
367
319
|
def delete_money(money):
|
368
320
|
|
369
321
|
canvas.delete(money.id)
|
370
322
|
|
371
323
|
|
372
324
|
|
373
|
-
#お金の移動(上下)
|
374
|
-
|
375
325
|
def move_money(money):
|
376
326
|
|
377
327
|
money.y += money.vy
|
378
328
|
|
379
329
|
|
380
330
|
|
381
|
-
#お金の再描画
|
382
|
-
|
383
331
|
def redraw_money(money):
|
384
332
|
|
385
333
|
canvas.coords(money.id, money.x, money.y,
|
@@ -388,12 +336,6 @@
|
|
388
336
|
|
389
337
|
|
390
338
|
|
391
|
-
#-------------------------------------------------
|
392
|
-
|
393
|
-
#wall
|
394
|
-
|
395
|
-
#壁の作成
|
396
|
-
|
397
339
|
def make_walls(ox, oy, width, height):
|
398
340
|
|
399
341
|
canvas.create_rectangle(ox, oy, ox + width, oy + height)
|
@@ -402,23 +344,19 @@
|
|
402
344
|
|
403
345
|
|
404
346
|
|
405
|
-
#---------------------------------------------------------
|
406
|
-
|
407
|
-
#パドル操作のイベントハンドラ
|
408
|
-
|
409
|
-
def left_paddle(event):
|
347
|
+
def left_paddle(event):
|
410
348
|
|
411
349
|
paddle.v = -PAD_VX
|
412
350
|
|
413
351
|
|
414
352
|
|
415
|
-
def right_paddle(event):
|
353
|
+
def right_paddle(event):
|
416
354
|
|
417
355
|
paddle.v = PAD_VX
|
418
356
|
|
419
357
|
|
420
358
|
|
421
|
-
def stop_paddle(event):
|
359
|
+
def stop_paddle(event):
|
422
360
|
|
423
361
|
paddle.v = 0
|
424
362
|
|
@@ -442,9 +380,9 @@
|
|
442
380
|
|
443
381
|
|
444
382
|
|
445
|
-
WALL_EAST = 800
|
383
|
+
WALL_EAST = 800
|
446
|
-
|
384
|
+
|
447
|
-
WALL_SOUTH = 600
|
385
|
+
WALL_SOUTH = 600
|
448
386
|
|
449
387
|
|
450
388
|
|
@@ -458,13 +396,13 @@
|
|
458
396
|
|
459
397
|
|
460
398
|
|
461
|
-
BALL_X0 = WALL_EAST/2
|
399
|
+
BALL_X0 = WALL_EAST/2
|
462
|
-
|
463
|
-
|
464
|
-
|
400
|
+
|
401
|
+
|
402
|
+
|
465
|
-
PADDLE_X0 = WALL_EAST/2-50
|
403
|
+
PADDLE_X0 = WALL_EAST/2-50
|
466
|
-
|
404
|
+
|
467
|
-
PADDLE_Y0 = WALL_SOUTH-100
|
405
|
+
PADDLE_Y0 = WALL_SOUTH-100
|
468
406
|
|
469
407
|
|
470
408
|
|
@@ -482,7 +420,7 @@
|
|
482
420
|
|
483
421
|
money = None
|
484
422
|
|
485
|
-
|
423
|
+
|
486
424
|
|
487
425
|
canvas.bind_all('<KeyPress-Left>', left_paddle)
|
488
426
|
|
@@ -492,11 +430,9 @@
|
|
492
430
|
|
493
431
|
canvas.bind_all('<KeyRelease-Right>', stop_paddle)
|
494
432
|
|
495
|
-
canvas.bind_all('<KeyPress-space>', game_start)
|
433
|
+
canvas.bind_all('<KeyPress-space>', game_start)
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
434
|
+
|
435
|
+
|
500
436
|
|
501
437
|
|
502
438
|
|
@@ -506,7 +442,7 @@
|
|
506
442
|
|
507
443
|
justify="left", anchor=NW)
|
508
444
|
|
509
|
-
|
445
|
+
|
510
446
|
|
511
447
|
id_text = canvas.create_text(WALL_EAST/2, 200, text = "Press 'SPACE' to start",
|
512
448
|
|
@@ -516,7 +452,7 @@
|
|
516
452
|
|
517
453
|
|
518
454
|
|
519
|
-
while not game.start:
|
455
|
+
while not game.start:
|
520
456
|
|
521
457
|
tk.update()
|
522
458
|
|
@@ -524,31 +460,25 @@
|
|
524
460
|
|
525
461
|
|
526
462
|
|
527
|
-
canvas.delete(id_text)
|
463
|
+
canvas.delete(id_text)
|
528
464
|
|
529
465
|
tk.update()
|
530
466
|
|
531
467
|
|
532
468
|
|
533
|
-
|
534
|
-
|
535
|
-
#プログラムのメインループ
|
536
|
-
|
537
|
-
|
538
|
-
|
539
469
|
while True:
|
540
470
|
|
541
471
|
move_paddle(paddle)
|
542
472
|
|
543
473
|
move_ball(ball)
|
544
474
|
|
545
|
-
if money : move_money(money)
|
475
|
+
if money : move_money(money)
|
546
|
-
|
476
|
+
|
547
|
-
if money == None and random.random() < 0.1:
|
477
|
+
if money == None and random.random() < 0.1:
|
548
478
|
|
549
479
|
money = make_money(random.randint(100, WALL_EAST - 100), 10)
|
550
480
|
|
551
|
-
if money and money.y + money.h >= WALL_SOUTH:
|
481
|
+
if money and money.y + money.h >= WALL_SOUTH:
|
552
482
|
|
553
483
|
delete_money(money)
|
554
484
|
|
@@ -556,23 +486,23 @@
|
|
556
486
|
|
557
487
|
|
558
488
|
|
559
|
-
if spear : move_spear(spear)
|
489
|
+
if spear : move_spear(spear)
|
560
|
-
|
490
|
+
|
561
|
-
if spear == None and random.random() < 0.7:
|
491
|
+
if spear == None and random.random() < 0.7:
|
562
492
|
|
563
493
|
spear = make_spear(random.randint(100, WALL_EAST - 100), 10)
|
564
494
|
|
565
|
-
if spear and spear.y + spear.h >= WALL_SOUTH:
|
495
|
+
if spear and spear.y + spear.h >= WALL_SOUTH:
|
566
496
|
|
567
497
|
delete_spear(spear)
|
568
498
|
|
569
499
|
spear = None
|
570
500
|
|
571
|
-
if ball.y + ball.vy <= 100:
|
501
|
+
if ball.y + ball.vy <= 100:
|
572
502
|
|
573
503
|
ball.vy = -ball.vy
|
574
504
|
|
575
|
-
if ball.y + ball.d >= WALL_SOUTH:
|
505
|
+
if ball.y + ball.d >= WALL_SOUTH:
|
576
506
|
|
577
507
|
game_over()
|
578
508
|
|
@@ -618,8 +548,6 @@
|
|
618
548
|
|
619
549
|
for block in blocks:
|
620
550
|
|
621
|
-
#ボールのX位置がブロックに届き、Y位置もブロックの範囲内
|
622
|
-
|
623
551
|
if ( block.x < ball.x + ball.d/2 < block.x + block.w
|
624
552
|
|
625
553
|
and (block.y <= ball.y <= block.y + block.h
|
@@ -628,13 +556,13 @@
|
|
628
556
|
|
629
557
|
ball.vy = -ball.vy
|
630
558
|
|
631
|
-
score += ADD_SCORE
|
559
|
+
score += ADD_SCORE
|
632
560
|
|
633
561
|
canvas.itemconfigure(id_score, text="score:" + str(score))
|
634
562
|
|
635
|
-
block.bc -= 1
|
563
|
+
block.bc -= 1
|
636
|
-
|
564
|
+
|
637
|
-
if block.bc == 0:
|
565
|
+
if block.bc == 0:
|
638
566
|
|
639
567
|
delete_block(block)
|
640
568
|
|
@@ -642,9 +570,9 @@
|
|
642
570
|
|
643
571
|
|
644
572
|
|
645
|
-
if blocks == []:break
|
573
|
+
if blocks == []:break
|
646
|
-
|
574
|
+
|
647
|
-
if len(blocks) == 0:
|
575
|
+
if len(blocks) == 0:
|
648
576
|
|
649
577
|
canvas.create_text(WALL_EAST/2, 200, text="Clear!",
|
650
578
|
|
@@ -652,8 +580,6 @@
|
|
652
580
|
|
653
581
|
break
|
654
582
|
|
655
|
-
#ボールがパドルの上端に届き、ボールの幅がパドルの幅に収まっている
|
656
|
-
|
657
583
|
if (ball.y + ball.d >= paddle.y \
|
658
584
|
|
659
585
|
and paddle.x <= ball.x <= paddle.x + paddle.w):
|
@@ -662,17 +588,27 @@
|
|
662
588
|
|
663
589
|
ball.vx = int(6 * (ball.x + ball.d/2 - paddle.x) / paddle.w)- 3
|
664
590
|
|
665
|
-
redraw_paddle(paddle)
|
591
|
+
redraw_paddle(paddle)
|
666
|
-
|
592
|
+
|
667
|
-
redraw_ball(ball)
|
593
|
+
redraw_ball(ball)
|
668
594
|
|
669
595
|
if money : redraw_money(money)
|
670
596
|
|
671
|
-
if spear : redraw_spear(spear)
|
597
|
+
if spear : redraw_spear(spear)
|
672
|
-
|
598
|
+
|
673
|
-
tk.update()
|
599
|
+
tk.update()
|
674
|
-
|
600
|
+
|
675
|
-
time.sleep(DURATION)
|
601
|
+
time.sleep(DURATION)
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
|
606
|
+
|
607
|
+
```ここに言語を入力
|
608
|
+
|
609
|
+
コード
|
610
|
+
|
611
|
+
```
|
676
612
|
|
677
613
|
|
678
614
|
|