python写俄罗斯方块代码
时间: 2023-09-27 13:06:48 浏览: 196
俄罗斯方块python代码
5星 · 资源好评率100%
好的,以下是一个简单的 Python 俄罗斯方块代码示例:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 游戏区域大小
WIDTH = 300
HEIGHT = 600
# 方块大小
BLOCK_SIZE = 30
# 方块移动速度
SPEED = 500
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 游戏区域矩形
game_rect = pygame.Rect(0, 0, WIDTH, HEIGHT)
# 方块矩形列表
block_rects = []
# 当前方块形状
current_block = []
# 下一个方块形状
next_block = []
# 方块类型列表
BLOCK_TYPES = [
[(0, 0), (1, 0), (0, 1), (1, 1)], # 方块
[(0, 0), (1, 0), (2, 0), (3, 0)], # 长条
[(0, 0), (0, 1), (1, 1), (2, 1)], # L 形
[(2, 0), (0, 1), (1, 1), (2, 1)], # 反 L 形
[(1, 0), (2, 0), (0, 1), (1, 1)], # Z 形
[(0, 0), (1, 0), (1, 1), (2, 1)], # 反 Z 形
[(1, 0), (0, 1), (1, 1), (2, 1)] # T 形
]
# 方块颜色列表
BLOCK_COLORS = [
YELLOW,
BLUE,
GREEN,
RED,
YELLOW,
BLUE,
GREEN,
RED
]
# 随机生成下一个方块
def generate_next_block():
global next_block
next_block = BLOCK_TYPES[random.randint(0, len(BLOCK_TYPES) - 1)]
# 生成新的方块
def generate_new_block():
global current_block
current_block = next_block
generate_next_block()
# 初始位置
x = WIDTH // 2 - BLOCK_SIZE
y = 0
# 根据方块形状生成矩形列表
for pos in current_block:
block_rects.append(pygame.Rect(x + pos[0] * BLOCK_SIZE, y + pos[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 移动方块
def move_block(dx, dy):
global block_rects
# 移动每个方块矩形
for rect in block_rects:
rect.move_ip(dx * BLOCK_SIZE, dy * BLOCK_SIZE)
# 旋转方块
def rotate_block():
global block_rects
# 计算旋转后的方块形状
new_block = []
for pos in current_block:
new_pos = (pos[1], -pos[0])
new_block.append(new_pos)
# 生成新的方块矩形列表
new_rects = []
x, y = block_rects[0].left, block_rects[0].top
for pos in new_block:
new_rects.append(pygame.Rect(x + pos[0] * BLOCK_SIZE, y + pos[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 判断是否与边界或其他方块重叠
for rect in new_rects:
if not game_rect.contains(rect) or rect.collidelist(block_rects) != -1:
return
# 更新方块矩形列表
block_rects = new_rects
# 消除已经填满的行
def remove_full_rows():
global block_rects
# 统计每一行的方块数量
row_blocks = {}
for rect in block_rects:
row = rect.top // BLOCK_SIZE
if row not in row_blocks:
row_blocks[row] = []
row_blocks[row].append(rect)
# 消除已经填满的行
removed_rows = []
for row, rects in row_blocks.items():
if len(rects) == WIDTH // BLOCK_SIZE:
for rect in rects:
block_rects.remove(rect)
removed_rows.append(row)
# 将上面的方块移动下来
for row in sorted(removed_rows, reverse=True):
for rect in block_rects:
if rect.top < row * BLOCK_SIZE:
rect.move_ip(0, BLOCK_SIZE)
# 游戏主循环
def main_loop():
global block_rects
global current_block
global next_block
# 生成初始方块
generate_new_block()
# 游戏主循环
clock = pygame.time.Clock()
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_block(-1, 0)
elif event.key == pygame.K_RIGHT:
move_block(1, 0)
elif event.key == pygame.K_DOWN:
move_block(0, 1)
elif event.key == pygame.K_UP:
rotate_block()
# 每隔一定时间自动下落方块
if pygame.time.get_ticks() % SPEED == 0:
move_block(0, 1)
# 消除已经填满的行
remove_full_rows()
# 如果方块到达底部或与其他方块重叠,生成新的方块
bottom = max(rect.bottom for rect in block_rects)
if bottom == HEIGHT or any(rect.collidelist(block_rects) != -1 for rect in block_rects):
block_rects = []
generate_new_block()
# 绘制游戏界面
screen.fill(BLACK)
for rect, color in zip(block_rects, BLOCK_COLORS[current_block]):
pygame.draw.rect(screen, color, rect)
pygame.display.flip()
clock.tick(60)
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 生成下一个方块
generate_next_block()
# 运行游戏
main_loop()
# 退出 Pygame
pygame.quit()
```
这个代码实现了一个简单的俄罗斯方块游戏,包括方块的移动、旋转、下落、消除已经填满的行等功能。你可以根据自己的需求进行修改和扩展。
阅读全文