
Table of Contents
1. Introduction
The Rust Pong Game is a perfect beginner project for anyone interested in learning Rust game development. Pong, one of the first video games ever made, was originally released in 1972 by Atari. With just two paddles and a ball, it captured the attention of millions and became a cultural icon.
Today, Pong remains a fantastic entry point for aspiring game developers. Its simple rules and mechanics make it a great starting project to practice programming concepts such as:
- Real-time rendering
- Collision detection
- User input handling
- Game loops
In this tutorial, you’ll learn how to create a Rust Pong Game using the macroquad game framework. By the end, you’ll have a fully playable Pong game that runs on desktop and, with a few extra steps, even in a web browser via WebAssembly.
2. Why Rust and Macroquad?
Before we start coding, let’s address the big question: why choose Rust for game development?
- Performance – Rust compiles to highly optimized machine code, making it as fast as C++.
- Memory Safety – Rust’s ownership system prevents memory leaks and unsafe data handling.
- Cross-platform Support – With macroquad, you can build games for desktop, mobile, and WebAssembly.
- Growing Ecosystem – The Rust game development scene is growing fast, with engines like Bevy, ggez, and macroquad offering different levels of complexity.
Why macroquad for this Rust Pong Game?
- Simple API and easy setup
- Runs without complex configuration
- No heavy dependencies
- Works well for 2D games and prototypes
3. Setting Up the Development Environment
Before you can create the Rust Pong Game, you need to set up your development environment.
Step 1: Install Rust
If you don’t have Rust installed yet, visit Rust’s official installation page and follow the instructions for your operating system. The recommended way is to use rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Step 2: Create a New Project
We’ll create a new Rust project called pong_rust
:
cargo new pong_rust --bin
cd pong_rust
Step 3: Add macroquad as a Dependency
Use Cargo to add macroquad
:
cargo add macroquad
Now you’re ready to start coding your Rust Pong Game.
4. Building the Rust Pong Game
Our Pong game will consist of:
- Paddles – One for each player.
- Ball – Moves across the screen, bouncing off walls and paddles.
- Game Loop – Updates game state and draws objects each frame.
Here’s the complete main.rs
file:
use macroquad::prelude::*;
struct Paddle {
x: f32,
y: f32,
width: f32,
height: f32,
}
impl Paddle {
fn draw(&self) {
draw_rectangle(self.x, self.y, self.width, self.height, WHITE);
}
}
struct Ball {
x: f32,
y: f32,
radius: f32,
dx: f32,
dy: f32,
}
impl Ball {
fn draw(&self) {
draw_circle(self.x, self.y, self.radius, WHITE);
}
}
#[macroquad::main("Pong Game")]
async fn main() {
let screen_w = screen_width();
let screen_h = screen_height();
let mut left_paddle = Paddle { x: 30.0, y: screen_h / 2.0 - 40.0, width: 20.0, height: 80.0 };
let mut right_paddle = Paddle { x: screen_w - 50.0, y: screen_h / 2.0 - 40.0, width: 20.0, height: 80.0 };
let mut ball = Ball { x: screen_w / 2.0, y: screen_h / 2.0, radius: 10.0, dx: 4.0, dy: 4.0 };
loop {
clear_background(BLACK);
// Paddle movement
if is_key_down(KeyCode::W) { left_paddle.y -= 5.0; }
if is_key_down(KeyCode::S) { left_paddle.y += 5.0; }
if is_key_down(KeyCode::Up) { right_paddle.y -= 5.0; }
if is_key_down(KeyCode::Down) { right_paddle.y += 5.0; }
// Ball movement
ball.x += ball.dx;
ball.y += ball.dy;
// Wall collision
if ball.y - ball.radius <= 0.0 || ball.y + ball.radius >= screen_h {
ball.dy = -ball.dy;
}
// Paddle collision
if ball.x - ball.radius <= left_paddle.x + left_paddle.width &&
ball.y >= left_paddle.y &&
ball.y <= left_paddle.y + left_paddle.height {
ball.dx = -ball.dx;
}
if ball.x + ball.radius >= right_paddle.x &&
ball.y >= right_paddle.y &&
ball.y <= right_paddle.y + right_paddle.height {
ball.dx = -ball.dx;
}
// Draw everything
left_paddle.draw();
right_paddle.draw();
ball.draw();
next_frame().await;
}
}
5. Code Walkthrough
Paddle Structure
The Paddle
struct stores:
- x, y: Position
- width, height: Dimensions
It also has a draw()
method that renders it using macroquad’s draw_rectangle()
.
Ball Structure
The Ball
struct stores:
- x, y: Position
- radius: Size of the ball
- dx, dy: Velocity components
Its draw()
method renders a circle using draw_circle()
.
Game Loop
The main loop:
- Clears the screen with
clear_background(BLACK)
- Handles paddle movement using key presses (
W
,S
,Up
,Down
) - Updates ball position each frame
- Checks for wall collisions – inverts
dy
if hitting top or bottom - Checks for paddle collisions – inverts
dx
if hitting a paddle - Draws all objects
- Waits for the next frame using
next_frame().await
6. Running the Rust Pong Game
To run the game:
cargo run
You’ll see a black window with two white paddles and a ball. Player 1 controls the left paddle with W/S, and Player 2 controls the right paddle with ↑/↓.
7. Expanding the Game
The current Rust Pong Game is fully playable, but you can enhance it by:
- Adding a score system that displays points for each player.
- Implementing AI to control the right paddle for single-player mode.
- Increasing ball speed over time for higher difficulty.
- Exporting to WebAssembly so people can play it in their browser.
8. Why This Project is Great for Beginners
The Rust Pong Game teaches:
- Basic Rust syntax
- Structs and methods
- Game loop logic
- Simple collision detection
- Drawing shapes
Even if you’re completely new to Rust, this project is approachable and rewarding.
9. Conclusion
The Rust Pong Game with macroquad is an excellent introduction to both Rust programming and 2D game development. It’s lightweight, easy to understand, and can be expanded with new features.
With only a few lines of code, you can bring an arcade classic back to life—this time running on one of the most modern and safe programming languages available.
Useful Links:
Alright ✅
Here’s the WebAssembly (WASM) compilation section you can directly add to the blog post so readers can run the Rust Pong Game in their browser.
10. Compiling the Rust Pong Game to WebAssembly
One of the advantages of using macroquad is that it can easily target WebAssembly (WASM), allowing your game to run directly in a web browser. This is a fantastic way to let people play your game without downloading anything.
Step 1: Install the WASM Target
You need to tell Rust to compile for the WebAssembly target:
rustup target add wasm32-unknown-unknown
Step 2: Install basic-http-server
For local testing, you’ll need a simple HTTP server to host the game files:
cargo install basic-http-server
Step 3: Build the Game for WebAssembly
Use Cargo to build your Rust Pong Game for the WASM target:
cargo build --target wasm32-unknown-unknown --release
This will create a .wasm
file in:
target/wasm32-unknown-unknown/release/pong_rust.wasm
Step 4: Use macroquad
’s HTML Template
macroquad provides a ready-to-use HTML and JavaScript loader for your WASM file.
- Go to the
macroquad
GitHub repo: macroquad Web Template - Copy the
index.html
andmq_js_bundle.js
files into your project’sdist
folder. - Place your
.wasm
build in the same folder. - In
index.html
, change the WASM file path to match your game:
<script>
load("pong_rust.wasm");
</script>
Step 5: Serve the Game Locally
Run the local server:
basic-http-server ./dist
Open your browser at:
http://127.0.0.1:4000
You should see your Rust Pong Game running right in your browser.
Step 6: Deploy Online
Once tested locally, you can upload the dist
folder to any static hosting service:
- GitHub Pages
- Netlify
- Vercel
- Cloudflare Pages
This way, anyone with a browser can play your game instantly.
11. FAQ – Rust Pong Game
1. What is the Rust Pong Game?
The Rust Pong Game is a recreation of the classic Pong arcade game, built using the Rust programming language and the macroquad game framework.
2. Why build Pong in Rust?
Because Rust offers high performance, memory safety, and cross-platform support, making it ideal for learning game development basics while producing a reliable result.
3. What is macroquad?
macroquad is a simple and lightweight Rust game framework for creating 2D games on desktop, mobile, and WebAssembly.
4. Do I need prior Rust experience to make the Rust Pong Game?
Not necessarily. Pong is simple enough that beginners can follow along and learn as they go.
5. How long does it take to build the Rust Pong Game?
If you follow this guide, you can build a basic version in just a few hours.
6. Can I run the Rust Pong Game in my browser?
Yes, by compiling it to WebAssembly (WASM), you can play it directly in a web browser.
7. What are the controls for the Rust Pong Game?
By default, W/S control the left paddle and the Up/Down arrow keys control the right paddle.
8. Can I add AI to the Rust Pong Game?
Absolutely. You can implement AI for the right paddle so you can play against the computer.
9. How can I make the Rust Pong Game harder?
Increase the ball speed gradually or reduce paddle size for added difficulty.
10. Does the Rust Pong Game support scoring?
The base version here doesn’t track scores, but you can easily add score counters for each player.
11. Can I change the game’s graphics?
Yes. macroquad lets you draw shapes, load images, and customize colors for paddles, balls, and backgrounds.
12. What platforms can the Rust Pong Game run on?
It can run on Windows, macOS, Linux, and WebAssembly for browsers.
13. How do I compile the Rust Pong Game to WebAssembly?
Install the wasm32-unknown-unknown
target, build with Cargo, and use macroquad’s web template to load it in a browser.
14. Can I publish the Rust Pong Game online?
Yes. You can host it on GitHub Pages, Netlify, or other static site hosts.
15. Is macroquad the only option for the Rust Pong Game?
No. You could use ggez, Bevy, or other Rust game frameworks, but macroquad is the simplest for 2D games.
16. Will the Rust Pong Game run on low-end hardware?
Yes. macroquad is lightweight and the game’s requirements are minimal.
17. Can I make the Rust Pong Game multiplayer over the internet?
It’s possible, but you’d need to add networking code using crates like tokio
or bevy_websocket
.
18. Does macroquad have built-in physics for the Rust Pong Game?
macroquad doesn’t have a full physics engine, but it provides enough tools for basic collision detection.
19. Can I use the Rust Pong Game as a portfolio project?
Definitely. It’s a great beginner-friendly project to showcase game development skills.
20. How can I add sound effects to the Rust Pong Game?
macroquad supports audio playback, so you can add paddle hit and score sounds.
21. What resolution does the Rust Pong Game use?
By default, it adapts to your window size, but you can set a fixed resolution.
22. Where can I learn more about Rust game development after making the Rust Pong Game?
You can explore macroquad’s GitHub examples, Bevy’s ECS-based approach, or ggez’s SDL2-based framework for more complex games.
Further Reading: Building Dynamic Systems in Rust
If you’re enjoying building your Rust Pong Game and are curious about expanding your understanding of dynamic logic and world-building in Rust, be sure to check out this excellent tutorial series:
Rust MUD Game Essentials – Part 1: Build a Dynamic Map System for Logical Thinking
This article walks you through designing a flexible, scalable map system in Rust—complete with modular room definitions, logical flowcharts, and commented code. It’s a fantastic companion read if you’re thinking about branching from simple 2D game loops to more complex, logic-driven systems. an4t.com+1an4t.com+2an4t.com+2