May-23-2025, 12:35 AM
I think the main problems is due to scope, reinitialization, and event loop structure. So here's to get your "hold key to start game" idea working:
First of all, avoid using too many global variables. Use a Game class to encapsulate the state and logic. Globals will always give you trouble as the program grows.
Now, keep the main loop separate by using this structure:
First of all, avoid using too many global variables. Use a Game class to encapsulate the state and logic. Globals will always give you trouble as the program grows.
Now, keep the main loop separate by using this structure:
def main(): global running while running: keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if keys[pygame.K_f]: # When F is held game_loop() screen.fill(BLACK) font = pygame.font.Font(None, 40) message = font.render("Hold 'F' to Start", True, WHITE) screen.blit(message, (WIDTH//2 - message.get_width()//2, HEIGHT//2)) pygame.display.flip() clock.tick(60)Then call main() at the bottom of your script:
if __name__ == "__main__": main() pygame.quit()