Tiny Turtle is a BASIC turtle-graphics program that draws repeated line segments using DRAW commands, with user-controlled parameters for repeat count, turning angle, step length, and a step increment that grows (or shrinks) the segment length each iteration. The degree-to-radian conversion is handled by a defined function at line 20 using DEF FN r(x)=x*PI/180, exploiting the built-in PI constant. A persistent angle accumulator (line 90) means successive runs without clearing will continue from where the previous drawing left off. The program loops back to line 40 after each drawing, allowing the user to layer multiple patterns on the same screen or clear between them.
Program Analysis
Program Structure
The program is organised into three logical phases: initialisation (lines 1–30), user input collection (lines 40–65), and the drawing loop (lines 70–130), followed by an unconditional return to the input phase at line 140. This creates an infinite interactive loop that allows the user to build up layered drawings without restarting the program.
- Lines 1–20: REMs and the degree-to-radian function definition.
- Line 30: Initialises the cumulative angle to zero once at program start.
- Lines 40–65: Collects five user parameters each cycle.
- Lines 70–130: Optionally clears the screen, then runs the drawing loop.
- Line 140: Loops back to input, preserving
angleacross iterations.
Degree-to-Radian Conversion
Line 20 defines FN r(x) as x*PI/180, converting degrees to radians for use with the trigonometric functions SIN and COS. This is a common BASIC idiom on the Spectrum, where all trig functions operate in radians but users typically think in degrees. Encapsulating the conversion in a DEF FN keeps lines 100 and 110 readable.
Turtle-Graphics Mechanics
Rather than tracking an absolute screen position, the program uses the Spectrum’s DRAW command with relative offsets. At each step, across and down are computed from the current cumulative angle using standard trigonometric projection:
across = SIN(angle_rad) * forward— horizontal componentdown = COS(angle_rad) * forward— vertical component
Note that the axes are swapped compared to conventional mathematics: SIN drives X and COS drives Y. This is intentional for a heading-based turtle model where angle 0 points upward.
Cumulative Angle and Step Increment
The variable angle is only initialised once (line 30) and accumulates across all loop iterations and even across successive input cycles. This means each new drawing session continues rotating from wherever the previous one ended, which can produce unexpected results if the user expects a fresh start without choosing CLS. The variable forward is increased by incstep at line 125 each iteration, enabling spiral or expanding-polygon forms when incstep is non-zero.
Key Variables
| Variable | Purpose |
|---|---|
repeat | Number of line segments to draw per cycle |
right | Degrees added to angle each step |
forward | Current segment length in pixels |
incstep | Amount added to forward each step |
angle | Persistent cumulative heading in degrees |
a$ | User’s yes/no choice for clearing the screen |
Notable Techniques and Anomalies
- The initial
PLOT 127,87on line 70 centres the drawing cursor at approximately the middle of the 256×176 graphics area before drawing begins — but only when the user chooses to clear the screen. If no CLS is performed, the cursor remains wherever the previousDRAWleft it. - Accepting both
"y"and"Y"for the clear prompt (line 70) is a good defensive input practice; however, any other input, including"n", silently skips the CLS without confirmation. - Because
angleis not reset between input cycles, a user who wants a genuinely fresh start must answer Y to the clean prompt AND be aware that the heading continues from its prior value — the CLS only clears the screen, not the state. - Setting
incstep=0produces regular polygons or stars; non-zero values yield Archimedean spiral variants.
Content
Source Code
1 REM "TINY TURTLE"
2 REM By Tim Hartnell ?
3 REM Your Sinclair June 86
20 DEF FN r(x)=x*PI/180
30 LET angle=0
40 INPUT "No. of repeats? ";repeat
50 INPUT "Angle turn to right? ";right
60 INPUT "Steps forward? ";forward
62 INPUT "Step increment? ";incstep
65 INPUT "Clean before drawing (Y/N)? ";a$
70 IF a$="y" OR a$="Y" THEN CLS : PLOT 127,87
80 FOR a=1 TO repeat
90 LET angle=angle+right
100 LET across=SIN (FN r(angle))*forward
110 LET down=COS (FN r(angle))*forward
120 DRAW across,down
125 LET forward=forward+incstep
130 NEXT a
140 GO TO 40
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
