请用python设计一个俄罗斯方块游戏,使用Tkinter 。 要求:用户使用左、右和下箭头来控制盒子的左、右和向下移动;使用上箭头来改变正方形的方向。在游戏结束时将会显示一个弹出式对话框
时间: 2024-01-22 11:20:30 浏览: 183
好的,以下是基于Python和Tkinter的俄罗斯方块游戏代码,希望能帮助到你:
```python
import random
import tkinter as tk
from tkinter import messagebox
# 定义方块形状及颜色
shapes = [
[[1, 1, 1], [0, 1, 0]],
[[0, 2, 2], [2, 2, 0]],
[[3, 3, 0], [0, 3, 3]],
[[4, 0, 0], [4, 4, 4]],
[[0, 0, 5], [5, 5, 5]],
[[6, 6], [6, 6]]
]
colors = ['black', 'red', 'blue', 'green', 'orange', 'purple', 'yellow']
class Tetris(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.master.title('俄罗斯方块')
self.width = 300
self.height = 600
self.cell_size = 30
self.board_width = 10
self.board_height = 20
self.canvas_width = self.cell_size * self.board_width
self.canvas_height = self.cell_size * self.board_height
self.canvas = tk.Canvas(self.master, width=self.width, height=self.height,
borderwidth=0, highlightthickness=0)
self.canvas.pack(padx=10, pady=10)
self.score = 0
self.is_gameover = False
self.bind_events()
self.init_game()
def bind_events(self):
self.master.bind('<Left>', lambda _: self.move(-1, 0))
self.master.bind('<Right>', lambda _: self.move(1, 0))
self.master.bind('<Down>', lambda _: self.move(0, 1))
self.master.bind('<Up>', lambda _: self.rotate())
def init_game(self):
# 初始化方格矩阵
self.board = [[0] * self.board_width for _ in range(self.board_height)]
self.current_shape = self.get_new_shape()
self.current_x = self.board_width // 2 - len(self.current_shape[0]) // 2
self.current_y = 0
self.draw_board()
self.draw_shape()
self.update_score()
def get_new_shape(self):
# 随机获取一种方块形状
shape = random.choice(shapes)
color = random.choice(colors)
return {'shape': shape, 'color': color}
def draw_board(self):
# 绘制游戏面板
self.canvas.delete('all')
for y in range(self.board_height):
for x in range(self.board_width):
color = colors[self.board[y][x]]
self.canvas.create_rectangle(
x * self.cell_size, y * self.cell_size,
(x + 1) * self.cell_size, (y + 1) * self.cell_size,
fill=color, outline='white'
)
def draw_shape(self):
# 绘制当前方块形状
for y, row in enumerate(self.current_shape):
for x, cell in enumerate(row):
if cell:
color = self.current_shape['color']
self.canvas.create_rectangle(
(self.current_x + x) * self.cell_size,
(self.current_y + y) * self.cell_size,
(self.current_x + x + 1) * self.cell_size,
(self.current_y + y + 1) * self.cell_size,
fill=color, outline='white'
)
def move(self, dx, dy):
# 移动方块
if not self.is_gameover:
self.current_x += dx
self.current_y += dy
if self.check_collision():
self.current_x -= dx
self.current_y -= dy
if dy:
self.lock_shape()
self.clear_lines()
self.current_shape = self.get_new_shape()
self.current_x = self.board_width // 2 - len(self.current_shape[0]) // 2
self.current_y = 0
if self.check_collision():
self.is_gameover = True
self.show_gameover_dialog()
self.draw_board()
self.draw_shape()
def rotate(self):
# 旋转方块
if not self.is_gameover:
old_shape = self.current_shape['shape']
new_shape = [[old_shape[j][i] for j in range(len(old_shape))] for i in range(len(old_shape[0]) - 1, -1, -1)]
self.current_shape['shape'] = new_shape
if self.check_collision():
self.current_shape['shape'] = old_shape
self.draw_board()
self.draw_shape()
def check_collision(self):
# 检测方块与边界或已有方块的碰撞
shape = self.current_shape['shape']
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
if x + self.current_x < 0 or x + self.current_x >= self.board_width or \
y + self.current_y >= self.board_height or \
self.board[y + self.current_y][x + self.current_x]:
return True
return False
def lock_shape(self):
# 锁定当前方块
shape = self.current_shape['shape']
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
self.board[y + self.current_y][x + self.current_x] = shapes.index(shape) + 1
def clear_lines(self):
# 消除满行
lines = 0
for y in range(self.board_height):
if all(self.board[y]):
self.board.pop(y)
self.board.insert(0, [0] * self.board_width)
lines += 1
if lines:
self.score += lines ** 2
self.update_score()
def update_score(self):
# 更新分数
self.master.title('俄罗斯方块 - 得分: {}'.format(self.score))
def show_gameover_dialog(self):
# 显示游戏结束对话框
messagebox.showinfo('游戏结束', '游戏结束,得分: {}'.format(self.score))
if __name__ == '__main__':
root = tk.Tk()
game = Tetris(root)
game.pack()
root.mainloop()
```
运行代码后即可开始游戏,使用左、右和下箭头来控制盒子的左、右和向下移动,使用上箭头来改变正方形的方向。在游戏结束时将会显示一个弹出式对话框。
阅读全文
相关推荐


















