如何使用 Deepseek 创建并运行《Pac-Man》游戏
创建《Pac-Man》游戏的步骤如下:
- 准备环境:安装 Python 和 Pygame 库(运行
pip install pygame
)。 - 设计框架:包括迷宫、吃豆人、豆子、鬼魂和得分系统。
- 编写代码:逐步实现迷宫绘制、角色移动、碰撞检测等功能。
- 优化扩展:可添加动画、音效或多关卡(可选)。
生成的完整的 Python 代码
以下是完整的 Python 代码,保存为 pacman.py
后运行:
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置窗口
WIDTH, HEIGHT = 800, 600
BLOCK_SIZE = 40
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pac-Man 小游戏")
# 颜色定义
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 迷宫布局(1为墙,0为路径)
maze = [
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
]
# Pac-Man初始设置
pacman_pos = [WIDTH // 2, HEIGHT // 2]
pacman_speed = 5
# 鬼魂初始设置
ghost_pos = [100, 100]
ghost_speed = 3
# 豆子初始化
dots = []
for row in range(len(maze)):
for col in range(len(maze[row])):
if maze[row][col] == 0:
dots.append((col * BLOCK_SIZE + BLOCK_SIZE // 2, row * BLOCK_SIZE + BLOCK_SIZE // 2))
# 分数和字体
score = 0
font = pygame.font.SysFont(None, 36)
# 绘制迷宫
def draw_maze():
for row in range(len(maze)):
for col in range(len(maze[row])):
if maze[row][col] == 1:
pygame.draw.rect(screen, BLUE, (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 绘制Pac-Man
def draw_pacman():
pygame.draw.circle(screen, YELLOW, (int(pacman_pos[0]), int(pacman_pos[1])), 20)
# 绘制豆子
def draw_dots():
global score
for dot in dots[:]:
pygame.draw.circle(screen, WHITE, (int(dot[0]), int(dot[1])), 5)
if abs(pacman_pos[0] - dot[0]) < 15 and abs(pacman_pos[1] - dot[1]) < 15:
dots.remove(dot)
score += 10
# 绘制鬼魂
def draw_ghost():
pygame.draw.rect(screen, RED, (ghost_pos[0], ghost_pos[1], 30, 30))
# 绘制分数
def draw_score():
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
# 碰撞检测
def is_collision(x, y):
row = int(y // BLOCK_SIZE)
col = int(x // BLOCK_SIZE)
if 0 <= row < len(maze) and 0 <= col < len(maze[0]):
return maze[row][col] == 1
return True
# 鬼魂移动
def move_ghost():
direction = random.choice([(ghost_speed, 0), (-ghost_speed, 0), (0, ghost_speed), (0, -ghost_speed)])
new_x = ghost_pos[0] + direction[0]
new_y = ghost_pos[1] + direction[1]
if not is_collision(new_x, new_y):
ghost_pos[0], ghost_pos[1] = new_x, new_y
# 检查游戏结束
def check_game_over():
if abs(pacman_pos[0] - ghost_pos[0]) < 30 and abs(pacman_pos[1] - ghost_pos[1]) < 30:
return True
return False
# 游戏主循环
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 吃豆人移动
keys = pygame.key.get_pressed()
new_x, new_y = pacman_pos[0], pacman_pos[1]
if keys[pygame.K_LEFT]:
new_x -= pacman_speed
if keys[pygame.K_RIGHT]:
new_x += pacman_speed
if keys[pygame.K_UP]:
new_y -= pacman_speed
if keys[pygame.K_DOWN]:
new_y += pacman_speed
if not is_collision(new_x, new_y):
pacman_pos[0], pacman_pos[1] = new_x, new_y
# 鬼魂移动
move_ghost()
# 检查游戏结束
if check_game_over():
print("游戏结束!最终得分:", score)
running = False
# 绘制游戏元素
screen.fill(BLACK)
draw_maze()
draw_dots()
draw_pacman()
draw_ghost()
draw_score()
# 更新屏幕
pygame.display.flip()
clock.tick(30)
pygame.quit()
运行步骤:
- 确保安装 Pygame(pip install pygame)。
- 将代码保存为 pacman.py。
- 在终端运行 python pacman.py。
操作说明:
- 使用箭头键(↑↓←→)控制吃豆人移动。
- 吃掉白色小点得分。
- 避开红色鬼魂,碰到即游戏结束。
如果运行出错怎么办?
- Pygame 未安装:运行 pip install pygame。
- 窗口不显示:检查 Python 环境,或以管理员权限运行终端。
- 其他错误:请提供具体错误信息,我会帮您调试。
保存与使用
- 将此 Markdown 内容复制到文本编辑器,保存为 pacman_faq.md。
- 您可以在本地查看,或上传到 GitHub 等平台生成在线链接。
评论区