Neil Mahajan
Personal Project
July 2025

๐Ÿ Terminal Snake Game in Go

A high-performance, terminal-based Snake game implementation written in Go, showcasing advanced concurrency patterns, data structures, and clean architecture principles.

Go
Goroutines
Channels
Concurrency
Terminal UI
ANSI Escape Sequences
Linked Lists
CLI
Terminal
๐Ÿ SNAKE GAME ๐Ÿ
Score: 6

โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ—โ—โ—โ—โ—โ—โ— โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ™ฆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Controls: W/Kโ†‘ S/Jโ†“ A/Hโ† D/Lโ†’ Q/ESC=Quit

Project Overview

This Terminal Snake Game is a sophisticated implementation written in Go that demonstrates mastery of concurrent programming, efficient data structures, and clean architecture principles. The game features real-time terminal manipulation, non-blocking input handling, and smooth gameplay mechanics.

Built with performance and maintainability in mind, the project showcases advanced Go concepts including goroutines for concurrent input processing, channels for safe communication, and efficient data structures like linked lists for optimal snake body management.

The game supports multiple difficulty levels with configurable board sizes and speeds, responsive controls, and cross-platform compatibility across macOS, Linux, and Windows terminals.

Key Features

Performance & Concurrency

  • Goroutines & Channels: Non-blocking input handling with safe concurrent communication
  • Efficient Data Structures: Linked list implementation for O(1) head/tail operations
  • Real-time Processing: Game loop with precise timing and input processing
  • Memory Optimization: ~2MB baseline memory footprint with minimal allocations

Gameplay & UI

  • Multiple Difficulty Levels: Three board sizes (Small, Medium, Large) and configurable speeds
  • Responsive Controls: Vim-style (hjkl) and WASD movement with direction locking
  • Terminal Programming: Raw terminal manipulation and ANSI escape sequences
  • Cross-platform: Works seamlessly on macOS, Linux, and Windows

Technical Implementation

Architecture & Design Patterns

1. Concurrent Input Processing

// Non-blocking input handling with goroutines
inputChannel := make(chan types.UserInput)
stopChannel := make(chan struct)
go func() {
ย ย defer wg.Done()
ย ย input.ListenForInput(inputChannel, s, stopChannel)
}()

2. Efficient Snake Body Management

// Using container/list for O(1) head/tail operations
type Snake struct {
ย ย Bodyย ย ย ย ย ย ย ย *list.List ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย // Doubly-linked list
ย ย ShouldGrowย ย bool
}

3. Channel-based Communication

// Type-safe communication between goroutines
type UserInput struct {
ย ย Directionย ย ย string
ย ย Errorย ย ย ย ย ย ย error
}

Project Structure

snake/
โ”œโ”€โ”€ cmd/
โ”‚ย ย ย โ””โ”€โ”€ main.go
โ”œโ”€โ”€ internal/
โ”‚ย ย ย โ”œโ”€โ”€ board/
โ”‚ย ย ย โ”‚ย ย ย โ””โ”€โ”€ draw_board.go
โ”‚ย ย ย โ”œโ”€โ”€ fruit/
โ”‚ย ย ย โ”‚ย ย ย โ””โ”€โ”€ create_fruit.go
โ”‚ย ย ย โ”œโ”€โ”€ input/
โ”‚ย ย ย โ”‚ย ย ย โ”œโ”€โ”€ get_difficulty_input.go
โ”‚ย ย ย โ”‚ย ย ย โ””โ”€โ”€ listen_for_input.go
โ”‚ย ย ย โ”œโ”€โ”€ snake/
โ”‚ย ย ย โ”‚ย ย ย โ”œโ”€โ”€ move_snake.go
โ”‚ย ย ย โ”‚ย ย ย โ””โ”€โ”€ new_snake.go
โ”‚ย ย ย โ””โ”€โ”€ types/
โ”‚ย ย ย ย ย ย ย โ””โ”€โ”€ types.go
โ”œโ”€โ”€ go.mod
โ””โ”€โ”€ README.md

Performance Metrics

~2MB
Memory Footprint
<1%
CPU Usage
<16ms
Input Latency
80x40
Max Board Size

Gameplay Mechanics

Controls

W / KMove Up
S / JMove Down
A / HMove Left
D / LMove Right
Q / ESCQuit Game

Game Rules

  • Snake grows when consuming fruit (F)
  • Game ends on wall or self-collision
  • Score increases with each fruit consumed
  • Fruit spawns randomly in unoccupied positions
  • Direction locking prevents reverse movement
  • Configurable board sizes and speeds

Installation & Usage

Quick Start

# Clone the repository
git clone https://github.com/Neilsmahajan/snake.git
cd snake
# Install dependencies
go mod tidy
# Run the game
go run cmd/main.go

Build & Deploy

# Build for current platform
go build -o snake cmd/main.go
./snake
# Cross-compile for different platforms
GOOS=linux GOARCH=amd64 go build -o snake-linux cmd/main.go
GOOS=windows GOARCH=amd64 go build -o snake-windows.exe cmd/main.go

Learning Outcomes

This project demonstrates mastery of several advanced Go concepts and software engineering principles:

Go Fundamentals

  • Goroutines & concurrency patterns
  • Channel communication
  • Error handling patterns
  • Memory management
  • Interface design

Software Engineering

  • Clean architecture principles
  • Modular design patterns
  • Resource management
  • Cross-platform compatibility
  • Performance optimization

Game Screenshots

Gameplay
๐Ÿ SNAKE GAME ๐Ÿ
Score: 6

โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ—โ—โ—โ—โ—โ—โ— โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ™ฆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆ โ–ˆ
โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Controls: W/Kโ†‘ S/Jโ†“ A/Hโ† D/Lโ†’ Q/ESC=Quit
Game Setup
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ ๐Ÿ SNAKE GAME ๐Ÿ โ•‘
โ•‘ Written in Go Language โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Board Size Selection:
[s] - Small (20x10) ๐Ÿ“ฑ
[m] - Medium (40x20) ๐Ÿ’ป
[l] - Large (80x40) ๐Ÿ–ฅ๏ธ
Enter your choice: m
โœ“ Medium board selected!
Speed Selection:
[s] - Slow (200ms) ๐ŸŒ
[m] - Medium (100ms) ๐Ÿšถ
[l] - Fast (50ms) ๐Ÿƒ
Enter your choice: m
โœ“ Medium speed selected!
Press Enter to start...
Game Over
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ Game Over โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿ† Final Score: 6

๐ŸŽฎ Keep trying! You'll get better! ๐ŸŽฎ

Thanks for playing Snake! ๐Ÿ
Press ENTER to exit...
Game Controls:
โ†‘ Move Up: W or K
โ†“ Move Down: S or J
โ† Move Left: A or H
โ†’ Move Right: D or L
๐Ÿšช Quit Game: Q or ESC