Skip to content

🐛 [Bug]: Snake-Game crashes with UnboundLocalError when game state changes #1309

Description

@jatingow

📝 Description

game_loop() in Snake-Game.py reads and reassigns game_state, score, level, speed, and high_score without declaring them as global inside that nested function. Because Python sees an assignment to these names somewhere in the function body, it treats them as local variables for the entire function — so the very first read of game_state (before any local assignment has happened) raises an UnboundLocalError. This crashes the game almost immediately after it starts running.

🪜 Steps to Reproduce

  1. Go to games/Snake-Game/
  2. Run script python3 Snake-Game.py
  3. Press SPACEBAR to start the game, then either let the snake hit the border/itself, or eat food to trigger a score update
  4. See error

🎯 Expected Behavior

The snake game should run normally — tracking score, level, speed, and game state changes (IDLE → PLAYING → PAUSED/GAME_OVER) without crashing.

💻 Environment

  • OS: [e.g. Windows 11, macOS, Ubuntu]
  • Python Version: [e.g. 3.10.x]
  • File Name: games/Snake-Game/Snake-Game.py

  • I am a registered GSSoC 2026 contributor.
  • I want to be assigned to fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions