o
odinpkg.dev
packages / app / snake_game.odin

snake_game.odin

92fca55app

Snake game (like in Nokia phones) built to run in terminal (for fun)

No license ยท updated 5 months ago

Snake Game ๐Ÿ

A terminal-based snake game written in Odin, featuring smooth gameplay, colorful graphics, and responsive controls.

Screen.Recording.2026-01-25.at.01.05.57.mov

Why This Project

This snake game was created to:

  • Learn Odin Programming: Explore the Odin programming language through a practical, fun project
  • Practice Data-Oriented Design: Implement efficient game logic using structured data types
  • Terminal Graphics: Create an engaging visual experience using ANSI escape sequences
  • Game Development: Understand core game mechanics like collision detection, input handling, and game loops

Features

  • Smooth Gameplay: Fixed-timestep game loop for consistent movement
  • Colorful Graphics: ANSI color codes for visual appeal (green snake, red food, gray borders)
  • Responsive Controls: Support for both arrow keys and WASD
  • Score System: Track your progress with points for each food item
  • Game States: Proper game over and restart functionality
  • Clean Terminal: Automatic cursor hiding and screen clearing

How to Run

Prerequisites

You need the Odin compiler installed on your system. Visit odin-lang.org for installation instructions.

Build and Run

  1. Clone or download the project:

    git clone https://github.com/pmbanugo/snake_game.odin
    cd snake_game.odin
  2. Compile the game:

    odin build main.odin
  3. Run the executable:

    ./snake_game

Or compile and run in one command:

odin run main.odin

Controls

Action Keys
Move Up โ†‘ Arrow or W
Move Down โ†“ Arrow or S
Move Left โ† Arrow or A
Move Right โ†’ Arrow or D
Quit Game Q
Restart R (when game over)

Game Rules

  1. Control the snake to eat food (red circles)
  2. Avoid hitting the walls or your own body
  3. Grow longer each time you eat food
  4. Score points - 10 points per food item
  5. Game over when you collide with walls or yourself

Technical Details

Architecture

The game follows a data-oriented design approach:

  • Game State: Central state structure containing snake position, direction, food location, and score
  • Terminal Handling: Raw terminal mode for direct input without buffering
  • Fixed Timestep: Consistent game timing using 120ms tick rate
  • Collision Detection: Efficient boundary and self-collision checking

Key Components

  • Vec2: 2D vector for positions
  • Direction: Enum for snake movement
  • Game_State: Main game data structure
  • Terminal Control: ANSI escape sequences for rendering
  • Input System: Non-blocking keyboard input handling

Performance

  • Memory Efficient: Dynamic arrays for snake segments
  • CPU Optimized: Small sleep intervals prevent excessive CPU usage
  • Smooth Rendering: Double buffering effect with clear screen and redraw

File Structure

snake_game.odin/
โ”œโ”€โ”€ main.odin          # Main game source code
โ”œโ”€โ”€ snake_game         # Compiled executable
โ”œโ”€โ”€ .gitignore         # Git ignore file
โ””โ”€โ”€ README.md          # This documentation

Development

Code Style

  • Data-Oriented: Structured approach with clear data types
  • Modular: Separated concerns (input, rendering, game logic)
  • Documented: Clear section headers and comments
  • Error Handling: Proper cleanup with defer statements

Customization

You can easily modify:

  • Game dimensions: Change WIDTH and HEIGHT constants in main()
  • Game speed: Adjust tick_rate in the main loop
  • Colors: Modify ANSI color codes in rendering functions
  • Starting position: Change snake initialization in init_game()

Troubleshooting

Terminal Issues

If the game doesn't display correctly:

  • Ensure your terminal supports ANSI escape sequences
  • Try resizing your terminal to at least 40x20 characters
  • Some terminals may require enabling raw mode support

Build Issues

If compilation fails:

  • Verify Odin is properly installed and in your PATH
  • Check that you're using a compatible Odin version
  • Ensure all dependencies are available (core Odin packages)

Performance

If the game feels laggy:

  • Close other applications that might be using CPU
  • Try a smaller terminal window
  • Adjust the tick rate for faster/slower gameplay

License

This project is open source. Feel free to learn from, modify, and distribute the code.

Contributing

Interested in contributing? Consider:

  • Adding new features (power-ups, obstacles, levels)
  • Improving performance or code quality
  • Adding sound effects or music
  • Creating different game modes

Enjoy playing! ๐ŸŽฎ