找回密码
 中文实名注册
搜索
查看: 121|回复: 2

逃离幽灵2

[复制链接]

8

主题

17

回帖

660

积分

高级会员

积分
660
发表于 2025-10-5 11:59:21 | 显示全部楼层 |阅读模式
[Python] 纯文本查看 复制代码
# --- 4. 绘图函数 ---

def draw_maze(screen, maze):
    for y in range(MAZE_H):
        for x in range(MAZE_W):
            cell_walls = maze[y][x]
            x1, y1 = x * CELL_SIZE, y * CELL_SIZE
            x2, y2 = (x + 1) * CELL_SIZE, (y + 1) * CELL_SIZE
            wall_thickness = 2 

            if cell_walls[0] == 1: 
                pygame.draw.line(screen, BLUE, (x1, y1), (x2, y1), wall_thickness)
            if cell_walls[1] == 1: 
                pygame.draw.line(screen, BLUE, (x2, y1), (x2, y2), wall_thickness)
            if cell_walls[2] == 1: 
                pygame.draw.line(screen, BLUE, (x1, y2), (x2, y2), wall_thickness)
            if cell_walls[3] == 1: 
                pygame.draw.line(screen, BLUE, (x1, y1), (x1, y2), wall_thickness)

def draw_start_end(screen):
    start_rect = pygame.Rect(0, 0, CELL_SIZE, CELL_SIZE)
    pygame.draw.rect(screen, (0, 100, 0), start_rect) 

    end_rect = pygame.Rect((MAZE_W - 1) * CELL_SIZE, (MAZE_H - 1) * CELL_SIZE, CELL_SIZE, CELL_SIZE)
    pygame.draw.rect(screen, GREEN_END, end_rect)


# --- 5. 游戏主循环 ---

