1.源代码
import pygame
import random
# 初始化游戏
pygame.init()
# 游戏窗口尺寸
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 方块大小和颜色
BLOCK_SIZE = 30
COLORS = [
(0, 0, 0), # 黑色
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 165, 0), # 橙色
(0, 255, 255), # 青色
(128, 0, 128) # 紫色
]
# 初始化游戏窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
# 定义游戏区域,使用二维列表表示,0代表空格
game_area = [[0] * (WINDOW_WIDTH
# 定义各种形状的俄罗斯方块
tetriminos = [
[[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]]
]
# 初始化当前俄罗斯方块的位置和形状
current_tetrimino = random.choice(tetriminos)
current_x = (WINDOW_WIDTH
current_y = 0
# 定义函数:绘制游戏区域
def draw_game_area():
for y in range(WINDOW_HEIGHT
for x in range(WINDOW_WIDTH
if game_area[y][x] != 0:
pygame.draw.rect(window, COLORS[game_area[y][x]], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 定义函数:检查当前俄罗斯方块是否触底或碰撞到其他方块
def check_collision():
for y in range(len(current_tetrimino)):
for x in range(len(current_tetrimino[0])):
if current_tetrimino[y][x] == 1:
if current_y + y >= WINDOW_HEIGHT
return True
return False
# 定义函数:将当前俄罗斯方块放置到游戏区域
def place_tetrimino():
for y in range(len(current_tetrimino)):
for x in range(len(current_tetrimino[0])):
if current_tetrimino[y][x] == 1:
game_area[current_y + y][current_x + x] = len(COLORS) - 1
# 定义函数:消除满行
def clear_lines():
lines_cleared = 0
y = WINDOW_HEIGHT
while y >= 0:
if all(game_area[y]):
del game_area[y]
game_area.insert(0, [0] * (WINDOW_WIDTH
lines_cleared += 1
else:
y -= 1
return lines_cleared
# 游戏循环
running = True
clock = pygame.time.Clock()
fall_timer = 0
fall_interval = 1000 # 方块下落间隔,单位毫秒
score = 0
while running:
window.fill(0)
draw_game_area()
# 方块下落
current_time = pygame.time.get_ticks()
if current_time - fall_timer >= fall_interval:
fall_timer = current_time
current_y += 1
if check_collision():
current_y -= 1
place_tetrimino()
lines_cleared = clear_lines()
score += lines_cleared
if lines_cleared > 0:
fall_interval = max(fall_interval - 50, 200) # 加快方块下落速度
current_tetrimino = random.choice(tetriminos)
current_x = (WINDOW_WIDTH
current_y = 0
if check_collision():
running = False
# 处理用户输入事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_x -= 1
if check_collision():
current_x += 1
elif event.key == pygame.K_RIGHT:
current_x += 1
if check_collision():
current_x -= 1
elif event.key == pygame.K_DOWN:
current_y += 1
if check_collision():
current_y -= 1
elif event.key == pygame.K_UP:
# 旋转方块
rotated_tetrimino = []
for i in range(len(current_tetrimino[0])):
rotated_row = [current_tetrimino[j][i] for j in range(len(current_tetrimino) - 1, -1, -1)]
rotated_tetrimino.append(rotated_row)
if not check_collision():
current_tetrimino = rotated_tetrimino
# 绘制当前俄罗斯方块
for y in range(len(current_tetrimino)):
for x in range(len(current_tetrimino[0])):
if current_tetrimino[y][x] == 1:
pygame.draw.rect(window, COLORS[len(COLORS) - 1], ((current_x + x) * BLOCK_SIZE, (current_y + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
pygame.display.update()
clock.tick(60)
pygame.quit()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
2.游戏运行