Who says first article, says first game. And who says first game (on this blog) says Pong... Finally, something that will look a little bit like it.

The concept of Pong comes from.......a long time ago.

However, there is something to have fun with, in particular by modifying it a little. Pong is very nice, but either you have to find another player, or play against a wall (boring), or code an AI.

We will see a little different gameplay this time: the same player plays both paddles at the same time. I admit I even want to make one with four simultaneous paddles...

I will here use Rust and macroquad and I will try to show you that Rust is not that difficult (in many cases), but on the contrary, it can bring you a lot of things.

Macroquad is quite easy to use and removes, in my opinion, a lot of roughness from Rust. In addition, it is light and minimalist, and "cerise sur le gâteau", clearly cross-platform.

I will place myself from a beginner's point of view in Rust.

## Project setup

After creating project with cargo new --bin psycho_pong, we add the dependencies in Cargo.toml

[dependencies]


Then, we modify our main.rs file with the classical Macorquad's main function.

use macroquad::prelude::*;

const WIDTH: i32 = 480;
const HEIGHT: i32 = 480;

async fn main() {
// Game initialization are here
loop {
// Update logic
// Handle input
// Draw stuff

clear_background(BLACK);
next_frame().await;
}
}

fn window_conf() -> Conf {
Conf {
window_title: "Psycho Pong".to_owned(),
window_width: WIDTH,
window_height: HEIGHT,
fullscreen: false,
..Default::default()
}
}


Don't be afraid, Rust's async and await are just used to solve the cross platform main loop organization, nothing else.

At this point , it's not really fancy: if we compile this, we get a nice little window, black, empty, very empty, sadly empty.

Basically, we can imagine a paddle like a simple rectangle and define it like a structure:

struct Paddle {
x: f32,     // x position
y: f32,     // y position
w: f32,     // width (the x-axis size )
h: f32      // height (the y-axis size)
}


And use the macroquad's  draw_rectangle() function to draw each paddle, and add an update(paddle_one: Paddle, paddle_two: Paddle) function like this in the main loop:

async fn main() {
x: 0.5 * (WIDTH - 100) as f32,
y: 5.0,
w: 100.0,
h: 20.0};

x: 0.5 * (WIDTH - 100) as f32,
y: HEIGHT as f32 - 25.0,
w: 100.0,
h: 20.0};

loop {

// Handle input for the first paddle
if is_key_down(KeyCode::Left) {
}
else if is_key_down(KeyCode::Right) {
}

// Handle input for the second paddle
if is_key_down(KeyCode::A) {
}
else if is_key_down(KeyCode::D) {
}

clear_background(BLACK);  // Scene cleanup

draw_rectangle(
WHITE);
draw_rectangle(
WHITE);

next_frame().await;
}
}



So we have come to this...

Clearly, it's not a very elegant route, and which will quickly become tedious when it comes to managing four paddles. In addition, the movement of the paddles lacks flexibility.

## Some generalizations for our four paddles

Good. We will try to generalize this, by implementing some functionality to the Paddle structure. At the same time, we will now create four paddles. One on each edge of the screen

First, we can generalize a little more the creation of the paddles according to their position on the screen. For this, let's create an enum describing the position of the paddle and use it for the creation:


// Our enum that discribe the paddle's position
Top,
Down,
Left,
Right,
}

x: f32,
y: f32,
w: f32,
h: f32,
}
match name {
x: 0.5 * (WIDTH - 100) as f32,
y: 5.0,
w: 100.0,
h: 20.0,
name,
},
x: 0.5 * (WIDTH - 100) as f32,
y: HEIGHT as f32 - 25.0,
w: 100.0,
h: 20.0,
name,
},
x: 5.0,
y: 0.5 * (HEIGHT - 100) as f32,
w: 20.0,
h: 100.0,
name,
},
x: WIDTH as f32 - 25.0,
y: 0.5 * (HEIGHT - 100) as f32,
w: 20.0,
h: 100.0,
name,
},
}
}
}



And now, if we create four paddle, and draw them with :

async fn main() {

loop {
// .....
clear_background(BLACK);
draw_rectangle(
WHITE,
);
draw_rectangle(
WHITE,
);
draw_rectangle(
WHITE,
);
draw_rectangle(
WHITE,
);
next_frame().await
}
}


As you can see, it's a bit long... And we don't want to read code that just repeats itself. Draw...draw...draw...draw. Some code rewriting will do us the greatest good.

So let's replace all that horrible code with this:

async fn main() {
// We create our four paddles, on each side of the screen,
// stored in an Array
];

loop {
clear_background(BLACK);

// And we can iterate over our list
draw_rectangle(
WHITE);
}

next_frame().await;
}
}


It's still more readable and pleasant to see now, we have greatly reduced the previous boring repetitions. Now it's time to move it's paddle, each with the controls assigned to it. As much before we had two paddles, now we have four.

To handle the input command, we can use a classic method

// inputs for paddle one
if is_key_down(KeyCode::Left) { /* Do something */ }
else if is_key_down(KeyCode::Right) { /* Do something */ }

if is_key_down(KeyCode::A) { /* Do something */ }
else if is_key_down(KeyCode::D) { /* Do something */ }

if is_key_down(KeyCode::W) { /* Do something */ }
else if is_key_down(KeyCode::S) { /* Do something */ }

if is_key_down(KeyCode::Up) { /* Do something */ }
else if is_key_down(KeyCode::Down) { /* Do something */ }


But let's try another way. First we create a function that return the direction of motion (1.0 or -1.0) given specific KeyCode inputs

pub fn get_direction(key_a: KeyCode, key_b:KeyCode) -> f32 {
let left = if is_key_down(key_a) {1.0} else {0.0};
let right = if is_key_down(key_b) {1.0} else {0.0};

right - left
}


and use it to update the position of each paddle. But wait !! Which update function ?!? In impl Paddle{} of course...

fn update(&mut self, direction: f32) {
//The movements are different
//when the paddles are vertical or horizontal
match self.name {
}

// And the paddle need to stay in the screen
if self.x < 0.0 {
self.x = 0.0;
}
else if self.x > WIDTH as f32 - self.w {
self.x = WIDTH as f32 - self.w;
}

if self.y < 0.0 {
self.y = 0.0;
}
else if self.y > HEIGHT as f32 - self.h {
self.y = HEIGHT as f32 - self.h;
}

}


And we can use this update function in our main loop

paddle_list[0].update(get_direction(KeyCode::Left, KeyCode::Right));


As we have seen before, we can avoid some repetitions by using an iterator. But in this case, we need mutability because pressing a key will modify the position of our paddle (and therefore modify certain data specific to each paddle), so we have to use .iter_mut() instead of .iter().

So, in our main loop, we can replace the old four paddles update code with this one

for paddle in paddle_list.iter_mut() {
}


And now we have our four paddles that can move around while staying on screen...

Of course, this is just a suggestion, one way, and there are plenty of other ways to deal with this situation.

## Et Voilà !

You can test (macroquad allows to generate wasm) now >> here <<.