Bat-and-Ball

This file is part of and Timex Sinclair Public Domain Library Tape 1007. Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000
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

VariableInitial ValueRole
K2Ball direction multiplier (+2 or −2)
A10Bat row (fixed vertical position)
B10Bat column (player-controlled)
C20 (A+A)Ball row (fixed at row 20)
D6Ball 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**KRND* 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.

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10294-10335.

Related Products

Related Articles

Related Content

Image Gallery

Bat-and-Ball

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 

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top