写一个俄罗斯方块python程序
时间: 2023-08-14 13:05:41 浏览: 54
以下是一个简单的俄罗斯方块 Python 程序示例,使用 Pygame 模块实现:
```python
import pygame
import random
# 定义方块的形状及颜色
block_shapes = [
[[1, 1, 1],
[0, 1, 0]],
[[0, 2, 2],
[2, 2, 0]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 0, 0],
[4, 4, 4]],
[[0, 0, 5],
[5, 5, 5]],
[[6, 6, 6, 6]],
[[7, 7],
[7, 7]]
]
block_colors = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(0, 255, 255), # 青色
(255, 0, 255), # 紫色
(255, 255, 255) # 白色
]
# 初始化 Pygame
pygame.init()
# 定义游戏窗口
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("俄罗斯方块")
# 定义游戏参数
block_size = 30
board_width = 10
board_height = 20
board_top = 50
board_left = (screen_width - block_size * board_width) // 2
fps = 30
score = 0
# 定义游戏区域
board = [[0] * board_width for i in range(board_height)]
# 绘制游戏区域
def draw_board():
for i in range(board_height):
for j in range(board_width):
if board[i][j] > 0:
pygame.draw.rect(screen, block_colors[board[i][j] - 1], (board_left + j * block_size, board_top + i * block_size, block_size, block_size), 0)
pygame.draw.rect(screen, (255, 255, 255), (board_left + j * block_size, board_top + i * block_size, block_size, block_size), 1)
# 绘制方块
def draw_block(block, x, y):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] > 0:
pygame.draw.rect(screen, block_colors[block[i][j] - 1], (board_left + (x + j) * block_size, board_top + (y + i) * block_size, block_size, block_size), 0)
pygame.draw.rect(screen, (255, 255, 255), (board_left + (x + j) * block_size, board_top + (y + i) * block_size, block_size, block_size), 1)
# 判断方块是否越界或与已有方块重叠
def is_valid_position(block, x, y):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] > 0:
if x + j < 0 or x + j >= board_width or y + i < 0 or y + i >= board_height or board[y + i][x + j] > 0:
return False
return True
# 随机生成一个方块
def get_random_block():
shape = random.choice(block_shapes)
block = []
for i in range(len(shape)):
row = []
for j in range(len(shape[i])):
if shape[i][j] > 0:
row.append(len(block_colors))
else:
row.append(0)
block.append(row)
return block
# 将方块加入游戏区域
def add_block_to_board(block, x, y):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] > 0:
board[y + i][x + j] = block[i][j]
# 消除整行
def remove_complete_lines():
global score
num_lines_removed = 0
i = board_height - 1
while i >= 0:
if all(board[i]):
for j in range(i, 0, -1):
for k in range(board_width):
board[j][k] = board[j - 1][k]
for k in range(board_width):
board[0][k] = 0
num_lines_removed += 1
score += 10
else:
i -= 1
return num_lines_removed
# 绘制得分
def draw_score():
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), 1, (255, 255, 255))
screen.blit(text, (10, 10))
# 主函数
def main():
global score
clock = pygame.time.Clock()
# 随机生成第一个方块
current_block = get_random_block()
current_x = board_width // 2 - len(current_block[0]) // 2
current_y = -len(current_block)
next_block = get_random_block()
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if is_valid_position(current_block, current_x - 1, current_y):
current_x -= 1
elif event.key == pygame.K_RIGHT:
if is_valid_position(current_block, current_x + 1, current_y):
current_x += 1
elif event.key == pygame.K_UP:
rotated_block = [[current_block[j][i] for j in range(len(current_block))] for i in range(len(current_block[0]) - 1, -1, -1)]
if is_valid_position(rotated_block, current_x, current_y):
current_block = rotated_block
elif event.key == pygame.K_DOWN:
if is_valid_position(current_block, current_x, current_y + 1):
current_y += 1
# 绘制游戏界面
screen.fill((0, 0, 0))
draw_board()
draw_block(current_block, current_x, current_y)
draw_score()
# 移动方块
if is_valid_position(current_block, current_x, current_y + 1):
current_y += 1
else:
add_block_to_board(current_block, current_x, current_y)
num_lines_removed = remove_complete_lines()
score += num_lines_removed ** 2
current_block = next_block
current_x = board_width // 2 - len(current_block[0]) // 2
current_y = -len(current_block)
next_block = get_random_block()
if not is_valid_position(current_block, current_x, current_y):
pygame.quit()
quit()
# 更新屏幕
pygame.display.update()
# 控制帧率
clock.tick(fps)
# 启动程序
if __name__ == '__main__':
main()
```
这个程序实现了一个基本的俄罗斯方块游戏,玩家可以通过方向键控制方块的移动和旋转,将方块放置在游戏区域中,消除整行并得分。程序中使用 Pygame 模块实现了游戏窗口、方块和游戏区域的绘制,以及方块的移动、旋转和放置等游戏逻辑。
阅读全文