Example 1
What does std::cout << "HP: " << 50 * 2 << "!"; print?
- The text in quotes prints exactly as written: HP: (with a space).
- 50 * 2 is worked out first: 100.
- Then the ! follows straight after, with no space.
Answer: HP: 100!
🎮 Game dev with C++ · session 1 of 24 · 90 minutes · free
Every game you have played, from tiny phone games to Unreal Engine worlds, is a program that stores numbers, reads the player's input, makes decisions and moves things on a screen. Today you write your first C++ program and do all four.

A complete lesson · 90 minutes
Open this page with your learner and work down it, or press Teach this lesson to go through it one step at a time on a big screen. Every section has something to do.
Ages 13 to 16 · beginner to first portfolio · 90-minute lessons
Warm-up · think first
10 800 frames: 3 minutes × 60 seconds × 60 frames.
Teacher tip: Ask for a guess first. Then: if each frame takes the computer 16 milliseconds to draw, why does that matter? (That is the game loop, coming today.)
🎮 Warm-up games · 5 quick games
Five quick rounds in the game studio. Vote, say why, then show the answer.
Three levels, like a real engineer
First use what the lesson shows exactly as it is, then change one key thing and see what happens, then make your own.
Level 1 · Use it
Run this exact line in your C++ program: std::cout << "HP: " << 50 * 2 << "!"; Watch what appears on screen and point to each part that prints.
Level 2 · Tweak it
In that same HP line, change 50 to 25 and change * 2 to * 3. Predict the new HP number before you run it, then check your guess.
Level 3 · Build it
Write your own C++ program from scratch that prints a hero card: name, HP, and coins. Use std::cout and at least one maths expression, then run it and test.
🗺️ Play the Lesson QuestThree worlds, one for each level, with short stages that bring back what you miss.
Part 1 of 4 · learn, see, practise
Welcome to the studio. I am Byte, the studio robot. Games are written as text called source code, then a compiler turns that text into a program the computer can run. Let's build one.
C++ is the language most big game engines are written in: Unreal Engine, Godot's core and most console games. It is fast because a compiler (g++, clang or Microsoft's MSVC) translates your source file (main.cpp) into machine code once, before the game runs.
Every C++ program starts running in a function called main. The lines between its curly braces { } run from top to bottom. return 0; at the end tells the operating system the program finished without errors.
#include <iostream> brings in the input/output library. std::cout ("see-out") is the console output, and << sends things into it: text in double quotes, numbers and sums. \n (or std::endl) starts a new line.
Almost every statement ends with a semicolon ;. C++ is case-sensitive: Main and main are different names, and the compiler stops at the first mistake with an error message. Reading those messages calmly is a real game-developer skill.
std:: means "from the standard library". Many tutorials write using namespace std; so they can say cout instead of std::cout. Pros keep std:: to avoid name clashes, so we do too.
Game engines are libraries too: later you will write #include "raylib.h" in exactly the same way.
int main() {
std::cout << "Hello, game world!\n";
std::cout << "Level " << 1 << " loading...\n";
std::cout << "Coins: " << 20 + 5 << "\n";
return 0;
}▶ Output
Hello, game world! Level 1 loading... Coins: 25
What does std::cout << "HP: " << 50 * 2 << "!"; print?
Answer: HP: 100!
The compiler says: error: expected ';' before 'return'. What is wrong?
Answer: A missing semicolon on the line above return
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
Part 2 of 4 · learn, see, practise
A game remembers everything in variables: your health, your speed, whether you are alive, your name. In C++ every variable has a type that says what kind of value it holds.
Declare a variable with its type, a name and a starting value: int health = 100; The main types are int (whole numbers: health, score, lives), double (decimals: speed 2.5, gravity 9.8), bool (true or false: isAlive), char (one letter in single quotes: 'A') and std::string (text in double quotes, from #include <string>).
Change a variable with =, which means "store", not "equals": health = health - 35; takes the old value, subtracts 35 and stores the result. Short forms: health -= 35; score += 10; and ++level; adds one.
The type changes the maths. With two ints, / throws the decimal part away: 7 / 2 is 3, and % gives the remainder: 7 % 2 is 1. If either side is a double, you get a decimal: 7.0 / 2 is 3.5. Games use / and % all the time: tiles across a map, frames per animation, minutes and seconds on a timer.
const marks a value that must never change, like const int MAX_HEALTH = 100; The compiler then refuses any line that tries to change it, which catches bugs early.
Printing a bool shows 1 for true and 0 for false.
The order matters: double avg = (3 + 4) / 2; stores 3, because (3 + 4) / 2 is worked out with ints first. Write (3 + 4) / 2.0 to get 3.5.
Name variables clearly: playerSpeed beats ps. Names cannot start with a digit or contain spaces.
int main() {
std::string hero = "Byte";
int health = 100;
double speed = 2.5;
bool alive = true;
health = health - 35;
std::cout << hero << " has " << health << " health\n";
std::cout << "Speed x 2 = " << speed * 2 << "\n";
std::cout << "7 / 2 = " << 7 / 2 << ", 7 % 2 = " << 7 % 2 << "\n";
std::cout << "alive = " << alive << "\n";
return 0;
}▶ Output
Byte has 65 health Speed x 2 = 5 7 / 2 = 3, 7 % 2 = 1 alive = 1
A level lasts 250 seconds. Show it as minutes:seconds.
Answer: 4:10
int coins = 40; coins -= 15; coins += 3; What is coins now?
Answer: 28
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
Part 3 of 4 · learn, see, practise
A game is not a film: the player decides. std::cin reads what they type, and if, else if and else choose what happens next.
std::cin >> door; waits for the player to type a value and press Enter, then stores it in the variable door. The variable must exist first (int door;).
if (condition) { … } runs its block only when the condition is true. else if tries another condition, and else catches everything left. Only ONE block of an if / else if / else chain runs.
Conditions compare values: == equal, != not equal, <, >, <=, >=. Combine them with && (and: both must be true) and || (or: at least one). Example: if (health > 0 && hasKey) opens the door only for a living player with the key.
The most famous C++ bug: if (lives = 0) uses ONE equals sign, so it stores 0 in lives instead of comparing, and the condition is false. Always use == to compare. Good compilers warn you; read the warnings.
Every game loop is decisions on repeat: is a key pressed? did the player touch the enemy? is health 0? You will write all of these.
When you test a program with input, try every branch: a right answer, a wrong one and a silly one (like 99).
int main() {
int door;
std::cout << "Pick a door (1, 2 or 3): ";
std::cin >> door;
if (door == 2) {
std::cout << "Treasure! +50 coins\n";
} else if (door == 1 || door == 3) {
std::cout << "A slime appears!\n";
} else {
std::cout << "That door does not exist.\n";
}
return 0;
}▶ Output (when the player types 2)
Pick a door (1, 2 or 3): Treasure! +50 coins
int score = 120; if (score >= 100) std::cout << "Gold"; else std::cout << "Silver"; What prints?
Answer: Gold
int x = 4; Is (x > 2 && x < 4) true or false?
Answer: false
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
Part 4 of 4 · learn, see, practise
Here is the secret behind every moving thing in a game: two numbers. Where it is, (x, y), and how far it moves each frame, (vx, vy). Mathematicians call a pair like that a vector.
A 2D vector is an ordered pair of numbers. A sprite's position is a vector (x, y); its velocity (vx, vy) says how far it moves in one frame. In raylib, SDL2 and most 2D engines the screen's (0, 0) is the TOP-LEFT corner: x grows to the right and y grows DOWN, so a negative vy moves a sprite up.
Every frame the game adds the velocity to the position: x = x + vx; y = y + vy; Adding vectors means adding matching parts: (3, 2) + (1, −4) = (4, −2). After n frames the sprite has moved n × velocity.
Multiplying a vector by a number (scaling) multiplies both parts: 3 × (2, −1) = (6, −3). Double the velocity and the sprite goes twice as far each frame in the same direction.
Frames are not seconds. At 60 frames a second, 2 pixels a frame is 120 pixels a second. Real games multiply by the time each frame took (delta time), so the speed stays the same on fast and slow computers: coming in lesson 15.
This is linear algebra, the maths of vectors and matrices. Unit 2 adds length (Pythagoras), direction, the dot product and matrices that rotate sprites.
The loop in the program below (for) is a preview: lesson 3 explains it fully.
int main() {
int x = 2, y = 10; // position
int vx = 2, vy = -1; // velocity per frame
for (int frame = 1; frame <= 5; frame++) {
x = x + vx;
y = y + vy;
std::cout << "frame " << frame << ": (" << x << ", " << y << ")\n";
}
return 0;
}▶ Output
frame 1: (4, 9) frame 2: (6, 8) frame 3: (8, 7) frame 4: (10, 6) frame 5: (12, 5)
A sprite at (0, 0) moves with velocity (4, −2). Where is it after 3 frames?
Answer: (12, −6)
A sprite at (100, 300) must reach (160, 240) in 20 frames. What velocity does it need?
Answer: (3, −3)
The alien starts at (2, 10). Set its velocity with the arrows, then press Next frame: each frame adds the velocity to its position. Grab the coin!
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
Watch out
if (lives = 0) stores 0 and is always false. Compare with ==.
7 / 2 is 3 with ints. Make one side a double (7.0 / 2) or use / 2.0.
On screen, up is negative y. Draw a quick axis before you set vy.
Read the FIRST error only, look at the line it names and the line above, fix one thing, build again.
Class activity 1 of 3
Predict each output on paper first. Then paste the line into the ▶ Compiler Explorer program from Part 1 (inside main) and run it. Every surprise is a lesson.
Class activity 2 of 3
Open the treasure-door program from Part 3 in Compiler Explorer and make it yours. Type the answers your finished program prints.
Class activity 3 of 3
Two racers start on the same line. Work out where each one is, then decide who reaches the finish first.
After class · project
Write a C++ program that asks the player for their hero's name and a class (1 = knight, 2 = wizard), sets health, speed and coins to suit the class, prints a neat stat card, then offers three doors with a different result for each.
What to make: a console program that asks for the hero's name and class, prints a stat card and lets the player pick a door
Stretch: Add a third class, and a fourth door that only opens if coins >= 20 (use &&).
| Look for | Done well when |
|---|---|
| I | t |
| U | s |
| i | f |
| T | h |
| E | v |
Press Copy and paste it into the editor at home, or work on it right here.
▶ Output
Line 4: SyntaxError: invalid syntax
📦 Memory boxes
Homework
Try each on paper, then tap to see the answer and working. Or print the practice sheet.
14 r 2
(8, 60) | 8, 60
4
2:5
🔥 Challenge round · finished early?
Cover the notes and try these from memory. They are meant to make you think hard: draw, try small cases, and check your answer.
A sprite starts at (0, 0) and its velocity INCREASES by (0, 1) every frame (gravity), starting at (3, −6). Write its position for the first 6 frames. When does it come back to y = 0?
Talk it through or use the drawing board. There is more than one way to get there.
Quiz · 8 questions
Pick an answer to see if it is right and why. Your best score is saved on this device.
🖨 Print what we learned
Pick what to practise, then print it or choose Save as PDF in the print window to download it.
For parents and teachers
Use it as it is, or stretch any part over two sessions. Each row links to the part of the page you need.
| Time | Part | The teacher | The learner |
|---|---|---|---|
| 0–6 min | Warm-up games | Five fast rounds; take votes and reasons, not just answers. | Vote and say why. |
| 6–10 min | Warm-up | 60 × 60 × 3 on the board, then ask why speed per frame matters. | Guess, then explain. |
| 10–25 min | How a C++ program is built | Open Compiler Explorer with ▶; change the text, break a semicolon on purpose and read the error together. | Run it, change it, read one error. |
| 25–40 min | Variables and types | Run the stats program, then predict 7 / 2 before revealing. Spend time on the int-division trap. | Practise up to the stretch. |
| 40–54 min | Input and decisions | Run the doors program three times with different input (the stdin box in Compiler Explorer). | Test every branch. |
| 54–68 min | Positions and velocities | Do the vector lab tasks first, then run the five-frames program and match it to the lab. | Lab tasks, then practice. |
| 68–75 min | Activity 1: predict, then prove it | Predictions on paper first; then run. | Predict and check. |
| 75–81 min | Activity 2: the treasure doors | Learner drives the keyboard; the teacher only asks questions. | Extend the program. |
| 81–85 min | Activity 3: vector race | Quick mental maths; the lab checks one racer. | Race maths. |
| 85–89 min | Quiz | Eight questions, no running the code. | Answer and read why. |
| 89–90 min | Set the project | Show the stat-card starter and agree a due date. | Start the project. |
Pair-program: the learner types, the teacher reads the error messages aloud with them. Keep a code journal of every error and its fix.
A sprite starts at (0, 0) and its velocity INCREASES by (0, 1) every frame (gravity), starting at (3, −6). Write its position for the first 6 frames. When does it come back to y = 0?
FAQ
Not for the first lessons: Compiler Explorer runs C++ in the browser. From the raylib unit we help set up a free compiler and raylib on Windows, Mac or Linux, step by step.
Unreal Engine is written in C++, and engines make far more sense once you know the language and the vector maths underneath. The course gets to raylib prototypes and UE5, but on solid ground.
It starts with coordinates and grows slowly: vectors, distance, angles and matrices, always shown moving on screen first.
Start a conversation
Tell us the learner's age and level. A teacher from our network teaches with these lessons; we confirm timing and price first.