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

PyGame恐龙闪卡游戏:正确答题提示无法显示问题求助

问题分析与解决方案

首先咱们来拆解你遇到的核心问题:按下数字键后正确提示不显示、调试用的回车提示瞬间消失,本质是循环逻辑混乱+显示流程错误导致的:

  1. 内层while count < 2会在主循环第一次迭代里直接跑完2题,之后主循环只处理事件却不再重绘屏幕;
  2. 按键触发blit后没有调用pygame.display.flip()更新显示,就算临时显示了,后续屏幕刷新也会直接覆盖;
  3. 缺少答题状态管理,比如当前题目是否已作答、是否需要停留显示提示等。

下面是修复后的完整代码,我标注了关键修改点:

import pygame, random

pygame.font.init()
pygame.init()

font = pygame.font.Font(None, 48)
size = width, height = 800, 800
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Dinosaur Flashcards")

# 预加载图片(避免每次循环重复加载,提升性能)
mainpic = pygame.image.load("trex1.png")

# 游戏核心状态变量
score = 0
current_question_idx = None
current_options = []
show_congrats = False
congrats_text = font.render("Congrats, You got it right!", True, (0, 255, 0))
# 调试用文本
testtext = font.render("Hello World", True, (250, 250, 250))

# 题目库(修正拼写错误:triceratops)
dino_questions = [
    "Does a t-rex eat meat?",
    "Does a triceratops have 3 horns?"
]
# 答案选项
dino_answers = ["Yes", "No"]
# 正确答案映射:每个题目对应的正确答案索引
correct_answer_map = [0, 0]

def generate_new_question():
    """生成随机题目与干扰选项"""
    q_idx = random.randint(0, len(dino_questions)-1)
    # 生成两个不同的干扰选项,确保其中一个是正确答案
    opt1 = random.randint(0, len(dino_answers)-1)
    opt2 = random.randint(0, len(dino_answers)-1)
    while opt1 == opt2:
        opt2 = random.randint(0, len(dino_answers)-1)
    # 随机把正确答案放到选项1或选项2的位置
    if random.random() > 0.5:
        return q_idx, [correct_answer_map[q_idx], opt2]
    else:
        return q_idx, [opt1, correct_answer_map[q_idx]]

# 初始化第一题
current_question_idx, current_options = generate_new_question()

crashed = False
clock = pygame.time.Clock()  # 控制帧率,避免画面跳帧

while not crashed:
    clock.tick(30)  # 限制帧率为30FPS
    screen.fill((0, 0, 0))  # 每次循环先清空屏幕,避免残留内容

    # 绘制固定元素:恐龙图片
    screen.blit(mainpic, (150, 20))

    # 根据状态绘制内容:要么显示题目选项,要么显示恭喜提示
    if not show_congrats:
        # 绘制当前题目与选项
        question_render = font.render(dino_questions[current_question_idx], True, (255, 255, 255))
        opt1_render = font.render(f'1 - {dino_answers[current_options[0]]}', True, (255, 255, 255))
        opt2_render = font.render(f'2 - {dino_answers[current_options[1]]}', True, (255, 255, 255))
        
        screen.blit(question_render, (200, 590))
        screen.blit(opt1_render, (200, 640))
        screen.blit(opt2_render, (200, 690))
    else:
        # 显示恭喜提示,停留3秒后自动切换下一题
        screen.blit(congrats_text, (150, 650))
        # 等待期间仍处理退出事件,避免程序卡顿
        start_time = pygame.time.get_ticks()
        while pygame.time.get_ticks() - start_time < 3000:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    crashed = True
                    break
            if crashed:
                break
        show_congrats = False
        # 生成新题目
        current_question_idx, current_options = generate_new_question()

    # 处理用户输入事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        if event.type == pygame.KEYDOWN:
            # 调试用回车键
            if event.key == pygame.K_RETURN:
                screen.blit(testtext, (200, 699))
            # 处理数字键答题(仅在非提示状态下生效)
            if not show_congrats:
                if event.key == pygame.K_1:
                    if current_options[0] == correct_answer_map[current_question_idx]:
                        show_congrats = True
                        score += 1
                elif event.key == pygame.K_2:
                    if current_options[1] == correct_answer_map[current_question_idx]:
                        show_congrats = True
                        score += 1

    # 关键:每次循环最后更新显示,确保所有绘制内容生效
    pygame.display.flip()

pygame.quit()

关键修改说明

  • 重构循环逻辑:去掉嵌套的while count<2,用主循环统一控制流程,每次循环只处理当前题目;
  • 状态管理:新增show_congrats变量控制提示显示状态,避免提示被瞬间覆盖;
  • 显示流程修正:每次循环最后调用pygame.display.flip(),确保所有绘制内容都能显示;
  • 性能优化:预加载恐龙图片,避免重复IO操作;
  • 答题逻辑完善:明确正确答案映射,处理数字键输入,判断正确后触发提示;
  • 提示停留机制:显示恭喜后停留3秒再切换题目,期间仍处理退出事件避免卡顿;
  • 细节修正:修正了恐龙名称的拼写错误(trycerotopes→triceratops)。

额外优化建议

  • 如果要支持3个选项,可以扩展current_options列表和数字键(K_3)的处理逻辑;
  • 可以添加分数显示模块,让孩子直观看到自己的得分;
  • 允许用户按任意键跳过恭喜提示,提升交互灵活性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:14:49