用python写一个俄罗斯方块
时间: 2023-11-23 12:07:45 浏览: 35
好的,下面是一个简单的俄罗斯方块游戏的 Python 代码:
```python
import pygame
import random
# 游戏屏幕大小
screen_width = 600
screen_height = 800
# 方块大小和间隔
block_size = 30
block_space = 2
# 游戏区域大小
game_width = block_size * 10 + block_space * 9
game_height = block_size * 20 + block_space * 19
# 方块形状
shapes = [
[[1, 1], [1, 1]], # 方块
[[1, 1, 1, 1]], # 长条
[[1, 1, 0], [0, 1, 1]], # Z 形
[[0, 1, 1], [1, 1, 0]], # 反 Z 形
[[1, 1, 1], [0, 1, 0]], # T 形
[[1, 0, 0], [1, 1, 1]], # L 形
[[0, 0, 1], [1, 1, 1]] # 反 L 形
]
# 方块颜色
colors = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 0, 255), # 紫色
(0, 255, 255), # 青色
(255, 255, 255) # 白色
]
# 初始化 Pygame
pygame.init()
# 创建游戏屏幕
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption("俄罗斯方块")
# 创建游戏区域
game_area = pygame.Surface((game_width, game_height))
# 设置游戏区域背景颜色
game_area.fill((0, 0, 0))
# 计算游戏区域在屏幕中的位置
game_x = (screen_width - game_width) // 2
game_y = (screen_height - game_height) // 2
# 创建字体对象
font = pygame.font.SysFont(None, 36)
# 创建计分板
score_board = font.render("Score: 0", True, (255, 255, 255))
# 计分板位置
score_board_x = game_x + game_width + 50
score_board_y = game_y + 50
# 创建下一个方块区域
next_block_area = pygame.Surface((block_size * 4, block_size * 4))
# 下一个方块区域位置
next_block_area_x = game_x + game_width + 50
next_block_area_y = game_y + 150
# 游戏结束标志
game_over = False
# 方块类
class Block:
def __init__(self):
# 随机选择一个形状和颜色
self.shape = random.choice(shapes)
self.color = random.choice(colors)
# 初始位置在游戏区域顶部中央
self.x = game_width // 2 - len(self.shape[0]) * block_size // 2
self.y = 0
# 绘制方块
def draw(self, surface):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j]:
pygame.draw.rect(surface, self.color, (self.x + j * (block_size + block_space), self.y + i * (block_size + block_space), block_size, block_size))
# 移动方块
def move(self, dx, dy):
self.x += dx
self.y += dy
# 检查方块是否碰到边缘或其他方块
def check_collision(self, dx, dy, blocks):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j]:
x = self.x + j * (block_size + block_space) + dx
y = self.y + i * (block_size + block_space) + dy
if x < 0 or x >= game_width or y < 0 or y >= game_height:
return True
for block in blocks:
if block != self and x == block.x and y == block.y:
return True
return False
# 旋转方块
def rotate(self):
old_shape = self.shape
new_shape = []
for i in range(len(old_shape[0])):
new_row = []
for j in range(len(old_shape)):
new_row.append(old_shape[len(old_shape) - j - 1][i])
new_shape.append(new_row)
self.shape = new_shape
if self.check_collision(0, 0, []):
self.shape = old_shape
# 方块列表
blocks = []
# 当前方块和下一个方块
current_block = Block()
next_block = Block()
# 游戏分数
score = 0
# 游戏循环
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 current_block.check_collision(-block_size - block_space, 0, blocks):
current_block.move(-block_size - block_space, 0)
elif event.key == pygame.K_RIGHT:
if not current_block.check_collision(block_size + block_space, 0, blocks):
current_block.move(block_size + block_space, 0)
elif event.key == pygame.K_DOWN:
if not current_block.check_collision(0, block_size + block_space, blocks):
current_block.move(0, block_size + block_space)
elif event.key == pygame.K_UP:
current_block.rotate()
# 移动当前方块
if not current_block.check_collision(0, block_size + block_space, blocks):
current_block.move(0, block_size + block_space)
else:
# 将当前方块加入方块列表
blocks.append(current_block)
# 检查是否有满行
full_rows = []
for i in range(game_height // (block_size + block_space)):
if all([(b.y // (block_size + block_space)) == i for b in blocks]):
full_rows.append(i)
if full_rows:
# 移除满行
for i in range(len(blocks) - 1, -1, -1):
if blocks[i].y // (block_size + block_space) in full_rows:
del blocks[i]
# 从上往下移动所有方块
for i in range(len(blocks) - 1, -1, -1):
if blocks[i].y // (block_size + block_space) in range(max(full_rows) + 1):
blocks[i].move(0, block_size + block_space * len(full_rows))
# 增加分数
score += len(full_rows) * 100
score_board = font.render("Score: " + str(score), True, (255, 255, 255))
# 更新当前方块和下一个方块
current_block = next_block
next_block = Block()
# 检查游戏结束
if current_block.check_collision(0, 0, blocks):
game_over = True
# 绘制游戏区域和方块列表
game_area.fill((0, 0, 0))
for block in blocks:
block.draw(game_area)
current_block.draw(game_area)
# 绘制下一个方块区域和下一个方块
next_block_area.fill((0, 0, 0))
next_block.draw(next_block_area)
# 绘制分数板
screen.blit(score_board, (score_board_x, score_board_y))
# 将游戏区域和下一个方块区域绘制到屏幕上
screen.blit(game_area, (game_x, game_y))
screen.blit(next_block_area, (next_block_area_x, next_block_area_y))
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
```
这个代码实现了一个简单的俄罗斯方块游戏,包含方块的移动、旋转和消除,以及分数的计算和显示。你可以运行这个代码,查看游戏效果。