☰ Year plan · Communication Technology

Dynamic pages, a quiz and a game

Week 14 · Chapter 13 AI Programming (2): dynamic web pages, presenting your site, a quiz app, a block-breaker game and reviewing what you built · 3 lessons
3

A block-breaker game and how to present what you built

ICT Secondary 1 Term 1, Chapter 13, Lessons 13-5 to 13-12: pp. 190–202
GoalExplain the parts of a block-breaker game with small checked fragments, and present a project.
New words
game loop · the steps that repeat many times a second: move, check, drawspeed · how far the ball moves in each stepbounce · reverse the speed when the ball meets a wallpresentation · a short talk: purpose, users, what you made, what the helper did, what you checked
Explanation

A block-breaker game is a few simple pieces. The ball has a position x and a speed dx, the number of pixels it moves in each step. On every step the game does x = x + dx. When the ball reaches the wall (x is 100 or more, or 0 or less), the speed is reversed with dx = -dx, so the ball bounces. The paddle is moved by the keys, left or right by a fixed number of pixels. The blocks are stored in an array: 1 means the block is still there, 0 means it was hit. When the ball hits block i, the game sets blocks[i] = 0 and adds 10 to the score. Everything repeats in the game loop: move the ball, check walls, paddle and blocks, draw the screen. The game is developed in small steps, one piece at a time, tested after each step. When you present your work, say what it is for, who will use it, what you made, what the AI helper did and what you checked yourself. Remember that long game code is easy to get wrong. Read the parts, run them, and keep only what you understand.

Worked examples
JavaScript
1let x = 80;
2let dx = 5;
3function step() {
4  x = x + dx;
5  if (x >= 100 || x <= 0) {
6    dx = -dx;
7  }
8}
The ball and the wall. The ball starts at x = 80 with dx = 5. The program checks the ball after each step.
Stepxdx
1855
2905
3955
4100-5
595-5
690-5
785-5
A trace of the ball for seven steps. At x = 100 the speed is reversed, so the ball starts to move back.
JavaScript
1let blocks = [1, 1, 1, 1, 1];
2let score = 0;
3function hit(i) {
4  if (blocks[i] === 1) {
5    blocks[i] = 0;
6    score = score + 10;
7  }
8}
The blocks. Two rows of five blocks, 1 means present. If the ball hits the blocks at index 1 and index 3 of the first row, the array becomes 1 0 1 0 1 and the score is 20; hitting block 1 again changes nothing.
ABCDEF
1ABCDE
2Row 111011
3Row 210110
A picture of the blocks as a grid. Column letters are only labels; 1 is a block, 0 is a gap.
Class activity

On paper, trace the ball for the start x = 20, dx = -5, with a wall at 0. Write x and dx for the first six steps. Then write a five-sentence presentation of an invented project: its purpose, its user, what you made, what the helper did, and what you checked.

Practice
1
The ball is at x = 95 with dx = 5. What are x and dx after one step?
2
What does blocks[2] = 0 mean?
3
Name the three jobs of the game loop.
4
Name two things to say about the AI helper in a presentation.