Skip to content
Game 2

Give it a direction

Use a signed step to reverse movement at a boundary.

Our ball reaches the edge and stops. Give it a direction that can change and it can make the return journey. Continue with the complete program from Move a ball.

A step can be negative

Replace line 210 and add 220. Here is the complete program to enter or compare; the other lines stay the same:

10 BORDER 0: PAPER 0: INK 7: CLS
20 LET x=15: LET y=10: LET dx=1
30 PRINT AT 0,1;"VOLLEY"
100 PRINT AT y,x;"o"
110 PAUSE 2
120 PRINT AT y,x;" "
200 LET x=x+dx
210 IF x=29 THEN LET dx=-1
220 IF x=3 THEN LET dx=1
300 GO TO 100

The name dx means a change in x. A value of 1 moves one column right; −1 moves one column left. The instruction LET x=x+dx works in both cases. Adding −1 to 29 gives 28. We do not need separate movement instructions for left and right.

When the ball reaches column 29, line 210 sets dx to −1. When it reaches column 3, line 220 sets it to 1. Neither line changes the current column. It changes what the next update will do.

Follow a turn

The loop draws before calculating the following position. Around the right-hand turn, that gives us:

Column just drawn Direction used for the update Column after adding Direction after the boundary check
28 1 29 −1
29 −1 28 −1
28 −1 27 −1

The ball reaches the last allowed column, appears there once, then travels back. It stays on row 10 throughout. Run the program and watch both turns; this version continues until you use BREAK.

Why don’t we set dx to −1 on every trip through the loop?

Show the explanation

That would overwrite the direction chosen at the left boundary. The ball needs to keep its current direction between contacts, and change it only when a boundary is reached.

These tests work with our one-cell steps and starting position inside the limits. An equality test can miss an edge if a larger step jumps over its exact value. Keep dx at 1 or −1 in this version; increasing the number is a change to the movement model, not just an innocent speed setting.

A rule before a wall

There is still no visible court. Columns 3 and 29 are boundaries because our conditions make them boundaries. A line drawn on screen cannot stop the ball by itself; the program must decide what contact means.

That distinction will matter when we add the paddle. For now, check the simpler rule: the ball should visit both endpoints repeatedly and never go beyond them. If it crosses an edge, inspect the equality and the assigned sign. If it stays at an edge, check that line 200 still changes x and that line 300 returns to 100.

Stopping a running demonstration

On the Spectrum, BREAK is CAPS SHIFT plus SPACE. Emu198x Host keyboard mode also provides Pause as BREAK. Stop this demonstration before editing the next one. Do not close the emulator to stop a BASIC loop.

We now have motion that repeats without a player. Next we’ll give the ball a second direction so it can travel around a court instead of along one row.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapters 3 and 7 (comparisons and arithmetic), and chapter 18 (motion).