# Python Pac-Man 遊戲 以下是一個使用 Pygame 庫實現的簡單 Pac-Man 遊戲。這個遊戲包含一個黃色小球(Pac-Man)在迷宮中移動,玩家可以使用鍵盤方向鍵控制方向。 python import pygame import sys # 初始化 Pygame pygame.init() # 遊戲常數 WIDTH, HEIGHT = 600, 650 GRID_SIZE = 20 FPS = 60 # 顏色定義 BLACK = (0, 0, 0) YELLOW = (255, 255, 0) BLUE = (0, 0, 255) WHITE = (255, 255, 255) # 創建遊戲窗口 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pac-Man") clock = pygame.time.Clock() # 迷宮設計 (1表示牆壁,0表示通道) maze = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0,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0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] # Pac-Man 類 class PacMan: def __init__(self): self.x = 14 * GRID_SIZE + GRID_SIZE // 2 self.y = 23 * GRID_SIZE + GRID_SIZE // 2 self.radius = GRID_SIZE // 2 - 2 self.speed = 3 self.direction = 0 # 0: 停止, 1: 上, 2: 右, 3: 下, 4: 左 self.next_direction = 0 self.mouth_angle = 0 self.mouth_change = 3 self.mouth_max = 45 def move(self): # 根據當前方向移動 if self.direction == 1: # 上 new_y = self.y - self.speed if not self.check_wall_collision(self.x, new_y): self.y = new_y elif self.direction == 2: # 右 new_x = self.x + self.speed if not self.check_wall_collision(new_x, self.y): self.x = new_x elif self.direction == 3: # 下 new_y = self.y + self.speed if not self.check_wall_collision(self.x, new_y): self.y = new_y elif self.direction == 4: # 左 new_x = self.x - self.speed if not self.check_wall_collision(new_x, self.y): self.x = new_x # 檢查是否可以改變方向 self.try_change_direction() # 更新嘴巴動畫 self.mouth_angle += self.mouth_change if self.mouth_angle > self.mouth_max or self.mouth_angle <= 0: self.mouth_change = -self.mouth_change def check_wall_collision(self, x, y): # 檢查四個角落是否碰到牆壁 grid_x = x // GRID_SIZE grid_y = y // GRID_SIZE # 檢查左上 if maze[grid_y][grid_x] == 1: return True # 檢查右上 if maze[grid_y][(x + self.radius) // GRID_SIZE] == 1: return True # 檢查左下 if maze[(y + self.radius) // GRID_SIZE][grid_x] == 1: return True # 檢查右下 if maze[(y + self.radius) // GRID_SIZE][(x + self.radius) // GRID_SIZE] == 1: return True return False def try_change_direction(self): if self.next_direction == 0: return # 檢查是否可以改變方向 temp_x, temp_y = self.x, self.y if self.next_direction == 1: # 上 temp_y -= self.speed elif self.next_direction == 2: # 右 temp_x += self.speed elif self.next_direction == 3: # 下 temp_y += self.speed elif self.next_direction == 4: # 左 temp_x -= self.speed if not self.check_wall_collision(temp_x, temp_y): self.direction = self.next_direction self.next_direction = 0 def draw(self): # 繪製 Pac-Man if self.direction == 0: # 停止時畫完整圓 pygame.draw.circle(screen, YELLOW, (self.x, self.y), self.radius) else: # 根據方向計算嘴巴的角度 start_angle = 0 if self.direction == 1: # 上 start_angle = 90 elif self.direction == 2: # 右 start_angle = 0 elif self.direction == 3: # 下 start_angle = 270 elif self.direction == 4: # 左 start_angle = 180 # 繪製扇形 pygame.draw.arc(screen, YELLOW, (self.x - self.radius, self.y - self.radius, self.radius * 2, self.radius * 2), (start_angle + self.mouth_angle) * 3.14 / 180, (start_angle + 360 - self.mouth_angle) * 3.14 / 180, self.radius) # 填充扇形 pygame.draw.polygon(screen, YELLOW, [ (self.x, self.y), (self.x + self.radius * pygame.math.Vector2(1, 0).rotate(start_angle + self.mouth_angle)[0], self.y + self.radius * pygame.math.Vector2(1, 0).rotate(start_angle + self.mouth_angle)[1]), (self.x + self.radius * pygame.math.Vector2(1, 0).rotate(start_angle + 360 - self.mouth_angle)[0], self.y + self.radius * pygame.math.Vector2(1, 0).rotate(start_angle + 360 - self.mouth_angle)[1]) ]) # 繪製迷宮 def draw_maze(): for y in range(len(maze)): for x in range(len(maze[0])): if maze[y][x] == 1: pygame.draw.rect(screen, BLUE, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE)) elif maze[y][x] == 0: # 繪製小點 pygame.draw.circle(screen, WHITE, (x * GRID_SIZE + GRID_SIZE // 2, y * GRID_SIZE + GRID_SIZE // 2), 2) # 創建 Pac-Man pacman = PacMan() # 遊戲主循環 running = True while running: # 處理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: pacman.next_direction = 1 elif event.key == pygame.K_RIGHT: pacman.next_direction = 2 elif event.key == pygame.K_DOWN: pacman.next_direction = 3 elif event.key == pygame.K_LEFT: pacman.next_direction = 4 # 更新遊戲狀態 pacman.move() # 繪製 screen.fill(BLACK) draw_maze() pacman.draw() # 更新顯示 pygame.display.flip() # 控制幀率 clock.tick(FPS) # 退出遊戲 pygame.quit() sys.exit() ## 遊戲說明 1. *控制方式*: - 使用鍵盤方向鍵(上、下、左、右)控制 Pac-Man 移動 2. *遊戲元素*: - 黃色圓形角色 (Pac-Man) - 藍色迷宮牆壁 - 白色小點作為食物 3. *功能特點*: - Pac-Man 有動態的嘴巴開合動畫 - 碰撞檢測防止穿牆 - 方向鍵預輸入功能(可以提前按下方向鍵,當可以轉向時會自動轉向) ## 運行要求 需要安裝 Pygame 庫: pip install pygame 這個遊戲是一個基本實現,你可以進一步擴展它,例如添加鬼魂敵人、分數系統、關卡設計等來增強遊戲性。