--- title: "Bat-and-Ball" id: 56855 type: "computer_media" slug: "broken-program-2" url: "http://localhost/computer_media/broken-program-2/" markdown_url: "http://localhost/computer_media/broken-program-2.md" published_at: "2024-09-29T07:53:09+00:00" modified_at: "2026-03-30T21:20:43+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/09/323_Unk.png" excerpt: "A minimalist bat-and-ball game where a bouncing block and a player-controlled paddle face off using nothing but BASIC and inverse-video characters." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Game" slug: "game" taxonomy: "genre" url: "http://localhost/type/game/" media_contents: - id: 56738 title: "Timex Sinclair Public Domain Library Tape 1007" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1007/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/09/323_Unk.png" media_type_tags: "Game" --- This program implements a simple bat-and-ball game on the ZX81/TS1000, where a ball bounces horizontally across the screen and the player controls a bat using keys. The ball’s position is tracked by column variable D, which reverses direction when it hits the screen edges (columns 7 and 17), while the bat is drawn at row A using inverse-video space characters for solid blocks. The SCROLL command at line 80 is used to erase the previous ball position before redrawing, a common ZX81 screen-update trick. Key codes 50 and 63 correspond to the ‘2’ and ‘/’ keys respectively, used for left/right bat movement. Line 140 uses RETURN to exit the game loop when no key is pressed, serving as a termination condition. *** ## Program Analysis ### Program Structure The program is a short bat-and-ball game contained in a single loop from lines 60 to 150. Initialization occupies lines 10–50, and lines 160–180 handle save/restart. The main game loop redraws the ball, reads the keyboard, moves the bat, updates the ball’s horizontal position, and repeats. ### Variable Roles | Variable | Initial Value | Role | | --- | --- | --- | | `K` | 2 | Ball direction multiplier (+2 or −2) | | `A` | 10 | Bat row (fixed vertical position) | | `B` | 10 | Bat column (player-controlled) | | `C` | 20 (A+A) | Ball row (fixed at row 20) | | `D` | 6 | Ball column (moves each frame) | ### Game Loop Mechanics Each iteration of the loop (lines 60–150) performs the following steps in order: 1. Draw the ball at row `C`, column `D` (line 60). 2. Erase the bat’s previous position by printing a space at row `A`, column `B` (line 70). 3. `SCROLL` the screen up one line (line 80), which also erases the top ball row indirectly. 4. Read the keyboard: key code 50 (‘2’) moves the bat right; key code 63 (‘/’) moves it left (lines 90–100). 5. Redraw the bat at its new position (line 110). 6. Bounce the ball if it reaches column boundaries (lines 120–130). 7. Exit via `RETURN` if no key is held (line 140), otherwise loop back (line 150). ### Key BASIC Idioms and Techniques - **Inverse-video blocks:** The `%` sequences in PRINT statements render as solid inverse-video space characters, creating visible bat and ball sprites without UDGs or machine code. - **Direction flip:**`LET K=-K` at line 120 neatly reverses the ball’s horizontal direction by negating the step value between +2 and −2. - **RND* *K:** Line 130 uses `RND**K` — `RND*` on the ZX81 gives a random integer, and multiplying by `K` (±2) adds a variable-speed horizontal step each frame. - **RETURN as exit:** Line 140 uses `RETURN` when no key is pressed. Since the main loop is not called via `GOSUB`, this will cause an error or jump to a calling context, effectively acting as a game-over or halt mechanism. - **SCROLL for animation:** The `SCROLL` at line 80 shifts the entire display up, functioning as a crude erase for the ball row rather than blanking individual positions. ### Bugs and Anomalies - The bat movement keys are asymmetric: code 50 (‘2’) increases `B` (moves right), while code 63 (‘/’) decreases it (moves left). On a standard keyboard layout this is non-intuitive; ‘5’ and ‘8’ would be more natural for left/right. - There is no collision detection between the bat (row 10) and the ball (row 20); the game has no win/lose condition based on interception. - The bat column `B` is never clamped, so it can move off-screen without restriction. - `SCROLL` unconditionally shifts the whole display each frame, causing the entire screen to scroll upward continuously, which distorts any persistent display elements. - Lines 160–180 (`CLEAR`, `SAVE`, `RUN`) are unreachable from the main loop and appear to be leftover development/save boilerplate. ## Source Code ``` 10 LET K=2 20 LET A=10 30 LET B=A 40 LET C=A+A 50 LET D=6 60 PRINT AT C,D;"% % % % % % " 70 PRINT AT A,B;"% " 80 SCROLL 90 IF CODE INKEY$=50 THEN LET B=B+1 100 IF CODE INKEY$=63 THEN LET B=B-1 110 PRINT AT A,B;"% " 120 IF D>17 OR D<7 THEN LET K=-K 130 LET D=D+RND**K 140 IF NOT CODE INKEY$ THEN RETURN 150 GOTO 60 160 CLEAR 170 SAVE "1032%3" 180 RUN ```