š®
Hands-on Project
Guessing Game
Interactive number guessing game with input validation
Rust Game Project
A simple 2D game built with ggez game engine, demonstrating practical Rust concepts for game development.
Concepts Covered
This project demonstrates several important Rust concepts for game development:
- Game Engine Usage with
ggez - 2D Graphics Programming
- Game Loop Implementation
- Input Handling with keyboard events
- Vector Mathematics with
glamcrate - Random Number Generation with
randcrate - State Management in games
- Collision Detection
Project Structure
game/
āāā Cargo.toml
āāā src/
ā āāā main.rs
āāā resources/
āāā (optional asset files)Setup
Make sure you have Rust and Cargo installed:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shNavigate to the project directory:
cd rust-tutorial/projects/gameBuild and run the project:
cargo run
Game Mechanics
This is a simple "dodge the enemies" game where:
- You control a green square at the bottom of the screen
- Red squares fall from the top of the screen as enemies
- You move left and right to avoid the enemies
- Your score increases the longer you survive
- If an enemy touches you, the game ends
- Press 'R' to restart after game over
- Press 'Escape' to quit the game
Controls
- Left Arrow Key: Move player left
- Right Arrow Key: Move player right
- R Key: Restart game (when game over)
- Escape Key: Quit game
How It Works
The game uses ggez as the game engine which provides a simple game loop and graphics rendering capabilities.
Key Components
- GameState struct - Manages the game state including player position, enemies, score, and game over status
- Enemy struct - Represents enemies with position and velocity
- Game Loop - Implements the update and draw methods for the game loop
- Input Handling - Processes keyboard input for player movement and game controls
- Graphics Rendering - Draws the player, enemies, and score to the screen
- Collision Detection - Checks for collisions between player and enemies
Game Loop
The game loop consists of two main methods:
update()- Updates game logic (enemy movement, collision detection, etc.)draw()- Renders graphics to the screen
Running Tests
To run the tests for this project:
cargo testBuilding for Release
To build an optimized version for release:
cargo build --releaseThe executable will be in target/release/rust-tutorial-game.
Extending the Project
Possible extensions for this project:
- Add sound effects using
ggezaudio capabilities - Implement different enemy types with varying behaviors
- Add power-ups and collectibles
- Implement a high score system
- Add more complex graphics with sprites
- Implement multiple levels with increasing difficulty
- Add a main menu and game states
- Implement particle effects for explosions