You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修复Pygame企鹅捕鱼小游戏Game Over页面异常问题

修正企鹅捕鱼Pygame游戏的Game Over界面异常问题

问题根源

原代码在missed_score >=5时直接将game_running设为False,导致游戏循环立即终止。虽然绘制了Game Over界面,但循环结束后程序直接退出,所以界面会一闪而过(甚至黑屏)。此外,正常游戏流程的分支没有调用pygame.display.update(),也会导致画面无法刷新。

修正后的完整代码

import pygame
import random

pygame.init()

# 游戏窗口设置
screen_width = 600
screen_height = 800
game_window = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("企鹅捕鱼")

# 颜色定义
white = (255, 255, 255)
black = (0, 0, 0)

# 企鹅设置
penguin_img = pygame.image.load("penguin.png")
penguin_width = 35
penguin_height = 45
penguin_img = pygame.transform.scale(penguin_img, (penguin_width, penguin_height))
penguin_x = screen_width // 2 - penguin_width // 2
penguin_y = screen_height - penguin_height

# 鱼类设置
fish_img = pygame.image.load("fish.png")
fish_width = 25
fish_height = 25
fish_img = pygame.transform.scale(fish_img, (fish_width, fish_height))
fish_x = random.randint(0, screen_width - fish_width)
fish_y = -fish_height
fish_speed = 0.4

# 字体与分数设置
font = pygame.font.Font(None, 36)
caught_score = 0
missed_score = 0

# Game Over文本与按钮
game_over_text = font.render("游戏结束", True, black)
game_over_rect = game_over_text.get_rect(center=(screen_width // 2, screen_height // 2 - 20))
close_button_text = font.render("关闭", True, black)
close_button_rect = close_button_text.get_rect(center=(screen_width // 2, screen_height // 2 + 20))

# 游戏状态控制
game_running = True
game_over = False  # 新增游戏结束状态变量

while game_running:
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 仅在游戏结束状态下响应Close按钮
            if game_over and close_button_rect.collidepoint(event.pos):
                game_running = False

    if not game_over:
        # 企鹅移动控制(增加边界限制)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and penguin_x > 0:
            penguin_x -= 1
        elif keys[pygame.K_RIGHT] and penguin_x < screen_width - penguin_width:
            penguin_x += 1

        # 鱼类下落
        fish_y += fish_speed

        # 鱼类超出屏幕处理
        if fish_y > screen_height:
            fish_x = random.randint(0, screen_width - fish_width)
            fish_y = -fish_height
            missed_score += 1

        # 碰撞检测(企鹅捕鱼)
        if penguin_x < fish_x + fish_width and penguin_x + penguin_width > fish_x \
                and penguin_y < fish_y + fish_height and penguin_y + penguin_height > fish_y:
            fish_x = random.randint(0, screen_width - fish_width)
            fish_y = -fish_height
            caught_score += 1

        # 判断是否触发游戏结束
        if missed_score >= 5:
            game_over = True

        # 绘制游戏界面
        game_window.fill(white)
        game_window.blit(penguin_img, (penguin_x, penguin_y))
        game_window.blit(fish_img, (fish_x, fish_y))
        caught_text = font.render(f"捕获: {caught_score}", True, black)
        game_window.blit(caught_text, (10, 10))
        missed_text = font.render(f"错过: {missed_score}", True, black)
        game_window.blit(missed_text, (screen_width - 120, 10))
    else:
        # 绘制Game Over界面
        game_window.fill(white)
        game_window.blit(game_over_text, game_over_rect)
        game_window.blit(close_button_text, close_button_rect)

    # 统一更新显示,确保所有画面正常刷新
    pygame.display.update()

pygame.quit()

关键修改点

  • 新增游戏状态变量:用game_over标记游戏结束状态,避免直接终止循环,让程序能停留在Game Over界面处理按钮事件。
  • 统一画面更新:将pygame.display.update()移到循环末尾,确保无论游戏处于正常还是结束状态,画面都能正确刷新。
  • 增加企鹅边界限制:防止企鹅移出屏幕左右边界,提升游戏体验。
  • 事件响应优化:仅在游戏结束状态下响应Close按钮的点击事件,避免误操作。

内容的提问来源于stack exchange,提问作者Jose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:47:02