Pygame问卷循环异常:无法正常推进,重复重置问题排查
Hey there! Let's work through this Pygame quiz problem together. The issue you're hitting is super common when mixing game loops and one-off interactive flows like a quiz—either the quiz repeats nonstop, or your buttons stop working entirely. Here's how to fix it with a simple state-tracking approach:
The Core Problem Breakdown
- Putting
quiz()inside theplay_function'swhile Trueloop means it gets called every single frame, so the quiz restarts over and over. - Moving it outside the loop means the main game loop (which handles button clicks, window events, etc.) never runs again—so your buttons don't register input.
The Solution: Use a State Variable
We'll add a boolean flag to track whether the quiz has already been completed. This way, we only run the quiz once, and keep the main game loop alive to handle button interactions afterward.
Full Working Code Example
import pygame import sys # Initialize Pygame pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Pygame Quiz Demo") font = pygame.font.Font(None, 36) # Button setup button_rect = pygame.Rect(300, 250, 200, 80) button_color = (0, 255, 0) hover_color = (0, 200, 0) def quiz(): """Run the quiz once, then return to the main play screen""" current_question = 0 questions = [ "What's 2+2?", "What's the capital of France?", "Which planet is closest to the Sun?" ] answers = ["4", "Paris", "Mercury"] user_input = "" quiz_running = True while quiz_running: screen.fill((255, 255, 255)) # Draw current question question_text = font.render(questions[current_question], True, (0,0,0)) screen.blit(question_text, (100, 100)) # Draw input box and user text input_rect = pygame.Rect(100, 200, 600, 50) pygame.draw.rect(screen, (200,200,200), input_rect) input_text = font.render(user_input, True, (0,0,0)) screen.blit(input_text, (input_rect.x + 5, input_rect.y + 5)) # Draw submit button submit_rect = pygame.Rect(350, 300, 100, 50) pygame.draw.rect(screen, (0,255,0), submit_rect) submit_text = font.render("Submit", True, (0,0,0)) screen.blit(submit_text, (365, 310)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: user_input = user_input[:-1] elif event.key == pygame.K_RETURN: # Check answer and move to next question if user_input == answers[current_question]: current_question += 1 user_input = "" # End quiz if all questions are done if current_question >= len(questions): quiz_running = False if event.type == pygame.MOUSEBUTTONDOWN: if submit_rect.collidepoint(event.pos): if user_input == answers[current_question]: current_question += 1 user_input = "" if current_question >= len(questions): quiz_running = False pygame.display.flip() def play_function(): quiz_completed = False # State flag to track quiz status running = True while running: screen.fill((255, 255, 255)) # Draw play screen button mouse_pos = pygame.mouse.get_pos() if button_rect.collidepoint(mouse_pos): pygame.draw.rect(screen, hover_color, button_rect) else: pygame.draw.rect(screen, button_color, button_rect) button_text = font.render("Start Quiz", True, (0,0,0)) screen.blit(button_text, (330, 270)) # Run quiz only if it hasn't been completed yet if not quiz_completed: # Check if start button was clicked for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: if button_rect.collidepoint(event.pos): quiz() quiz_completed = True # Mark quiz as done else: # After quiz is done, show completion message and handle events complete_text = font.render("Quiz Completed! Thanks for playing!", True, (0,0,0)) screen.blit(complete_text, (150, 150)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.flip() pygame.quit() sys.exit() # Start the game play_function()
Key Details in This Code
quiz_completedFlag: This boolean variable lives in the mainplay_functionloop. It starts asFalse, and once the user finishes the quiz, we set it toTrue—so the quiz never runs again.- Isolated Quiz Loop: The
quiz()function has its own internal loop to handle question navigation, input, and submission. Once all questions are answered, it exits back to the mainplay_functionloop. - Event Handling: Both the main play screen and the quiz handle Pygame events properly, so buttons and input work as expected without freezing.
内容的提问来源于stack exchange,提问作者Mandy
相关产品推荐
相关产品推荐

