python小游戏俄罗斯方块源代码pygame
时间: 2023-06-22 14:48:14 浏览: 270
以下是使用 Pygame 实现俄罗斯方块的简单源代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 游戏区域的宽度和高度
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 800
# 方块的大小
BLOCK_SIZE = 40
# 游戏区域的宽度和高度按照方块大小计算
GAME_WIDTH = WINDOW_WIDTH // BLOCK_SIZE
GAME_HEIGHT = WINDOW_HEIGHT // BLOCK_SIZE
# 方块的颜色
BLOCK_COLORS = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 0, 255), # 紫色
(0, 255, 255), # 青色
(255, 255, 255) # 白色
]
# 7 种不同的方块形状
BLOCK_SHAPES = [
[[1, 1, 1, 1]], # I 形状
[[1, 1, 1], [0, 1, 0]], # T 形状
[[1, 1, 0], [0, 1, 1]], # Z 形状
[[0, 1, 1], [1, 1, 0]], # S 形状
[[1, 1], [1, 1]], # O 形状
[[1, 1, 1], [0, 0, 1]], # L 形状
[[1, 1, 1], [1, 0, 0]] # J 形状
]
# 游戏区域
game_area = [[0] * GAME_WIDTH for _ in range(GAME_HEIGHT)]
# 随机生成一个方块
def generate_block():
shape = random.choice(BLOCK_SHAPES)
color = random.choice(BLOCK_COLORS)
x = GAME_WIDTH // 2 - len(shape[0]) // 2
y = 0
return {"shape": shape, "color": color, "x": x, "y": y}
# 判断方块是否能够移动
def is_block_valid(block, dx, dy):
x = block["x"] + dx
y = block["y"] + dy
for i in range(len(block["shape"])):
for j in range(len(block["shape"][0])):
if block["shape"][i][j] == 1:
if x + j < 0 or x + j >= GAME_WIDTH or y + i >= GAME_HEIGHT or (y + i >= 0 and game_area[y + i][x + j] != 0):
return False
return True
# 绘制方块
def draw_block(screen, block):
for i in range(len(block["shape"])):
for j in range(len(block["shape"][0])):
if block["shape"][i][j] == 1:
pygame.draw.rect(screen, block["color"], (block["x"] * BLOCK_SIZE + j * BLOCK_SIZE, block["y"] * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 将方块加入到游戏区域中
def add_block_to_game_area(block):
for i in range(len(block["shape"])):
for j in range(len(block["shape"][0])):
if block["shape"][i][j] == 1:
game_area[block["y"] + i][block["x"] + j] = block["color"]
# 消除满行的方块
def remove_full_rows():
i = GAME_HEIGHT - 1
while i >= 0:
if all(game_area[i]):
for j in range(i, 0, -1):
game_area[j] = game_area[j - 1][:]
game_area[0] = [0] * GAME_WIDTH
else:
i -= 1
# 绘制游戏区域
def draw_game_area(screen):
for i in range(GAME_HEIGHT):
for j in range(GAME_WIDTH):
if game_area[i][j] != 0:
pygame.draw.rect(screen, game_area[i][j], (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
pygame.draw.rect(screen, (128, 128, 128), (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
# 主函数
def main():
# 创建 Pygame 窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
# 游戏时钟
clock = pygame.time.Clock()
# 当前方块
current_block = generate_block()
# 游戏是否结束
game_over = False
# 游戏循环
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 and is_block_valid(current_block, -1, 0):
current_block["x"] -= 1
elif event.key == pygame.K_RIGHT and is_block_valid(current_block, 1, 0):
current_block["x"] += 1
elif event.key == pygame.K_DOWN and is_block_valid(current_block, 0, 1):
current_block["y"] += 1
elif event.key == pygame.K_UP:
# 旋转方块
shape = current_block["shape"]
new_shape = [[shape[j][i] for j in range(len(shape))] for i in range(len(shape[0]) - 1, -1, -1)]
if is_block_valid({"shape": new_shape, "color": current_block["color"], "x": current_block["x"], "y": current_block["y"]}, 0, 0):
current_block["shape"] = new_shape
# 将当前方块向下移动一格
if is_block_valid(current_block, 0, 1):
current_block["y"] += 1
else:
# 将当前方块加入到游戏区域中
add_block_to_game_area(current_block)
# 消除满行的方块
remove_full_rows()
# 生成一个新的方块
current_block = generate_block()
# 判断游戏是否结束
if not is_block_valid(current_block, 0, 0):
game_over = True
# 绘制游戏区域和当前方块
screen.fill((0, 0, 0))
draw_game_area(screen)
draw_block(screen, current_block)
# 更新 Pygame 窗口
pygame.display.flip()
# 控制游戏时钟
clock.tick(30)
# 退出 Pygame
pygame.quit()
# 启动游戏
if __name__ == "__main__":
main()
```
这个源代码可能不是最完整最标准的版本,但足以说明 Pygame 如何实现俄罗斯方块游戏。
阅读全文