๐ Episode 1 ยท Beginner ยท No C++ Experience Needed
Compile & Play! Pong in C++
Your first C++ game with the SFML library: two paddles, a bouncing ball, scores and an AI. C++ powers Unreal, most AAA studios and every console, and it starts right here with Pong.
๐ถ Ages 12+โฑ๏ธ ~2 Hoursโ๏ธ C++ & SFMLโ Free
๐งฐ SFML setup๐ Game loop๐ช Shapes & drawingโจ๏ธ Real-time inputโฑ๏ธ Delta time๐ค Simple AI
Get a C++ toolchain and SFML installed, and compile a first window.
1Install a compiler: on Windows the easiest is MSYS2 (which gives you g++), or Visual Studio Community. Mac: xcode-select --install. Linux: sudo apt install g++.
2Install SFML: MSYS2 โ pacman -S mingw-w64-ucrt-x86_64-sfml; Mac โ brew install sfml; Linux โ sudo apt install libsfml-dev.
5A dark 800ร500 window should appear. If it does, your entire toolchain works, the hardest step of C++ is behind you!
pong.cpp
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 500), "C++ Pong");
window.setFramerateLimit(60);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
window.clear(sf::Color(15, 18, 32));
window.display();
}
return 0;
}
๐จโ๐ง
Parent note: Everything here is free and open-source. The compile command looks scary but never changes, save it in a build.bat / build.sh file and forget it.
โ๏ธ
Fill in the Blanks
+15 XP
C++ must be before it runs, unlike Python. The three SFML libraries we link are graphics, window and .
๐ง
Knowledge Check
+15 XP
What does -lsfml-graphics in the compile command do?
ANames the output file
BLinks your program against the SFML graphics library so window/draw calls exist
CTurns on fast graphics mode
2
๐
Paddles & Real-Time Input
RectangleShapes moved with isKeyPressed
Locked
๐ฏ
Goal for this step
Draw two paddles and move them with W/S and Up/Down.
1SFML gives us sf::RectangleShape, set a size, a position and a colour.
2Real-time input is one call: sf::Keyboard::isKeyPressed(sf::Keyboard::W), true while held.
3Move paddles with shape.move(0, ยฑspeed), then clamp using getPosition().
4Draw between clear() and display(): window.draw(paddle1);
pong.cpp
sf::RectangleShape paddle1(sf::Vector2f(14, 90));
paddle1.setPosition(20, 205);
paddle1.setFillColor(sf::Color::White);
sf::RectangleShape paddle2 = paddle1;
paddle2.setPosition(766, 205);
// inside the loop, before drawing:
float speed = 6.f;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) paddle1.move(0, -speed);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) paddle1.move(0, speed);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) paddle2.move(0, -speed);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) paddle2.move(0, speed);
window.draw(paddle1);
window.draw(paddle2);
โจ๏ธ
Code Challenge
+20 XP
Move paddle 1 with W and S:
pong.cpp
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
paddle1.move(0, );
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
paddle1.move(0, );
๐ก Hint: W moves up (negative y), S moves down (positive y). move() takes (dx, dy).
๐ง
Knowledge Check
+15 XP
paddle2 = paddle1; then repositioning it works becauseโฆ
AThey share one shape
BC++ copies the whole object, paddle2 is an independent clone with the same size and colour
CSFML links them
3
โช
The Ball & Delta Time
Frame-rate independent movement with sf::Clock
Locked
๐ฏ
Goal for this step
Move the ball using delta time, the professional way.
1Add a sf::CircleShape ball(7) and velocity floats bvx = 300, bvy = 180, in pixels per SECOND now, not per frame.
2An sf::Clock measures the time each frame took: float dt = clock.restart().asSeconds();
3Move by velocity ร dt, on a 60 FPS machine or a 144 FPS gaming rig, the ball covers the same distance per second.