[Python] 纯文本查看 复制代码
import pygame
# 初始化Pygame
pygame.init()
# 定义迷宫尺寸和方格尺寸
maze_width = 800
maze_height = 600
cell_size = 40
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 创建迷宫地图
maze = [
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
# 初始化游戏窗口
window_width = maze_width + cell_size
window_height = maze_height + cell_size
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Maze Game")
# 加载图像资源
ball_img = pygame.image.load("ball.png")
ball_img = pygame.transform.scale(ball_img, (10 , 20))
# 小球的初始位置
ball_x = 60
ball_y = 50
# 游戏主循环
while True:
# 填充背景色
window.fill(BLACK)
# 绘制迷宫
for row in range(len(maze)):
for col in range(len(maze[row])):
if maze[row][col] == 1:
pygame.draw.rect(window, WHITE, (col * cell_size, row * cell_size, cell_size, cell_size))
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
# 退出Pygame
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
pass
elif event.key == pygame.K_DOWN:
pass
elif event.key == pygame.K_LEFT:
pass
elif event.key == pygame.K_RIGHT:
pass
# 绘制小球
window.blit(ball_img, (ball_x, ball_y))
# 刷新屏幕
pygame.display.flip()