def game_loop():
    
    # 终点坐标
    end_cx, end_cy = MAZE_W - 1, MAZE_H - 1
    
    def setup_game():
        nonlocal maze_data, player, ghosts_group, protectors_group, doctors_group, all_sprites, player_lives
        
        maze_data = generate_maze(MAZE_W, MAZE_H)
        player = Player(maze_data)
        
        ghosts_list = []
        protectors_list = []
        
        # 1. 初始化 10 个幽灵
        for i in range(NUM_GHOSTS):
            start_cx = random.randrange(MAZE_W // 2, MAZE_W)
            start_cy = random.randrange(MAZE_H // 2, MAZE_H)
            delay = random.randrange(GHOST_MOVE_DELAY_MIN, GHOST_MOVE_DELAY_MAX)
            ghosts_list.append(Ghost(maze_data, start_cx, start_cy, delay))

        # 2. 初始化 2 个普通保镖
        for i in range(NUM_BODYGUARDS):
            start_cx = random.randrange(1, MAZE_W // 4)
            start_cy = random.randrange(1, MAZE_H // 4)
            protectors_list.append(Bodyguard(maze_data, start_cx, start_cy))

        # 3. 初始化 1 个关羽
        guan_yu_cx = MAZE_W // 2
        guan_yu_cy = MAZE_H // 2
        protectors_list.append(GuanYu(maze_data, guan_yu_cx, guan_yu_cy))
        
        # 4. 初始化 3 个医生
        for i in range(NUM_DOCTORS):
            start_cx = random.randrange(MAZE_W // 2, MAZE_W)
            start_cy = random.randrange(MAZE_H // 2, MAZE_H)
            protectors_list.append(Doctor(maze_data, start_cx, start_cy))


        ghosts_group = pygame.sprite.Group(ghosts_list)
        protectors_group = pygame.sprite.Group(protectors_list)
        doctors_group = pygame.sprite.Group([p for p in protectors_list if isinstance(p, Doctor)])
        
        all_sprites = pygame.sprite.Group(player, ghosts_group, protectors_group)
        
        player_lives = INITIAL_LIVES
        
        return False, False 

    maze_data = None
    player = None
    ghosts_group = None
    protectors_group = None
    doctors_group = None
    all_sprites = None
    player_lives = INITIAL_LIVES
    
    game_lost, game_won = setup_game()

    running = True

    while running:
        clock.tick(FPS)
        
        # 处理输入事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            
            if event.type == pygame.KEYDOWN and not game_won and not game_lost:
                dx, dy = 0, 0
                if event.key == pygame.K_LEFT: dx = -1
                elif event.key == pygame.K_RIGHT: dx = 1
                elif event.key == pygame.K_UP: dy = -1
                elif event.key == pygame.K_DOWN: dy = 1
                
                if dx != 0 or dy != 0:
                    player.move(dx, dy)
                
            elif event.type == pygame.KEYDOWN and (game_won or game_lost):
                if event.key == pygame.K_r: 
                    game_lost, game_won = setup_game()
                    
        # 游戏逻辑更新
        if not game_won and not game_lost:
            
            # 1. AI 移动更新
            for ghost in ghosts_group:
                ghost.update(player.cx, player.cy)

            # 区分保镖/关羽和医生进行更新
            for entity in protectors_group:
                if isinstance(entity, Doctor):
                    entity.update(player.cx, player.cy)
                elif isinstance(entity, (Bodyguard, GuanYu)):
                    entity.update(ghosts_group, player.cx, player.cy)

            # 2. 碰撞检测
            
            # 玩家与幽灵碰撞
            player_hit = False
            for ghost in ghosts_group:
                if player.cx == ghost.cx and player.cy == ghost.cy:
                    player_hit = True
                    break
            
            if player_hit:
                player_lives -= 1
                if player_lives <= 0:
                    game_lost = True
                else:
                    player.respawn()
            
            # 玩家与医生碰撞 (治疗)
            for doctor in doctors_group:
                if player.cx == doctor.cx and player.cy == doctor.cy:
                    if player_lives < MAX_LIVES:
                        player_lives += 1
                        doctor.respawn_random()
                    
            # 保镖/关羽与幽灵碰撞 (幽灵被永久移除)
            for protector in protectors_group:
                if isinstance(protector, (Bodyguard, GuanYu)):
                    for ghost in list(ghosts_group):
                        if protector.cx == ghost.cx and protector.cy == ghost.cy:
                            # 幽灵被永久移除
                            ghost.kill()
                            break 
            
            # 检查胜利条件
            if player.cx == end_cx and player.cy == end_cy:
                game_won = True

        # 绘制
        SCREEN.fill(BLACK)
        draw_start_end(SCREEN)
        draw_maze(SCREEN, maze_data)
        
        # 绘制实体
        all_sprites.draw(SCREEN)
        
        # 绘制生命值
        draw_text(SCREEN, f"生命: {player_lives}", 24, 50, SCREEN_HEIGHT - 20)
        
        # 状态提示
        if game_won:
            draw_text(SCREEN, "恭喜! 你逃脱了!", 48, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, YELLOW)
            draw_text(SCREEN, "按 R 键重新开始", 24, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50, WHITE)
        
        if game_lost:
            draw_text(SCREEN, "你被抓住了! 游戏失败!", 48, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, RED)
            draw_text(SCREEN, "按 R 键重新开始", 24, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50, WHITE)
            
        # 绘制存活的幽灵数量
        draw_text(SCREEN, f"幽灵存活: {len(ghosts_group)}", 24, SCREEN_WIDTH // 2, SCREEN_HEIGHT - 20)
        # 绘制保镖数量
        draw_text(SCREEN, f"保镖存活: {NUM_BODYGUARDS + 1}", 24, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 20)


        # 刷新显示
        pygame.display.flip()

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    # 运行此代码需要安装 Pygame 库: pip install pygame
    game_loop()

回复

使用道具 举报

8

主题

17

回帖

660

积分

高级会员

积分
660
 楼主| 发表于 2025-10-5 12:00:28 | 显示全部楼层
这是逃离幽灵3
回复

使用道具 举报

11

主题

13

回帖

848

积分

高级会员

积分
848
发表于 2025-10-18 14:05:25 | 显示全部楼层
有错误。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表