提问:生成一个俄罗斯方块游戏 回答: 以下是一个使用Python和pygame
库实现的简单俄罗斯方块游戏代码。
安装依赖
在运行代码之前,需要确保已经安装了pygame
库,可以使用以下命令进行安装:
bash
pip install pygame
代码实现
python:tetris_game.py
import pygame
import random
# 初始化pygame
pygame.init()
# 定义常量
WIDTH, HEIGHT = 300, 600
BLOCK_SIZE = 30
ROWS, COLS = HEIGHT // BLOCK_SIZE, WIDTH // BLOCK_SIZE
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
MAGENTA = (255, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
ORANGE = (255, 165, 0)
# 定义形状
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
]
COLORS = [CYAN, YELLOW, MAGENTA, GREEN, RED, BLUE, ORANGE]
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("俄罗斯方块")
# 定义游戏板
board = [[0] * COLS for _ in range(ROWS)]
# 定义方块类
class Block:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = random.choice(COLORS)
def move_down(self):
self.y += 1
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def rotate(self):
self.shape = list(map(list, zip(*self.shape[::-1])))
def draw(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[0])):
if self.shape[i][j] == 1:
pygame.draw.rect(screen, self.color, (
(self.x + j) * BLOCK_SIZE, (self.y + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 创建新方块
def new_block():
shape = random.choice(SHAPES)
return Block(COLS // 2 - len(shape[0]) // 2, 0, shape)
# 检查碰撞
def check_collision(block):
for i in range(len(block.shape)):
for j in range(len(block.shape[0])):
if block.shape[i][j] == 1:
if block.y + i >= ROWS or block.x + j < 0 or block.x + j >= COLS or board[block.y + i][block.x + j] == 1:
return True
return False
# 合并方块到游戏板
def merge_block(block):
for i in range(len(block.shape)):
for j in range(len(block.shape[0])):
if block.shape[i][j] == 1:
board[block.y + i][block.x + j] = 1
# 检查并清除满行
def clear_lines():
full_lines = []
for i in range(ROWS):
if all(board[i]):
full_lines.append(i)
for line in full_lines:
del board[line]
board.insert(0, [0] * COLS)
return len(full_lines)
# 绘制游戏板
def draw_board():
for i in range(ROWS):
for j in range(COLS):
if board[i][j] == 1:
pygame.draw.rect(screen, WHITE, (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 主游戏循环
clock = pygame.time.Clock()
current_block = new_block()
game_over = False
score = 0
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
new_block = Block(current_block.x - 1, current_block.y, current_block.shape)
if not check_collision(new_block):
current_block.move_left()
elif event.key == pygame.K_RIGHT:
new_block = Block(current_block.x + 1, current_block.y, current_block.shape)
if not check_collision(new_block):
current_block.move_right()
elif event.key == pygame.K_DOWN:
new_block = Block(current_block.x, current_block.y + 1, current_block.shape)
if not check_collision(new_block):
current_block.move_down()
elif event.key == pygame.K_UP:
new_shape = list(map(list, zip(*current_block.shape[::-1])))
new_block = Block(current_block.x, current_block.y, new_shape)
if not check_collision(new_block):
current_block.rotate()
new_block = Block(current_block.x, current_block.y + 1, current_block.shape)
if check_collision(new_block):
merge_block(current_block)
lines_cleared = clear_lines()
score += lines_cleared
current_block = new_block()
if check_collision(current_block):
game_over = True
else:
current_block.move_down()
screen.fill(BLACK)
draw_board()
current_block.draw()
pygame.display.flip()
clock.tick(3)
# 游戏结束
pygame.quit()
代码解释
- 初始化部分 :初始化
pygame
库,定义游戏窗口的大小、方块的大小、颜色等常量。 - 形状和颜色定义:定义了俄罗斯方块的七种形状和对应的颜色。
- 方块类:定义了方块的移动、旋转和绘制方法。
- 游戏逻辑:包括创建新方块、检查碰撞、合并方块到游戏板、清除满行等功能。
- 主游戏循环:处理用户输入,更新方块的位置,检查碰撞,绘制游戏界面。
运行代码
将上述代码保存为tetris_game.py
,然后在终端中运行以下命令:
bash
python tetris_game.py
这样就可以启动俄罗斯方块游戏了。使用左右箭头键控制方块的左右移动,下箭头键加速方块下落,上箭头键旋转方块。