写一个俄罗斯方块游戏代码范例
时间: 2023-12-20 18:03:14 浏览: 33
以下是一个简单的俄罗斯方块游戏的代码范例:
```
import pygame
import random
pygame.init()
# 定义游戏窗口大小
WINDOW_WIDTH, WINDOW_HEIGHT = 500, 600
WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
# 定义方块大小
BLOCK_SIZE = 25
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
PURPLE = (255, 0, 255)
ORANGE = (255, 165, 0)
# 定义方块类型和颜色
BLOCKS = {
"I": (BLUE, [(0, 0), (1, 0), (2, 0), (3, 0)]),
"J": (ORANGE, [(0, 0), (0, 1), (1, 1), (2, 1)]),
"L": (YELLOW, [(2, 0), (0, 1), (1, 1), (2, 1)]),
"O": (GREEN, [(0, 0), (1, 0), (0, 1), (1, 1)]),
"S": (PURPLE, [(1, 0), (2, 0), (0, 1), (1, 1)]),
"T": (RED, [(1, 0), (0, 1), (1, 1), (2, 1)]),
"Z": (WHITE, [(0, 0), (1, 0), (1, 1), (2, 1)])
}
# 定义游戏区域
GAME_AREA_WIDTH, GAME_AREA_HEIGHT = 10, 20
GAME_AREA = [[BLACK for _ in range(GAME_AREA_WIDTH)] for _ in range(GAME_AREA_HEIGHT)]
# 定义游戏区域起始位置
GAME_AREA_X, GAME_AREA_Y = 50, 50
# 定义当前方块和下一个方块
current_block = None
next_block = None
# 定义游戏速度和计时器
game_speed = 100
game_timer = 0
# 定义得分
score = 0
# 定义字体
FONT = pygame.font.SysFont("Arial", 30)
def draw_game_area():
"""
绘制游戏区域
"""
for y, row in enumerate(GAME_AREA):
for x, color in enumerate(row):
pygame.draw.rect(WINDOW, color, (GAME_AREA_X + x * BLOCK_SIZE, GAME_AREA_Y + y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
def draw_block(block, x, y):
"""
绘制方块
"""
for bx, by in block[1]:
pygame.draw.rect(WINDOW, block[0], (GAME_AREA_X + (bx + x) * BLOCK_SIZE, GAME_AREA_Y + (by + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
def generate_block():
"""
生成方块
"""
global current_block, next_block
if next_block is None:
current_block = random.choice(list(BLOCKS.items()))
else:
current_block = next_block
next_block = random.choice(list(BLOCKS.items()))
return current_block
def is_collision(block, x, y):
"""
检查方块是否与游戏区域或已放置的方块发生碰撞
"""
for bx, by in block[1]:
if x + bx < 0 or x + bx >= GAME_AREA_WIDTH or y + by >= GAME_AREA_HEIGHT:
return True
if y + by >= 0 and GAME_AREA[y + by][x + bx] != BLACK:
return True
return False
def update_game_area():
"""
更新游戏区域状态
"""
global current_block, score
for bx, by in current_block[1]:
GAME_AREA[by + current_y][bx + current_x] = current_block[0]
for y, row in enumerate(GAME_AREA):
if BLACK not in row:
GAME_AREA.pop(y)
GAME_AREA.insert(0, [BLACK for _ in range(GAME_AREA_WIDTH)])
score += 10
def draw_score():
"""
绘制得分
"""
score_text = FONT.render("Score: {}".format(score), True, WHITE)
WINDOW.blit(score_text, (50, 10))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 更新计时器
game_timer += clock.tick()
# 根据游戏速度更新游戏状态
if game_timer >= game_speed:
game_timer = 0
# 如果当前方块为空,则生成新的方块
if current_block is None:
current_block = generate_block()
current_x, current_y = GAME_AREA_WIDTH // 2 - 2, 0
if is_collision(current_block, current_x, current_y):
pygame.quit()
quit()
# 如果当前方块与游戏区域或已放置的方块发生碰撞,则更新游戏区域状态并清空当前方块
if is_collision(current_block, current_x, current_y + 1):
update_game_area()
current_block = None
# 否则将当前方块下落一格
else:
current_y += 1
# 绘制游戏界面
WINDOW.fill(BLACK)
draw_game_area()
draw_block(current_block, current_x, current_y)
draw_score()
pygame.display.update()
```