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

Pygame项目启动后立即退出问题排查求助

问题分析

你的程序启动后直接退出,核心问题在于用for i in sequence直接遍历序列——这个循环会在几毫秒内跑完所有10个序列项,根本没给你查看按钮、进行交互的时间,执行完就直接走到pygame.quit()语句退出了。另外你也没处理Pygame的QUIT事件,连窗口都没法正常关闭。

修复方案

把序列遍历逻辑放到Pygame的主循环(while running)里,维护一个当前序列索引,控制每次只显示一个按钮,等用户完成交互(比如点击对应按钮)后再切换到下一个序列项,同时补上QUIT事件处理。

修改后的代码
import pygame
import os
import random

# 初始化Pygame
pygame.init()

# 设置屏幕尺寸
screen_width = 1275
screen_height = 750

# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)

# 设置字体
font = pygame.font.SysFont('Arial', 20)

# 设置按钮属性
button_radius = 70
button_x_spacing = 400
button_y_spacing = 200

# 设置初始花朵图片
flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", "flower.jpg"))
flower_rect = flower_image.get_rect()
flower_rect.center = (screen_width//2, screen_height//2)

# 设置按钮位置
button_positions = [
    (button_x_spacing, button_y_spacing),
    (button_x_spacing, button_y_spacing + 200),
    (button_x_spacing, button_y_spacing + 400)
]

# 初始化屏幕
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Marmoset Buttons")

# 时钟对象控制帧率
clock = pygame.time.Clock()

running = True
timer = 0
# 序列索引,控制当前显示哪个按钮
seq_index = 0
sequence = [0,1,2,1,2,0,2,1,0,2]

while running:
    clock.tick(60)
    
    for event in pygame.event.get():
        # 处理窗口关闭事件
        if event.type == pygame.QUIT:
            running = False
        # 处理鼠标点击事件(仅在无计时状态下响应)
        if event.type == pygame.MOUSEBUTTONDOWN and timer == 0:
            current_btn = sequence[seq_index]
            distance_button = pygame.math.Vector2(button_positions[current_btn]) - pygame.math.Vector2(pygame.mouse.get_pos())
            # 加载对应按钮的花朵图片
            flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", f"flower{current_btn}.jpg"))
            if distance_button.length() < button_radius:
                timer = pygame.time.get_ticks()
                # 点击正确后切换到下一个序列项,判断是否遍历完成
                seq_index += 1
                if seq_index >= len(sequence):
                    # 序列遍历完成后退出,可按需改成重置序列等逻辑
                    running = False

    screen.fill(black)
    
    # 序列未遍历完时,显示当前按钮
    if seq_index < len(sequence):
        current_btn = sequence[seq_index]
        pygame.draw.circle(screen, white, button_positions[current_btn], button_radius)
        text_surface = font.render(f"Button {current_btn}", True, black)
        text_rect = text_surface.get_rect(center=button_positions[current_btn])
        screen.blit(text_surface, text_rect)
    
    # 处理花朵显示的计时逻辑
    if timer != 0:
        current_time = pygame.time.get_ticks()
        if current_time - timer < 3000:
            screen.blit(flower_image, flower_rect)
        else:
            timer = 0

    pygame.display.update()

# 退出Pygame
pygame.quit()
关键改动说明
  • 用while running主循环代替原for循环,保证程序持续运行直到用户操作终止
  • 新增seq_index变量跟踪当前要显示的序列项索引
  • 仅在用户点击当前展示的按钮后,才切换到下一个序列项
  • 添加QUIT事件处理,支持用户正常关闭窗口
  • 增加序列遍历完成后的退出逻辑,可按需修改为重置序列等其他行为

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 13:07:48