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
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
- 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
You need the Odin compiler installed on your system. Visit odin-lang.org for installation instructions.
-
Clone or download the project:
git clone https://github.com/pmbanugo/snake_game.odin cd snake_game.odin -
Compile the game:
odin build main.odin
-
Run the executable:
./snake_game
Or compile and run in one command:
odin run main.odin| 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) |
- Control the snake to eat food (red circles)
- Avoid hitting the walls or your own body
- Grow longer each time you eat food
- Score points - 10 points per food item
- Game over when you collide with walls or yourself
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
- 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
- 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
snake_game.odin/
โโโ main.odin # Main game source code
โโโ snake_game # Compiled executable
โโโ .gitignore # Git ignore file
โโโ README.md # This documentation
- 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
You can easily modify:
- Game dimensions: Change
WIDTHandHEIGHTconstants inmain() - Game speed: Adjust
tick_ratein the main loop - Colors: Modify ANSI color codes in rendering functions
- Starting position: Change snake initialization in
init_game()
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
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)
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
This project is open source. Feel free to learn from, modify, and distribute the code.
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! ๐ฎ