By the end of this lesson you will be able to name the five parts that every game has, and you will know why the speed of a moving object depends on one of them.
You need: a coding agent connected to AllModelAPI, and a browser · Time: 7 minutes
A game can look complicated, but underneath, every game that runs in a browser is made of the same five parts. Once you can see them, you stop feeling lost in someone else's code and you can ask for exactly the part you want.
- The screen. This is the drawing area inside the web page. The player sees nothing except what is drawn here.
- The loop. This is one small block of code that the browser runs again and again, many times every second. Each round of that block is called a frame. A frame is one complete drawing of the game.
- The input. This is what the player does: presses an arrow key, clicks the mouse, or taps the screen with a finger.
- The rules. These are the checks that decide what happens when two things on the screen touch each other. In a game where a basket catches falling mangoes, the rule is: when the basket and a mango overlap, the mango disappears and the score goes up by one.
- The result. This is how the player wins, loses, or starts again. Without it, the player is only watching things move.
Why?When a beginner asks an agent for "a game", the agent has to invent all five parts. When you name the five parts yourself, you are not asking for a game any more. You are asking for five small things that you can check one at a time.
The most important of the five is the loop, because it never stops. While the page is open on the screen, the browser keeps asking your game to draw the next frame.
Most screens refresh about sixty times every second, so a game that draws sixty frames every second looks smooth to the human eye. A game can also draw more frames or fewer, depending on how heavy the game is and how powerful the device is.
The five parts, and the four things that happen in every round of the loop. The loop always does the same four things, in the same order:
- Read what the player is pressing right now.
- Move the things that should move.
- Check whether anything touched anything else.
- Draw the whole scene again.
Then it starts over. This is why a game feels alive: the player presses a key, and by the next frame, the picture already shows the result.
Why?The loop is the reason a game is not a picture. A picture is drawn once; a game is drawn again and again, and every drawing is slightly different.
Here is the mistake that catches almost every beginner, and it is worth reading slowly.
If you tell the agent "move the mango down by five pixels on every frame", the mango moves five pixels per drawing. On a screen that draws sixty frames every second, the mango falls 300 pixels every second. On a screen that draws 144 frames every second, the same code moves the mango 720 pixels every second. The game becomes much faster on a better screen, and the player thinks the game is broken.
The fix is called measuring time between frames, and people also call it delta time. Instead of moving a fixed number of pixels per frame, you move by the amount of time that passed since the last frame. On a slow device and a fast device, the mango then falls at the same speed in real seconds.
What you ask for What happens "Move the mango down 5 pixels every frame" Speed depends on the screen and the device "Move the mango down [300] pixels every second" Same speed everywhere, smoother or rougher depending on the device TipWhen you write your brief later, say the speed in seconds, not in frames. Say "the mango falls about 300 pixels every second". That sentence keeps working on every device.
Take a game you already know how to play and find its five parts. Then do the same for the game you are about to build: a basket at the bottom of the screen catches mangoes that fall from the top, every caught mango gives one point, and three mangoes that hit the floor end the game.
Put in order Put one round of the game loop in the order the browser runs it
Tap the steps in the order you would do them. Tap a placed step to remove it.
- 1
Why?Doing this once with a game you already know makes the five parts obvious. Doing it with your own game is the first half of the brief you will write in the next lesson.
You do not have to work out the parts alone. Ask the agent to do it in writing, before it writes any code. This is also a cheap way to find out whether the agent understood you.
Prompt · copy it into your AI agentI am about to build a very small browser game. Do not write any code yet. The game: a basket sits at the bottom of the screen. Mangoes fall from the top at different speeds. When the basket touches a mango, the mango disappears and the score goes up by one. When three mangoes reach the floor, the game is over. Answer using these five headings, and write one or two plain sentences under each: 1. The screen 2. The loop 3. The input 4. The rules 5. The result Then answer one more question in two sentences: what should move per second rather than per frame, so the game runs at the same speed on a fast laptop and an older phone?
Why?An agent that can fill these five headings has understood the game. An agent that cannot will guess while writing code, and you will find the guess later as a bug.
- Every browser game has five parts: the screen, the loop, the input, the rules and the result.
- The loop runs many times every second, and every round is called a frame.
- A frame is one complete drawing, and the browser usually aims for about sixty frames every second.
- Movement written as pixels per frame changes speed between devices. Movement written as pixels per second does not.
- Asking the agent to fill the five headings before it writes code is a cheap check that it understood the game.
- Next: writing a one-page brief that gives the agent the five parts, so it builds the small game you actually meant.
Create a free account to save your progress and open every lesson. Create free account