我正在使用Tkinter在python中创建一个基本的破砖游戏。我已经想出了如何删除这些块,但是当球和砖发生碰撞时会出现错误。它总是说索引超出范围。任何建议都会受到欢迎。
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, 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.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
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 pos[2] >= block1_pos[0] and pos[0] <= block1_pos[2]:
if 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 pos[2] >= block2_pos[0] and pos[0] <= block2_pos[2]:
if 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 pos[2] >= block3_pos[0] and pos[0] <= block3_pos[2]:
if 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 pos[2] >= block4_pos[0] and pos[0] <= block4_pos[2]:
if 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 pos[2] >= block5_pos[0] and pos[0] <= block5_pos[2]:
if pos[3] >= block5_pos[1] and pos[3] <= block5_pos[3]:
self.canvas.delete(self.block5.id)
return True
return False
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 + 20, fill=color)
self.canvas.move(self.id, self.x, self.y)
block1 = Block(canvas, 50, 50, 'orange')
block2 = Block(canvas, 90, 50, 'yellow')
block3 = Block(canvas, 130, 50, 'green')
block4 = Block(canvas, 170, 50, 'blue')
block5 = Block(canvas, 210, 50, 'violet')
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 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 :(得分:0)
您的问题是,当删除某个块时,其坐标不再存在。
对于所有块,你想要做的是这样的事情:
if block1_pos and (pos[2] >= block1_pos[0] and pos[0] <= block1_pos[2]):
这将确保在检查块之前存在该块。