Random Squares

This file is part of and Timex Sinclair Public Domain Library Tape 1002. Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000
Tags: Demo

This program continuously draws randomly positioned hollow squares on screen using block graphics characters stored in a 4×4 string array. Each square is four rows tall and four characters wide, constructed from Spectrum block graphics to create a box outline with solid corners and edges. The screen position is chosen randomly each iteration: column in the range 0–28 and row in the range 0–16, ensuring the 4-row square fits within the display. The loop runs indefinitely, accumulating squares on screen until the user presses the “A” key to stop.


Program Analysis

Program Structure

The program is compact and self-contained, falling into three logical phases:

  1. Initialisation (lines 10–60): A 4×4 string array A$ is declared and loaded with four rows of block graphics forming a hollow square outline.
  2. Main loop (lines 70–130): Random screen coordinates are chosen, the square is printed at that position row by row, a keypress is checked, and execution loops back unconditionally.
  3. Save/run stub (lines 140–150): Handles program persistence.

Block Graphic Square Construction

The four strings stored in A$ use ZX Spectrum block graphic characters to draw a 4×4 hollow box. Decoded, the rows are:

Array rowLineVisual roleCharacters used
A$(1)30Top edge▙▀▀▟
A$(2)40Middle (upper)▌   ▐
A$(3)50Middle (lower)▌   ▐
A$(4)60Bottom edge▛▄▄▜

The two middle rows are identical, giving the square a hollow interior two character-cells tall. The overall shape is 4 columns wide and 4 rows tall in character-cell terms.

Random Placement

Line 70 computes S=INT(RND*29), giving a column in 0–28, and line 80 computes T=INT(RND*17), giving a row in 0–16. Since the square is 4 rows tall and 4 columns wide, the maximum safe top-left position on a 32×22 display would be column 28 and row 18. The column range is therefore safe, but the row range (0–16) is conservatively constrained to keep the square fully on screen with plenty of margin.

Key BASIC Idioms

  • DIM A$(4,4): Declares a two-dimensional string array with 4 strings each of length 4, matching the 4-character-wide square exactly. Each row of the graphic maps directly to one string element.
  • FOR H=0 TO 3 / PRINT AT T+H,S: Iterates over the four graphic rows, offsetting the row coordinate by H each time to print them consecutively.
  • IF INKEY$=”A” THEN STOP: Polls the keyboard each iteration without pausing, so the program runs at full speed and only halts when “A” is held. This is a non-blocking keypress check, unlike the PAUSE 0/INKEY$ idiom which waits for any key.
  • GOTO 70: Returns to the random-coordinate generation rather than the top of the print loop, ensuring fresh coordinates for every new square.

Notable Techniques

Because the screen is never cleared between iterations, squares accumulate on top of one another, producing an increasingly dense pattern. The program exploits the transparency of the block graphic approach — each new square simply overwrites the character cells it occupies, creating an organic layering effect rather than explicit animation.

Potential Anomalies

The DIM A$(4,4) declaration allocates strings of exactly 4 characters. Each graphic row is also exactly 4 characters, so there is no padding issue. However, if a future edit extended any row beyond 4 characters, the excess would be silently truncated by the fixed-length string array. Additionally, lines 140–150 (SAVE and RUN) are unreachable during normal execution because the program loops indefinitely via GOTO 70 and exits only via STOP; they exist solely for program loading/saving purposes.

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10051 – 10121.

Related Products

Related Articles

Faster Random Numbers Frank Terranella Have you ever been frustrated by the RND function? You type in:LET R=INT (RND*10)+1to generate...

Related Content

Image Gallery

Source Code

  10 REM %R%A%N%D%O%M% %S%Q%U%A%R%E%S% % % % % % % % % 
  20 DIM A$(4,4)
  30 LET A$(1)="\:'\''\''\':"
  40 LET A$(2)="\:   \ :"
  50 LET A$(3)="\:   \ :"
  60 LET A$(4)="\:.\..\..\.:"
  70 LET S=INT (RND*29)
  80 LET T=INT (RND*17)
  90 FOR H=0 TO 3
 100 PRINT AT T+H,S;A$(H+1)
 110 NEXT H
 120 IF INKEY$="A" THEN STOP 
 130 GOTO 70
 140 SAVE "1008%6"
 150 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