用python写个俄罗斯方块的程序
时间: 2024-03-21 22:39:28 浏览: 156
python编写俄罗斯方块
好的,下面是一个简单的俄罗斯方块程序的Python代码:
```python
import pygame
import random
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 定义方块大小和数量
BLOCK_SIZE = 20
BLOCK_COUNT_X = 10
BLOCK_COUNT_Y = 20
# 初始化 pygame
pygame.init()
# 创建窗口
screen_width = BLOCK_SIZE * BLOCK_COUNT_X
screen_height = BLOCK_SIZE * BLOCK_COUNT_Y
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption("俄罗斯方块")
# 随机生成一个方块类型和颜色
def random_block():
block_types = ["I", "O", "T", "S", "Z", "J", "L"]
block_type = random.choice(block_types)
block_colors = [RED, GREEN, BLUE, YELLOW]
block_color = random.choice(block_colors)
return (block_type, block_color)
# 旋转方块
def rotate_block(block):
if block[0] == "I":
return ("I", block[1], [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]])
elif block[0] == "O":
return ("O", block[1], [[1, 1], [1, 1]])
elif block[0] == "T":
return ("T", block[1], [[0, 1, 0], [1, 1, 1], [0, 0, 0]])
elif block[0] == "S":
return ("S", block[1], [[0, 1, 1], [1, 1, 0], [0, 0, 0]])
elif block[0] == "Z":
return ("Z", block[1], [[1, 1, 0], [0, 1, 1], [0, 0, 0]])
elif block[0] == "J":
return ("J", block[1], [[1, 0, 0], [1, 1, 1], [0, 0, 0]])
elif block[0] == "L":
return ("L", block[1], [[0, 0, 1], [1, 1, 1], [0, 0, 0]])
# 绘制方块
def draw_block(x, y, color):
pygame.draw.rect(screen, color, [x + 1, y + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2])
# 绘制方块矩阵
def draw_block_matrix(x, y, matrix, color):
for row in range(len(matrix)):
for col in range(len(matrix[row])):
if matrix[row][col] == 1:
draw_block(x + col * BLOCK_SIZE, y + row * BLOCK_SIZE, color)
# 判断方块是否越界或与已有方块重叠
def is_collision(x, y, matrix):
for row in range(len(matrix)):
for col in range(len(matrix[row])):
if matrix[row][col] == 1:
if x + col < 0 or x + col >= BLOCK_COUNT_X or y + row >= BLOCK_COUNT_Y:
return True
if block_matrix[y // BLOCK_SIZE + row][x // BLOCK_SIZE + col] != BLACK:
return True
return False
# 将方块加入已有方块矩阵
def add_block_to_matrix(x, y, matrix, color):
for row in range(len(matrix)):
for col in range(len(matrix[row])):
if matrix[row][col] == 1:
block_matrix[y // BLOCK_SIZE + row][x // BLOCK_SIZE + col] = color
# 消除已有方块
def remove_complete_lines():
global score
complete_lines = []
for row in range(BLOCK_COUNT_Y):
if all(block_matrix[row][col] != BLACK for col in range(BLOCK_COUNT_X)):
complete_lines.append(row)
for row in complete_lines:
for y in range(row, 0, -1):
for x in range(BLOCK_COUNT_X):
block_matrix[y][x] = block_matrix[y - 1][x]
for x in range(BLOCK_COUNT_X):
block_matrix[0][x] = BLACK
score += len(complete_lines)
# 初始化已有方块矩阵
block_matrix = [[BLACK for _ in range(BLOCK_COUNT_X)] for _ in range(BLOCK_COUNT_Y)]
# 初始化游戏变量
block = random_block()
block_x = BLOCK_COUNT_X // 2 - len(block[2][0]) // 2
block_y = 0
game_over = False
score = 0
clock = pygame.time.Clock()
# 游戏循环
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 is_collision(block_x - BLOCK_SIZE, block_y, block[2]):
block_x -= BLOCK_SIZE
elif event.key == pygame.K_RIGHT:
if not is_collision(block_x + BLOCK_SIZE, block_y, block[2]):
block_x += BLOCK_SIZE
elif event.key == pygame.K_UP:
rotated_block = rotate_block(block)
if not is_collision(block_x, block_y, rotated_block[2]):
block = rotated_block
elif event.key == pygame.K_DOWN:
while not is_collision(block_x, block_y + BLOCK_SIZE, block[2]):
block_y += BLOCK_SIZE
# 更新方块位置
if not is_collision(block_x, block_y + BLOCK_SIZE, block[2]):
block_y += BLOCK_SIZE
else:
add_block_to_matrix(block_x, block_y, block[2], block[1])
remove_complete_lines()
block = random_block()
block_x = BLOCK_COUNT_X // 2 - len(block[2][0]) // 2
block_y = 0
if is_collision(block_x, block_y, block[2]):
game_over = True
# 绘制背景
screen.fill(BLACK)
# 绘制已有方块
for row in range(len(block_matrix)):
for col in range(len(block_matrix[row])):
draw_block(col * BLOCK_SIZE, row * BLOCK_SIZE, block_matrix[row][col])
# 绘制当前方块
draw_block_matrix(block_x, block_y, block[2], block[1])
# 绘制得分
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, WHITE)
screen.blit(text, [10, 10])
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(10)
# 退出 pygame
pygame.quit()
```
运行该程序后,可以使用方向键向左移动方块、向右移动方块、向上旋转方块、向下加速方块下落。当方块堆积到达顶部时,游戏结束。程序还会记录玩家的得分,通过消除已有方块来增加得分。
阅读全文