Small Steps II — Making Things
Make a Small Game
Make a Small Game
Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.
Combining what you learned into a small game
What you will learn
A game receives input, changes its state, and draws that state. This lesson is an assembly activity rather than new syntax.
Try it
void small_main()
{
Window window;
window.set_title("Mini Pong");
window.open(800, 500);
double paddle_y = 210;
double ball_x = 400;
double ball_y = 250;
double ball_vx = 260;
double ball_vy = 180;
StopWatch watch;
while (window.is_open())
{
double dt = watch.elapsed();
watch.reset();
if (window.key_down(Key::Up))
paddle_y = paddle_y - 300 * dt;
if (window.key_down(Key::Down))
paddle_y = paddle_y + 300 * dt;
ball_x = ball_x + ball_vx * dt;
ball_y = ball_y + ball_vy * dt;
if (ball_y < 10)
{
ball_y = 10;
ball_vy = -ball_vy;
}
if (ball_y > 490)
{
ball_y = 490;
ball_vy = -ball_vy;
}
if (ball_x < 50 && ball_x > 30 &&
ball_y > paddle_y && ball_y < paddle_y + 80)
{
ball_x = 50;
ball_vx = -ball_vx;
play_sound(Sound::Hit);
}
if (ball_x > 790)
{
ball_x = 790;
ball_vx = -ball_vx;
}
if (ball_x < 0)
{
ball_x = 400;
ball_y = 250;
ball_vx = 260;
play_sound(Sound::Lose);
}
window.clear(Black);
window.fill_rectangle(30, paddle_y, 15, 80, White);
window.fill_circle(ball_x, ball_y, 10, Yellow);
window.show();
}
}
Run the example and move the paddle with the up and down arrow keys. The code reads time and input, updates positions, handles collisions, then draws.
A collision check asks whether objects touch. After crossing a wall, the program corrects the position to the boundary as well as reversing direction. Start by finding the paddle movement instead of changing everything at once.
Adding a score to a game
What you will learn
Game rules determine how state changes when an event occurs. Increase the score only when the ball is caught.
Try it
void small_main()
{
Window window;
window.set_title("Catch the Ball");
window.open(640, 480);
double player_x = 320;
double ball_x = 100;
double ball_y = 80;
double ball_vx = 180;
double ball_vy = 140;
int score = 0;
StopWatch watch;
while (window.is_open())
{
double dt = watch.elapsed();
watch.reset();
if (window.key_down(Key::Left)) player_x = player_x - 250 * dt;
if (window.key_down(Key::Right)) player_x = player_x + 250 * dt;
ball_x = ball_x + ball_vx * dt;
ball_y = ball_y + ball_vy * dt;
if (ball_x < 15)
{
ball_x = 15;
ball_vx = -ball_vx;
}
if (ball_x > 625)
{
ball_x = 625;
ball_vx = -ball_vx;
}
if (ball_y < 15)
{
ball_y = 15;
ball_vy = -ball_vy;
}
if (ball_y > 430 && ball_y < 460 &&
ball_x > player_x - 60 && ball_x < player_x + 60)
{
ball_y = 430;
ball_vy = -ball_vy;
score = score + 1;
play_sound(Sound::Coin);
}
if (ball_y > 500)
{
ball_x = 100;
ball_y = 80;
}
window.set_title("Score: ", score);
window.clear(Black);
window.fill_rectangle(player_x - 60, 450, 120, 12, White);
window.fill_circle(ball_x, ball_y, 15, Cyan);
window.show();
}
}
Move the paddle with the left and right arrow keys. When the ball touches the paddle's range, it bounces and score increases by 1. set_title displays the current score in the window title.
After a miss, only the ball returns to its starting position. Test both hits and misses. Diagnose problems by separating position updates, conditions, and score changes.
Exercise
The starter isolates paddle movement from the first Pong example. Add code to keep paddle_y between 0 and 420. The entire paddle must stay inside the window even when an arrow key is held. You can also transfer the tested limit code to the first Pong example.
void small_main()
{
Window window;
window.open(800, 500);
double paddle_y = 210;
StopWatch watch;
while (window.is_open())
{
double dt = watch.elapsed();
watch.reset();
if (window.key_down(Key::Up))
paddle_y = paddle_y - 300 * dt;
if (window.key_down(Key::Down))
paddle_y = paddle_y + 300 * dt;
// Keep paddle_y between 0 and 420.
window.clear(Black);
window.fill_rectangle(30, paddle_y, 15, 80, White);
window.show();
}
}
Hint
After updating paddle_y from input, use two if statements to keep it within 0 and 420.
show solution
void small_main()
{
Window window;
window.open(800, 500);
double paddle_y = 210;
StopWatch watch;
while (window.is_open())
{
double dt = watch.elapsed();
watch.reset();
if (window.key_down(Key::Up))
paddle_y = paddle_y - 300 * dt;
if (window.key_down(Key::Down))
paddle_y = paddle_y + 300 * dt;
if (paddle_y < 0) paddle_y = 0;
if (paddle_y > 420) paddle_y = 420;
window.clear(Black);
window.fill_rectangle(30, paddle_y, 15, 80, White);
window.show();
}
}
Try More (Optional)
Exercise
Move the starter's paddle with the left and right arrow keys. Increase score by 1 only when the ball hits it, and show it with window.set_title("Score: ", score). Moving the paddle aside and missing must not increase the score. Collision detection is provided in the starter.
void small_main()
{
Window window;
window.open(640, 480);
int score = 0;
double paddle_x = 320;
double ball_x = 320;
double ball_y = 100;
double ball_vy = 180;
StopWatch watch;
while (window.is_open())
{
double dt = watch.elapsed();
watch.reset();
if (window.key_down(Key::Left)) { paddle_x = paddle_x - 250 * dt; }
if (window.key_down(Key::Right)) { paddle_x = paddle_x + 250 * dt; }
if (paddle_x < 70) { paddle_x = 70; }
if (paddle_x > 570) { paddle_x = 570; }
double previous_y = ball_y;
ball_y = ball_y + ball_vy * dt;
if (ball_vy > 0 && previous_y <= 430 && ball_y >= 430 &&
ball_x >= paddle_x - 70 && ball_x <= paddle_x + 70)
{
ball_y = 430;
ball_vy = -ball_vy;
// Increase score only on a paddle hit.
play_sound(Sound::Hit);
}
if (ball_y < 20) { ball_y = 20; ball_vy = -ball_vy; }
if (ball_y > 500) { ball_y = 100; ball_vy = 180; }
// show score in the window title.
window.clear(Black);
window.fill_rectangle(paddle_x - 70, 450, 140, 10, White);
window.fill_circle(ball_x, ball_y, 20, Yellow);
window.show();
}
}
Hint
Create score with value 0 outside the loop, and increase it inside the collision if.
show solution
void small_main()
{
Window window;
window.open(640, 480);
int score = 0;
double paddle_x = 320;
double ball_x = 320;
double ball_y = 100;
double ball_vy = 180;
StopWatch watch;
while (window.is_open())
{
double dt = watch.elapsed();
watch.reset();
if (window.key_down(Key::Left)) { paddle_x = paddle_x - 250 * dt; }
if (window.key_down(Key::Right)) { paddle_x = paddle_x + 250 * dt; }
if (paddle_x < 70) { paddle_x = 70; }
if (paddle_x > 570) { paddle_x = 570; }
double previous_y = ball_y;
ball_y = ball_y + ball_vy * dt;
if (ball_vy > 0 && previous_y <= 430 && ball_y >= 430 &&
ball_x >= paddle_x - 70 && ball_x <= paddle_x + 70)
{
ball_y = 430;
ball_vy = -ball_vy;
score = score + 1;
play_sound(Sound::Hit);
}
if (ball_y < 20) { ball_y = 20; ball_vy = -ball_vy; }
if (ball_y > 500) { ball_y = 100; ball_vy = 180; }
window.set_title("Score: ", score);
window.clear(Black);
window.fill_rectangle(paddle_x - 70, 450, 140, 10, White);
window.fill_circle(ball_x, ball_y, 20, Yellow);
window.show();
}
}