用Python写一段俄罗斯方块的游戏代码
时间: 2024-04-25 19:05:31 浏览: 9
好的,以下是一个简单的俄罗斯方块游戏的代码,基于 Python 和 Pygame 库实现:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义窗口尺寸
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 500
# 定义游戏区域尺寸
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
# 定义方块尺寸
BLOCK_SIZE = 20
# 定义颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Tetris')
# 定义游戏区域
board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)]
# 定义方块形状
shapes = [
[[1, 1],
[1, 1]],
[[0, 2, 0],
[2, 2, 2]],
[[0, 3, 3],
[3, 3, 0]],
[[4, 4, 0],
[0, 4, 4]],
[[5, 5, 5, 5]],
[[0, 6, 6],
[6, 6, 0]],
[[7, 7, 7],
[0, 7, 0]],
]
# 定义方块颜色
colors = [
RED,
GREEN,
BLUE,
YELLOW,
WHITE,
RED,
GREEN,
]
# 随机生成一个方块
current_shape = random.choice(shapes)
current_color = random.choice(colors)
current_x = BOARD_WIDTH // 2 - len(current_shape[0]) // 2
current_y = 0
# 定义游戏时钟
clock = pygame.time.Clock()
# 定义游戏结束标志
game_over = False
# 定义得分
score = 0
# 定义字体
font = pygame.font.SysFont(None, 30)
# 定义函数:绘制游戏区域
def draw_board():
for y in range(BOARD_HEIGHT):
for x in range(BOARD_WIDTH):
color = BLACK
if board[y][x] != 0:
color = colors[board[y][x] - 1]
pygame.draw.rect(screen, color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
pygame.draw.rect(screen, WHITE, (0, 0, BOARD_WIDTH * BLOCK_SIZE, BOARD_HEIGHT * BLOCK_SIZE), 5)
# 定义函数:绘制方块
def draw_shape(shape, x, y, color):
for row in range(len(shape)):
for col in range(len(shape[0])):
if shape[row][col] != 0:
pygame.draw.rect(screen, color, ((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
# 定义函数:检查方块是否越界或与其他方块重叠
def check_collision(board, shape, x, y):
for row in range(len(shape)):
for col in range(len(shape[0])):
if shape[row][col] != 0:
new_x = x + col
new_y = y + row
if new_x < 0 or new_x >= BOARD_WIDTH or new_y < 0 or new_y >= BOARD_HEIGHT or board[new_y][new_x] != 0:
return True
return False
# 定义函数:将当前方块固定到游戏区域
def fix_shape(board, shape, x, y, color):
for row in range(len(shape)):
for col in range(len(shape[0])):
if shape[row][col] != 0:
new_x = x + col
new_y = y + row
board[new_y][new_x] = color
# 定义函数:清除已填满的行
def clear_rows(board):
num_rows_cleared = 0
for y in range(BOARD_HEIGHT):
if all(board[y]):
board.pop(y)
board.insert(0, [0] * BOARD_WIDTH)
num_rows_cleared += 1
return num_rows_cleared
# 游戏循环
while not game_over:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if not check_collision(board, current_shape, current_x - 1, current_y):
current_x -= 1
elif event.key == pygame.K_RIGHT:
if not check_collision(board, current_shape, current_x + 1, current_y):
current_x += 1
elif event.key == pygame.K_DOWN:
if not check_collision(board, current_shape, current_x, current_y + 1):
current_y += 1
elif event.key == pygame.K_UP:
# 旋转方块
rotated_shape = [[shape[col][len(shape) - row - 1] for col in range(len(shape[0]))] for row in range(len(shape))]
if not check_collision(board, rotated_shape, current_x, current_y):
current_shape = rotated_shape
elif event.key == pygame.K_SPACE:
# 一键到底
while not check_collision(board, current_shape, current_x, current_y + 1):
current_y += 1
fix_shape(board, current_shape, current_x, current_y, colors.index(current_color) + 1)
num_rows_cleared = clear_rows(board)
score += num_rows_cleared ** 2
current_shape = random.choice(shapes)
current_color = random.choice(colors)
current_x = BOARD_WIDTH // 2 - len(current_shape[0]) // 2
current_y = 0
# 绘制游戏界面
screen.fill(BLACK)
draw_board()
draw_shape(current_shape, current_x, current_y, current_color)
# 显示得分
score_text = font.render('Score: {}'.format(score), True, WHITE)
screen.blit(score_text, (WINDOW_WIDTH - score_text.get_width() - 10, 10))
# 更新游戏区域
if not check_collision(board, current_shape, current_x, current_y + 1):
current_y += 1
else:
fix_shape(board, current_shape, current_x, current_y, colors.index(current_color) + 1)
num_rows_cleared = clear_rows(board)
score += num_rows_cleared ** 2
current_shape = random.choice(shapes)
current_color = random.choice(colors)
current_x = BOARD_WIDTH // 2 - len(current_shape[0]) // 2
current_y = 0
if check_collision(board, current_shape, current_x, current_y):
game_over = True
# 更新屏幕
pygame.display.update()
# 控制游戏帧率
clock.tick(10)
# 退出 Pygame
pygame.quit()
```
运行代码后,即可开始玩俄罗斯方块游戏。在游戏中,按左、右、下、上箭头键可以控制方块的移动和旋转,按空格键可以一键到底。得分规则为消除行数的平方,游戏结束条件为方块到达游戏区域顶部。