import pygame
from pygame.locals import *
import random
# 定义方块的形状
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]]
]
# 初始化游戏
pygame.init()
WIDTH, HEIGHT = 300, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("俄罗斯方块")
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
# 定义方块的大小和位置
BLOCK_SIZE = 30
GRID_WIDTH, GRID_HEIGHT = WIDTH // BLOCK_SIZE, HEIGHT // BLOCK_SIZE
# 游戏循环
def game_loop():
grid = [[BLACK] * GRID_WIDTH for _ in range(GRID_HEIGHT)] # 初始化游戏网格
# 创建一个随机方块
shape = random.choice(shapes)
current_x = GRID_WIDTH // 2 - len(shape[0]) // 2
current_y = 0
clock = pygame.time.Clock()
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
return
if event.type == KEYDOWN:
if event.key == K_LEFT:
current_x -= 1
if not check_collision(shape, current_x, current_y, grid):
current_x += 1
elif event.key == K_RIGHT:
current_x += 1
if not check_collision(shape, current_x, current_y, grid):
current_x -= 1
elif event.key == K_DOWN:
current_y += 1
if not check_collision(shape, current_x, current_y, grid):
current_y -= 1
elif event.key == K_SPACE:
shape = rotate(shape)
if not check_collision(shape, current_x, current_y, grid):
shape = rotate(shape, rotate_counterclockwise=True)
if not check_collision(shape, current_x, current_y, grid):
current_y += 1
if check_game_over(shape, current_x, current_y, grid):
game_over = True
# 更新游戏网格
update_grid(shape, current_x, current_y, grid)
# 消除满行
clear_lines(grid)
# 画出游戏界面
draw_grid(grid)
draw_shape(shape, current_x, current_y)
pygame.display.flip()
clock.tick(5)
# 检查方块是否与游戏网格发生碰撞
def check_collision(shape, x, y, grid):
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] == 1:
if (y + row >= GRID_HEIGHT or x + col < 0 or x + col >= GRID_WIDTH or
grid[y + row][x + col] != BLACK):
return True
return False
# 检查游戏是否结束
def check_game_over(shape, x, y, grid):
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] == 1 and y + row < 0:
return True
return False
# 更新游戏网格
def update_grid(shape, x, y, grid):
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] == 1:
grid[y + row][x + col] = current_color
# 消除满行
def clear_lines(grid):
for row in range(GRID_HEIGHT):
if all(cell != BLACK for cell in grid[row]):
del grid[row]
grid.insert(0, [BLACK] * GRID_WIDTH)
# 旋转方块
def rotate(shape, rotate_counterclockwise=False):
if rotate_counterclockwise:
return [[shape[col][len(shape) - 1 - row] for col in range(len(shape))]
for row in range(len(shape[0]))]
else:
return [[shape[len(shape) - 1 - col][row] for col in range(len(shape))]
for row in range(len(shape[0]))]
# 画出游戏网格
def draw_grid(grid):
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
pygame.draw.rect(WIN, grid[row][col],
(col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(WIN, WHITE,
(col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
# 画出方块
def draw_shape(shape, x, y):
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] == 1:
pygame.draw.rect(WIN, current_color,
((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(WIN, WHITE,
((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
# 游戏开始
game_loop()
12-19
08-01
06-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交