帮我用python的tkinter写一个打砖块
当然可以!下面是一个使用Python的Tkinter库编写的基本打砖块游戏的示例代码:
import tkinter as tk
import random
class Ball:
def __init__(self, canvas, paddle, blocks, color):
self.canvas = canvas
self.paddle = paddle
self.blocks = blocks
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
start_positions = [-3, -2, -1, 1, 2, 3]
random.shuffle(start_positions)
self.x = start_positions[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
self.hit_bottom = False
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 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 pos[3] >= self.canvas_height:
self.hit_bottom = True
if self.hit_paddle(pos):
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
for block in self.blocks:
if self.hit_block(pos, block):
self.y = 3
self.blocks.remove(block)
break
def hit_block(self, pos, block):
block_pos = self.canvas.coords(block.id)
if pos[2] >= block_pos[0] and pos[0] <= block_pos[2]:
if pos[3] >= block_pos[1] and pos[3] <= block_pos[3]:
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.move_left)
self.canvas.bind_all('<KeyPress-Right>', self.move_right)
self.canvas.bind_all('<KeyRelease-Left>', self.stop)
self.canvas.bind_all('<KeyRelease-Right>', self.stop)
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
def move_left(self, event):
self.x = -2
def move_right(self, event):
self.x = 2
def stop(self, event):
self.x = 0
class Block:
def __init__(self, canvas, x, y, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 50, 20, fill=color)
self.canvas.move(self.id, x, y)
def main():
root = tk.Tk()
root.title("打砖块")
root.resizable(0, 0)
canvas = tk.Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
root.update()
blocks = []
for i in range(0, 5):
for j in range(0, 3):
x = i * 100
y = j * 50
color = random.choice(['red', 'orange', 'yellow', 'green', 'blue'])
blocks.append(Block(canvas, x, y, color))
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, blocks, 'red')
while True:
if not ball.hit_bottom:
ball.draw()
paddle.draw()
else:
canvas.create_text(250, 200, text="游戏结束", font=('Helvetica', 30))
break
root.update_idletasks()
root.update()
canvas.focus_set()
if __name__ == '__main__':
main()
这个示例代码创建了一个打砖块游戏窗口,并使用Tkinter库实现了游戏的主要逻辑。在游戏中,玩家通过控制台左右方向键来移动挡板,使小球弹射到砖块上,直到击中所有砖块或者小球触底游戏结束。
注意:此示例中使用了random模块来随机生成砖块和小球的颜色,你可以根据需要自行修改
原文地址: https://www.cveoy.top/t/topic/h3YG 著作权归作者所有。请勿转载和采集!