我正在使用tkinter创建一个破砖游戏,但无法弄清楚如何更新记分板。我没有收到任何错误消息。唯一的问题是整个游戏中记分牌保持为零。每次击中一个块时,它应该增加10个点。任何建议都会很棒。
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)
答案 0 :(得分:2)
更改self.scoreboard
后,您必须手动替换文字
canvas.itemconfig(self.id, text='Your score = %s' % (self.scoreboard))
effbot.org:The Tkinter Canvas Widget
顺便说一句:如果您使用IntVar()
来保持分数,那么您可以使用trace('w', callback)
将函数callback
分配给此IntVar()
,这可能会在每次更改时在画布上更改此文本此IntVar()
effbot.org:The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar)