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

Pygame赛车游戏碰撞提示仅闪显问题排查与修复求助

Fix: Pygame Racing Game Crash Message Flashes Then Resets Instantly

Hey there, I took a look at your code and found the exact issue causing the "YOU CRASHED" message to flash only briefly before restarting the game. Let's break it down and fix it step by step.

The Root Problem

The issue lives in your message_display function. Right now, you're restarting the game loop before giving the sleep timer a chance to run:

def message_display(text):
    # ... (text rendering code)
    pygame.display.update()
    game_loop()  # This restarts the game immediately
    time.sleep(2)  # This line never gets executed!

As soon as you call game_loop(), the program jumps into a new game loop, so the time.sleep(2) is skipped entirely. That's why the message only appears for a single frame before the game resets.

Quick Fix: Adjust Execution Order

Simply swap the order of time.sleep(2) and game_loop() so the sleep runs first, giving players time to read the message:

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf",100)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)  # Wait 2 seconds so the player sees the message
    game_loop()     # Now restart the game

Optional: Better Crash Screen (With Buttons)

If you want to let players choose to restart or quit instead of auto-restarting, you can modify the crash() function to show a proper crash screen with buttons, similar to your intro:

def crash():
    crash_active = True
    while crash_active:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        
        gameDisplay.fill(white)
        # Show crash message
        largeText = pygame.font.Font("freesansbold.ttf",100)
        TextSurf, TextRect = text_objects("YOU CRASHED!", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)
        
        # Add restart/quit buttons
        button("PLAY AGAIN", 150, 450, 150, 50, green, bright_green, "play")
        button("QUIT", 550, 450, 100, 50, red, bright_red, "quit")
        
        pygame.display.update()
        clock.tick(15)

And update message_display to just handle text rendering (or remove it entirely if you use this crash screen):

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf",100)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()

Why This Works

  • The quick fix ensures the game pauses for 2 seconds to display the crash message before restarting.
  • The optional crash screen gives players control, making the game feel more polished and intentional.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:33:48