An open API service indexing awesome lists of open source software.

https://github.com/xsa-dev/snake_macroquad


https://github.com/xsa-dev/snake_macroquad

demo macroquad rust snake

Last synced: 6 days ago
JSON representation

Awesome Lists containing this project

README

          

# Snake - Matrix Style

A retro-style Snake game built with Rust and Macroquad, featuring a Matrix-inspired aesthetic with procedural map generation and dynamic gameplay.

![Game Over Screen](img/game_over2.png)

## Features

### 🎮 Core Gameplay
- **Classic Snake mechanics** with smooth movement and collision detection
- **Procedural map generation** with configurable wall density
- **Configurable game speed** for different difficulty levels
- **Score tracking** with persistent best score storage

### 🎨 Visual Design
- **Matrix-inspired aesthetic** with green color palette
- **Dynamic glyph rendering** using Matrix-style characters (0, 1, <, >, [, ], etc.)
- **Animated Matrix rain background** for immersive atmosphere
- **Responsive scaling** that adapts to different screen sizes
- **Fullscreen mode** with high DPI support for immersive gameplay
- **Adaptive UI** with centered text and dynamic scaling for all screen resolutions
- **Map preview in lobby** with animated snake demo showing current difficulty settings

### 🔊 Audio
- **Procedurally generated sound effects** using WAV synthesis
- **Eat sound** (880Hz tone) when consuming food
- **Death sound** (110Hz tone) when game ends
- **Adjustable volume** with dedicated settings screen
- **Persistent volume settings** saved across game sessions

### 💾 Data Persistence
- **Save system** using JSON for game settings and high scores
- **Persistent configuration** of last used seed, wall density, and speed
- **Best score tracking** across game sessions
- **Sound volume persistence** with automatic restoration on launch

## Controls

### Global
- **Q** - Quit the game (from any screen)

### In-Game
- **Arrow Keys** or **WASD** - Move the snake
- **R** - Restart game (when game over)

### In Lobby
- **Enter** - Start new game (when "Start" is selected)
- **↑ / ↓** - Navigate menu items
- **← / →** - Adjust selected setting (wall density or speed)
- **R** - Generate new random seed
- **- / +** - Decrease/Increase wall density (0-35%)
- **[ / ]** - Decrease/Increase game speed (50-350ms)
- **S** - Open settings screen

### In Settings
- **← / →** or **- / +** - Adjust sound volume (0-100%)
- **M** - Toggle mute/unmute
- **Enter** or **Esc** - Return to lobby

### Game Over
- **R** - Restart game with same settings
- **Enter** - Return to lobby
- **Q** - Quit game

## Technical Details

### Architecture
- **State-based design** with four main screens: Lobby, Settings, Playing, GameOver
- **Modular components** for map generation, snake logic, and rendering
- **Efficient collision detection** using HashSet for wall positions
- **Deterministic map generation** using seeded random number generation
- **Dynamic screen management** with smooth transitions between states
- **Adaptive rendering** using screen dimensions for multi-resolution support

### Performance
- **60 FPS target** with smooth frame timing
- **Efficient rendering** with glyph-based graphics
- **Memory-efficient** data structures for game state

### Dependencies
- **macroquad 0.4** - Cross-platform game framework
- **serde** - Serialization for save data
- **serde_json** - JSON format support

## Installation & Running

### Prerequisites
- Rust 1.70+ with Cargo
- OpenGL 3.3+ compatible graphics card

### Build & Run
```bash
# Clone the repository
git clone
cd snake_macroquad

# Run the game
cargo run --release
```

### Development
```bash
# Run in debug mode
cargo run

# Build for release
cargo build --release
```

## Game Configuration

### Map Generation
- **Seed-based generation** ensures reproducible maps
- **Wall density** controls difficulty (0-35% of cells)
- **Safe spawn area** prevents immediate collision

### Gameplay Settings
- **Move interval** controls snake speed (50-350ms)
- **Grid size** is fixed at 32x24 tiles
- **Tile size** scales with screen resolution
- **Sound volume** adjustable from 0-100% (persisted across sessions)

### Display Settings
- **Fullscreen mode** enabled by default
- **High DPI support** for crisp rendering on high-resolution displays
- **Adaptive scaling** ensures proper display on any screen size

## File Structure

```
snake_macroquad/
├── src/
│ └── main.rs # Main game logic and rendering
├── img/
│ └── game_over.png # Screenshot for documentation
├── Cargo.toml # Project dependencies
├── Cargo.lock # Dependency lock file
├── snake_save.json # Persistent save data (auto-generated)
├── CHANGELOG.md # Project changelog
└── README.md # This file
```

## Code Highlights

### Matrix Glyph System
```rust
const MATRIX_GLYPHS: &[u8] = b"01<>[]{}()/\\|-=+*;:.,^~ABCDEFGHIJKLMNOPQRSTUVWXYZ";

fn matrix_char_for_cell(c: Cell) -> char {
let hx = (c.x as i64).wrapping_mul(73_856_093);
let hy = (c.y as i64).wrapping_mul(19_349_663);
let h = (hx ^ hy).unsigned_abs() as usize;
MATRIX_GLYPHS[h % MATRIX_GLYPHS.len()] as char
}
```

### Procedural Map Generation
```rust
fn generate(seed: u64, wall_density: f32) -> Self {
macroquad::rand::srand(seed);
// Generate border walls and random interior walls
// Ensure safe spawn area around center
}
```

### Sound Generation
```rust
fn generate_wav_sine(frequency_hz: f32, duration_seconds: f32, volume: f32) -> Vec {
// Generate WAV file data for simple sine wave tones
}
```

## Contributing

Feel free to submit issues, feature requests, or pull requests to improve the game!

## License

This project is open source. Feel free to use and modify as needed.

---

*Built with ❤️ using Rust and Macroquad*