Platform Game Programming with AI
Senior 3 · ICT
Platform Game Programming with AI
Lesson 4 of 4
60 min
So far your enemy follows rules you wrote. Now you will add a different kind of AI: a machine-learning model that you train yourself. Hold your hand flat to the camera and the player walks right; raise a fist and it jumps. Nobody writes a rule for what a fist looks like: the model learns it from examples. In S1 you learned that machine learning finds patterns in training data. Here you will collect the data, train the model, test it, and use its predictions in Scratch. Then you will look at the ethical questions any AI game raises.
Definition
Training data: the examples a machine-learning model learns from. Here, webcam pictures of each hand gesture.
Class (label): one category the model can recognise, such as right, jump or nothing.
Model: the result of training: a program that takes a new input (a camera picture) and predicts its class.
Confidence: how sure the model is of a prediction, from 0 (not at all) to 1 (completely), often shown as a percentage.
Teachable Machine: a free website from Google for training image, sound and pose models in the web browser, without writing code.
Training happens inside your browser. Google says the pictures stay on your computer unless you choose to save the project to Google Drive, and that uploading publishes the trained model, not the pictures. Anyone who has the link can use the model.
The standard Scratch website has no Teachable Machine blocks. A free, experimental version of Scratch 3 called Stretch3 adds an extension called TM2Scratch that does.
Then change two tests in the main player script, so the AI and the keyboard both work:
Block diagrams drawn by Ganzaa. The teal blocks belong to the TM2Scratch extension.
Each time the model recognises a gesture with at least the threshold confidence, the matching when received image label script runs and sets the variable ai move. The player script then treats ai move like a pressed key. Keep your hand in the middle of the camera picture: TM2Scratch uses the centre of the image.
Worked example 1: testing the model
Kamanzi trained his model with his own hand only. He tested it 20 times per class, with three classmates' hands:
| Shown | Predicted right | Predicted jump | Predicted nothing | Correct |
|---|---|---|---|---|
| right | 18 | 1 | 1 | 18 of 20 |
| jump | 4 | 13 | 3 | 13 of 20 |
| nothing | 0 | 1 | 19 | 19 of 20 |
Accuracy: . The weak class is jump (65%), mostly mistaken for right. Looking at his samples, he finds that all his fist pictures were taken close to the camera, in bright light, and only with his own hand. He adds 100 fist pictures from three classmates, at different distances and in dimmer light, retrains, and tests again: jump rises to 18 of 20.
Worked example 2: choosing a confidence threshold
With a threshold of 0.5, the player sometimes jumps by itself when Kamanzi scratches his face: the model is 55% sure it sees a fist. With 0.9, the player often ignores real fists, because confidence on a tilted fist is 80–85%. He settles on 0.8: the false jumps stop, and real fists still work. A threshold is a trade-off: higher means fewer false actions but more missed ones.
Every game that uses AI, or is made with AI tools, raises questions about people. Four matter most for your projects:
| Issue | The question | In your platformer | Good practice |
|---|---|---|---|
| Fairness | Does the AI treat every player equally, and is the challenge fair? | A gesture model trained on one person's hand may fail for players with different hands, skin tones or lighting. An enemy faster than the player is unbeatable. | Train on varied data from many people; test with people who were not in the data; always keep a keyboard option; tune enemies so skill decides. |
| Privacy | What personal data does the game collect, and who sees it? | Webcam pictures of classmates are personal data. | Ask permission before recording anyone; keep samples on your computer; upload only the model; never share pictures of others. Rwanda's data protection law (Law N° 058 of 2021) applies to personal data. |
| Copyright | Who owns the sprites, music and code, including what an AI tool made? | Sprites copied from a commercial game, or music from a famous song, belong to others. | Make your own art, use the Scratch library, or openly licensed work with credit; credit remixed Scratch projects; say when an AI tool helped make art or music, and check the tool's terms. |
| Inclusive storytelling | Who appears in the game, and how? | A hero who always looks the same, or villains drawn from one group of people. | Let players choose a character; show varied people with respect; set stories in places your players know; avoid stereotypes. |
Warning
Before you record anyone
Google: Teachable Machine 2.0, making AI easier for everyone. Shows how people gather examples, train a model in the browser and use it in their own projects.
Practice
Further reading
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.