用python的 tkinter编写俄罗斯方块
时间: 2023-07-27 22:09:57 浏览: 99
python编写俄罗斯方块
好的,以下是使用Python和Tkinter编写俄罗斯方块的基本代码:
```python
import tkinter as tk
import random
class Tetris(tk.Frame):
# 初始化游戏
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.parent.title("俄罗斯方块")
self.width = 300
self.height = 600
self.board = []
self.score = 0
self.init_game()
# 初始化游戏棋盘
def init_game(self):
self.canvas = tk.Canvas(self, width=self.width, height=self.height, borderwidth=0, highlightthickness=0)
self.canvas.pack(padx=10, pady=10)
self.bind_all('<Key>', self.on_key_press)
self.current_shape = None
self.next_shape = self.get_random_shape()
self.init_board()
self.score_label = tk.Label(self, text="分数: 0")
self.score_label.pack(side=tk.TOP)
self.after(1000, self.game_loop)
# 初始化游戏棋盘
def init_board(self):
for i in range(20):
row = []
for j in range(10):
row.append(0)
self.board.append(row)
# 绘制游戏棋盘
def draw_board(self):
self.canvas.delete("all")
for i in range(20):
for j in range(10):
if self.board[i][j] > 0:
self.canvas.create_rectangle(j * 30, i * 30, j * 30 + 30, i * 30 + 30, fill=self.get_color(self.board[i][j]), outline="white")
self.draw_shape(self.current_shape)
# 获取随机形状
def get_random_shape(self):
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, 0], [0, 5, 5, 5]],
[[6, 6], [6, 6]]
]
return random.choice(shapes)
# 绘制形状
def draw_shape(self, shape):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] > 0:
self.canvas.create_rectangle((self.current_pos[1] + j) * 30, (self.current_pos[0] + i) * 30, (self.current_pos[1] + j) * 30 + 30, (self.current_pos[0] + i) * 30 + 30, fill=self.get_color(shape[i][j]), outline="white")
# 获取颜色
def get_color(self, val):
colors = ["black", "cyan", "yellow", "purple", "green", "red", "blue"]
return colors[val]
# 游戏循环
def game_loop(self):
if not self.current_shape:
self.current_shape = self.next_shape
self.next_shape = self.get_random_shape()
self.current_pos = [0, 4]
if self.check_collision(self.current_shape, self.current_pos):
self.game_over()
else:
self.current_pos[0] += 1
if self.check_collision(self.current_shape, self.current_pos):
self.current_pos[0] -= 1
self.freeze_shape()
self.draw_board()
self.after(500, self.game_loop)
# 检查碰撞
def check_collision(self, shape, pos):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] > 0:
if i + pos[0] >= len(self.board) or j + pos[1] < 0 or j + pos[1] >= len(self.board[i]) or self.board[i + pos[0]][j + pos[1]] > 0:
return True
return False
# 冻结形状
def freeze_shape(self):
for i in range(len(self.current_shape)):
for j in range(len(self.current_shape[i])):
if self.current_shape[i][j] > 0:
self.board[self.current_pos[0] + i][self.current_pos[1] + j] = self.current_shape[i][j]
self.current_shape = None
self.check_lines()
# 检查行
def check_lines(self):
lines = 0
for i in range(len(self.board)):
if 0 not in self.board[i]:
lines += 1
self.board.pop(i)
self.board.insert(0, [0 for _ in range(10)])
self.score += lines * 10
self.score_label.config(text="分数: {}".format(self.score))
# 处理键盘事件
def on_key_press(self, event):
if event.keysym == 'Left':
self.current_pos[1] -= 1
if self.check_collision(self.current_shape, self.current_pos):
self.current_pos[1] += 1
elif event.keysym == 'Right':
self.current_pos[1] += 1
if self.check_collision(self.current_shape, self.current_pos):
self.current_pos[1] -= 1
elif event.keysym == 'Up':
self.rotate_shape()
elif event.keysym == 'Down':
self.current_pos[0] += 1
if self.check_collision(self.current_shape, self.current_pos):
self.current_pos[0] -= 1
self.freeze_shape()
# 旋转形状
def rotate_shape(self):
new_shape = []
for i in range(len(self.current_shape[0])):
row = []
for j in range(len(self.current_shape) - 1, -1, -1):
row.append(self.current_shape[j][i])
new_shape.append(row)
if not self.check_collision(new_shape, self.current_pos):
self.current_shape = new_shape
# 游戏结束
def game_over(self):
self.canvas.delete("all")
self.canvas.create_text(self.width / 2, self.height / 2, text="游戏结束", font=("Helvetica", 32))
if __name__ == '__main__':
root = tk.Tk()
game = Tetris(root)
game.pack()
root.mainloop()
```
这段代码使用了Tkinter创建了一个窗口,并在其中绘制了俄罗斯方块的游戏棋盘。在游戏中,使用方向键控制方块的移动和旋转,当方块堆积到底部时会被固定,如果一行被填满则会消除并得分。当方块堆积到顶部时游戏结束。
希望对你有所帮助!
阅读全文