Use the whole court
Combine horizontal and vertical movement and draw a court around them.
A ball crossing the same row is easy to follow. Let it travel up and down too, and the player will have a path to anticipate. Continue from Give it a direction after stopping the loop.
Two coordinates, two changes
Replace 20. Add 40, 50, 60, 230, 240 and 250. This is the complete result:
10 BORDER 0: PAPER 0: INK 7: CLS
20 LET x=15: LET y=10: LET dx=1: LET dy=1
30 PRINT AT 0,1;"VOLLEY"
40 FOR c=2 TO 30: PRINT AT 2,c;"-";AT 20,c;"-": NEXT c
50 FOR r=3 TO 19: PRINT AT r,30;"I": NEXT r
60 FOR r=3 TO 19: PRINT AT r,2;"I": NEXT r
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
230 LET y=y+dy
240 IF y=19 THEN LET dy=-1
250 IF y=3 THEN LET dy=1
300 GO TO 100
dy is the change in row, just as dx is the change in column. Starting both at
1 moves diagonally down and right. A positive change in row means down on
this display; it does not mean up as it might on a graph drawn in a maths book.
We update x and y separately. At the top or bottom limit, change only dy.
At the left or right limit, change only dx. A wall reverses the part of the
movement that points into it.
Draw the limits around the movement
The new FOR loops draw the court once, before the moving loop begins. In
FOR c=2 TO 30, c takes each whole-number value from 2 through 30, including both
ends. NEXT c advances it and repeats the body while there are values left.
Line 40 prints dashes in rows 2 and 20 at each of those columns. The semicolon
keeps the printing items together; another AT chooses a new position within
the same PRINT. Lines 50 and 60 draw vertical I characters in columns 30 and
2, from rows 3 through 19.
| Part | Rows | Columns |
|---|---|---|
| Ball’s allowed positions | 3–19 | 3–29 |
| Horizontal walls | 2 and 20 | 2–30 |
| Vertical walls | 3–19 | 2 and 30 |
The ball stays in the cells beside the walls. Our tests still use the inner limits, not the wall cells themselves. The drawing makes the rule visible.
When two turns happen together
Suppose the ball has just been drawn at row 4, column 28, travelling up and
right: dy=-1, dx=1. The update takes it to row 3, column 29. Both boundary
checks apply. The next drawing is at that corner of the allowed area, with
dy=1 and dx=-1 ready for the following update.
Where will it be drawn after leaving row 3, column 29 in those new directions?
Show the explanation
Row 4, column 28. It moves down one row and left one column. A corner can change both directions without needing a special diagonal movement command.
Run the program and watch the ball reach the top, bottom and right side. Its path repeats because the starting values and rules are fixed. We have no random serve, spin or acceleration hidden in the program.
If a wall is broken, check the start and end values of its drawing loop. If the ball wipes out a wall, check the movement limits too: correct drawing does not repair an incorrect position rule. Stop with BREAK when you are ready to continue.
The closed court cannot lose the ball. Next we’ll introduce the player’s paddle, then replace the automatic left-hand return with a contact decision.
Sources
Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 4 (FOR/NEXT), chapter 15 (PRINT AT), and chapter 18 (motion).