tkinterを使用してレンガブレーカーゲームを作成していますが、スコアボードを更新する方法がわかりません。エラーメッセージは表示されません。唯一の問題は、スコアボードがゲーム中ずっとゼロにとどまっていることです。ブロックにヒットするたびに10ポイントを追加する必要があります。どんな提案も素晴らしいだろう。tkinterを使用してスコアボードを更新するには
from tkinter import *
import random
import time
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()
class Ball:
def __init__(self, canvas, paddle, score, block1, block2, block3, block4, block5, color):
self.canvas = canvas
self.paddle = paddle
self.block1 = block1
self.block2 = block2
self.block3 = block3
self.block4 = block4
self.block5 = block5
self.score = score
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
self.hit_bottom = False
def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 3
if self.hit_paddle(pos) == True:
self.y = -3
if pos[3] >= self.canvas_height:
self.hit_bottom = True
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
if self.hit_block(pos) == True:
self.y = +3
if self.hit_block == True:
self.scoreboard = self.score_board + self.add_score
def hit_paddle(self, pos):
paddle_pos = self.canvas.coords(self.paddle.id)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False
def hit_block(self,pos):
block1_pos = self.canvas.coords(self.block1.id)
if block1_pos and (pos[2] >= block1_pos[0] and pos[0] <= block1_pos[2]):
if block1_pos and(pos[3] >= block1_pos[1] and pos[3] <= block1_pos[3]):
self.canvas.delete(self.block1.id)
return True
block2_pos = self.canvas.coords(self.block2.id)
if block2_pos and (pos[2] >= block2_pos[0] and pos[0] <= block2_pos[2]):
if block2_pos and(pos[3] >= block2_pos[1] and pos[3] <= block2_pos[3]):
self.canvas.delete(self.block2.id)
return True
block3_pos = self.canvas.coords(self.block3.id)
if block3_pos and (pos[2] >= block3_pos[0] and pos[0] <= block3_pos[2]):
if block3_pos and(pos[3] >= block3_pos[1] and pos[3] <= block3_pos[3]):
self.canvas.delete(self.block3.id)
return True
block4_pos = self.canvas.coords(self.block4.id)
if block4_pos and (pos[2] >= block4_pos[0] and pos[0] <= block4_pos[2]):
if block4_pos and(pos[3] >= block4_pos[1] and pos[3] <= block4_pos[3]):
self.canvas.delete(self.block4.id)
return True
block5_pos = self.canvas.coords(self.block5.id)
if block5_pos and (pos[2] >= block5_pos[0] and pos[0] <= block5_pos[2]):
if block5_pos and(pos[3] >= block5_pos[1] and pos[3] <= block5_pos[3]):
self.canvas.delete(self.block5.id)
return True
return False
class Score:
def __init__(self, canvas):
self.canvas = canvas
self.add_score = 10
self.scoreboard = 0
self.id = canvas.create_text(50,25, text='Your score = %s' %(self.scoreboard))
class Paddle:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
self.canvas.move(self.id, 200, 300)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
def turn_left(self, evt):
self.x = -2
def turn_right(self, evt):
self.x = 2
def draw(self):
self.canvas.move(self.id, self.x, 0)
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
elif pos[2] >= self.canvas_width:
self.x = 0
class Block:
def __init__(self, canvas, x, y, color):
self.canvas = canvas
self.x = int(x)
self.y = int(y)
self.id = canvas.create_rectangle(self.x, self.y, self.x + 50, self.y + 10, fill=color)
self.canvas.move(self.id, self.x, self.y)
block1 = Block(canvas, 50, 25, 'orange')
block2 = Block(canvas, 90, 25, 'yellow')
block3 = Block(canvas, 130, 25, 'green')
block4 = Block(canvas, 170, 25, 'blue')
block5 = Block(canvas, 210, 25, 'violet')
score = Score(canvas)
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, score, block1, block2, block3, block4, block5, 'red')
while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
。 – Frogboxe
'ブロック[0]'、 'ブロック[1]'などのリストをブロックし、 'for'ループを使って' hit_paddle'の衝突をチェックする必要があります。あなたのコードはずっと短くて簡単に変更できます。 – furas
'canvas'には、ブロックとの衝突を簡単にチェックできる[find_overlapping](http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.find_overlapping-method)があります。 – furas