import tkinter as tk
import random
class Block:
def __init__(self, color, shape):
self.color = color
self.shape = shape
self.rotation = 0
self.x = 5
self.y = 0
class Tetris:
def __init__(self, master):
self.master = master
self.master.title("俄罗斯方块游戏")
self.canvas = tk.Canvas(self.master, width=300, height=600, bg="white")
self.canvas.pack()
# 游戏状态初始化
self.board = [[None] * 10 for _ in range(20)]
self.current_block = self.spawn_block()
self.score = 0 # 初始化得分
self.game_over = False
self.game_over_text = None # 保存游戏结束文本对象
self.restart_text = None # 保存 Press 'r' to restart 文本对象
# 调整速度
self.speed = 1000 # 初始速度为1秒
self.master.bind("<Control-Up>", self.increase_speed)
self.master.bind("<Control-Down>", self.decrease_speed)
# 绑定按键操作
self.master.bind("<Left>", self.move_left)
self.master.bind("<Right>", self.move_right)
self.master.bind("<Down>", self.move_down)
self.master.bind("<Up>", self.rotate_block)
self.master.bind("<r>", self.restart_game)
self.update()
def spawn_block(self):
"""生成一个新的方块"""
color = random.choice(["cyan", "magenta", "yellow", "green", "red", "blue"])
shape = random.choice([I_SHAPE, J_SHAPE, L_SHAPE, O_SHAPE, S_SHAPE, T_SHAPE, Z_SHAPE])
return Block(color, shape)
def rotate_block(self, event):
"""旋转当前方块"""
if not self.game_over:
rotated_shape = self.rotate_shape(self.current_block.shape)
if self.is_valid_position(rotated_shape):
self.current_block.rotation = (self.current_block.rotation + 1) % 4
self.current_block.shape = rotated_shape
self.draw_block()
def move_left(self, event):
"""向左移动当前方块"""
if not self.game_over and self.is_valid_position(self.current_block.shape, dx=-1):
self.current_block.x -= 1
self.draw_block()
def move_right(self, event):
"""向右移动当前方块"""
if not self.game_over and self.is_valid_position(self.current_block.shape, dx=1):
self.current_block.x += 1
self.draw_block()
def move_down(self, event):
"""向下移动当前方块"""
if not self.game_over:
while self.is_valid_position(self.current_block.shape, dy=1):
self.current_block.y += 1
self.place_block()
self.current_block = self.spawn_block()
self.clear_lines()
# 检查是否达到底部
if not self.is_valid_position(self.current_block.shape, dy=1):
self.check_game_over()
self.draw_block()
def rotate_shape(self, shape):
"""旋转方块形状"""
return list(zip(*shape[::-1]))
def is_valid_position(self, shape, dx=0, dy=0):
"""检查方块是否在当前位置有效"""
x, y = self.current_block.x + dx, self.current_block.y + dy
for i, row in enumerate(shape):
for j, cell in enumerate(row):
if cell and (x + j < 0 or x + j >= 10 or y + i >= 20 or (y + i >= 0 and self.board[y + i][x + j])):
return False
return True
def draw_block(self):
"""绘制当前方块"""
self.canvas.delete("block")
for i, row in enumerate(self.current_block.shape):
for j, cell in enumerate(row):
if cell:
x, y = (self.current_block.x + j) * 30, (self.current_block.y + i) * 30
color = self.current_block.color if (self.current_block.y + i) >= 0 else "white"
self.canvas.create_rectangle(x, y, x + 30, y + 30, fill=color, outline="black", tags="block")
self.draw_board()
def update(self):
"""更新游戏状态"""
if not self.game_over:
if self.is_valid_position(self.current_block.shape, dy=1):
self.current_block.y += 1
self.draw_block()
else:
self.place_block()
self.current_block = self.spawn_block()
self.clear_lines()
# 检查是否达到底部
if not self.is_valid_position(self.current_block.shape, dy=1):
self.check_game_over()
self.draw_block()
self.master.after(self.speed, self.update) # 调整速度
def place_block(self):
"""将当前方块放置到底部"""
for i, row in enumerate(self.current_block.shape):
for j, cell in enumerate(row):
if cell and (self.current_block.y + i) >= 0:
self.board[self.current_block.y + i][self.current_block.x + j] = self.current_block.color
self.draw_board()
def clear_lines(self):
"""清除满行"""
full_lines = [i for i, row in enumerate(self.board) if all(row)]
for line in full_lines:
self.board.pop(line)
self.board.insert(0, [None] * 10)
# 更新得分
self.score += len(full_lines)
self.draw_score()
self.draw_board()
def draw_board(self):
"""绘制整个游戏面板"""
for i, row in enumerate(self.board):
for j, cell in enumerate(row):
if cell:
x, y = j * 30, i * 30
self.canvas.create_rectangle(x, y, x + 30, y + 30, fill=cell, outline="black", tags="block")
def draw_score(self):
"""绘制得分"""
self.canvas.delete("score")
self.canvas.create_text(150, 20, text=f"Score: {self.score}", font=("Helvetica", 16), fill="black", tags="score")
def check_game_over(self):
"""检查游戏是否结束"""
if any(cell for cell in self.board[0]):
self.game_over = True
self.game_over_text = self.canvas.create_text(150, 150, text="Game Over", font=("Helvetica", 24),
fill="red")
self.restart_text = self.canvas.create_text(150, 350, text="Press 'r' to restart", font=("Helvetica", 16),
fill="black")
def restart_game(self, event):
"""重新开始游戏"""
if self.game_over:
self.canvas.delete(self.game_over_text) # 清除游戏结束文本
self.canvas.delete(self.restart_text) # 清除 Press 'r' to restart 文本
self.board = [[None] * 10 for _ in range(20)]
self.score = 0
self.game_over = False
self.current_block = self.spawn_block()
self.draw_score()
self.draw_board()
# 调整速度
def increase_speed(self, event):
"""增加游戏速度"""
if self.speed > 100:
self.speed -= 100
print(f"Speed increased! New speed: {self.speed} milliseconds")
def decrease_speed(self, event):
"""减小游戏速度"""
self.speed += 100
print(f"Speed decreased! New speed: {self.speed} milliseconds")
# 定义方块形状
I_SHAPE = [[1, 1, 1, 1]]
J_SHAPE = [[1, 1, 1], [0, 0, 1]]
L_SHAPE = [[1, 1, 1], [1, 0, 0]]
O_SHAPE = [[1, 1], [1, 1]]
S_SHAPE = [[0, 1, 1], [1, 1, 0]]
T_SHAPE = [[1, 1, 1], [0, 1, 0]]
Z_SHAPE = [[1, 1, 0], [0, 1, 1]]
# 创建主窗口
root = tk.Tk()
tetris_game = Tetris(root)
# 运行主循环
root.mainloop()
没有合适的资源?快使用搜索试试~ 我知道了~
用python3编写的俄罗斯方块小游戏
共1个文件
py:1个
需积分: 5 0 下载量 53 浏览量
2023-12-14
23:09:21
上传
评论
收藏 2KB ZIP 举报
温馨提示
用python3编写的俄罗斯方块小游戏,可以记录分数,结束了可以重来,还可以调整下落速度。非常适合python小白研究。
资源推荐
资源详情
资源评论
收起资源包目录
俄罗斯方块.zip (1个子文件)
俄罗斯方块.py 8KB
共 1 条
- 1
资源评论
kong282828
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功