Wednesday, September 2, 2026

ZX Spectrum - Building a game in BASIC - PART 2

ZX Spectrum - Learning Sinclair BASIC  

PART 2

PRINT,  FOR,  NEXT,  INT  and  RND

In part 1 I shared examples of keyboard control using the command INKEY$. So, with the press of a key we now have a program in place to move a character, let us now introduce something else on the screen, maybe some walls and then perhaps some items which have to be collected, and to make the game more challenging we’ll eventually add a nasty, or 2, but let's not get ahead of ourselves I will design the playing area first!

With the PRINT and AT commands I am going to assume that you already have an understanding with printing and positioning on the screen, vertical 0 to 21 and horizontal 0 to 31, plus there are plenty of examples available of the 10 PRINT “Hello World”: GO TO 10  code that I expect you've seen so I won’t bore you with the logistics of how it works, if however you are unfamiliar you’ll soon get the idea as we continue with the project.  

Using the FOR and NEXT commands to create some simple walls on the screen.


We now have a playing area to work with, using the hashtag symbol '#' and INVERSE 1 I have created some walls, the print command at line 790 has the equation INT (RND*30)+1 assigned to the horizontal position.  INT rounds a given number down to the largest integer less than or equal to that number, RND generates a random decimal number between 0 and 1.   Therefore in this case the equation will generate a random number between 1 and 30, and because it's positioned in a FOR NEXT loop routine with variable ‘f’ assigned to the vertical position,  f=3 TO 17 STEP 2 the code will place a blank space randomly on every wall, the STEP 2 specifies the increment value for a counting loop, telling the computer to increase the loop variable by 2 at each iteration.  Every FOR command must have a matching NEXT command,  the loop routine in this code starts from vertical print position 3 and ends when vertical print position 17 is reached.

RUN

 

Below I have added the keyboard control program to the game play area program, you’ll notice a few changes.



Line 20 GO SUB (Go to Subroutine) 600, I have moved the variables to line 610, then the game play area code from line 700 continues on until it reaches line 900 RETURN, as the command instructs it returns back to line 20 and continues to read the code, in this instance there is no more code after the GO SUB therefore the BASIC interpreter moves to the next line.  
I have also changed the parameters after the k$ instructions so that now are character stays within the boundaries.  

RUN

As you can see when the program is ran the character when moved will wipe out whatever is in its path, we now need some collision detection put in place.

Wednesday, August 26, 2026

ZX SPECTRUM - Building a game in BASIC - PART 1

ZX SPECTRUM - Learning Sinclair BASIC

PART 1  Keyboard control

Author Animated ALex

I have been learning the Sinclair BASIC language now for sometime and have been sharing my journey on my YouTube channel https://www.youtube.com/@animatedAL   In this blog i'd like to share my approach at making a simple game using the techniques that I have learnt, explaining briefly in layman's terms to the best of my knowledge and understanding of the various programming commands that I will be using.

So let's make a game! I mean after all the majority who want to learn to program this 1980's computer want to make games, and if Machine code seems a bit too daunting to explore then BASIC is a good stepping stone to start with, and if you are more advanced than I am at programming then I am expecting criticism,  so please don't hold back as some constructive criticism is always welcome! 

Now one thing with BASIC it can be slow, so when designing a game this is worth taking in to consideration, don't decide to make something that is too over complicated and becomes an un-playable mess, unless of course you are deciding to go down the Machine code compiling route using such software as MCODER 3.   For me personally when I first started programming in BASIC I wanted to make a character move on the screen under keyboard control, so let us start with the INKEY$ command,

INKEY$ is the command used to read the keyboard of the ZX Spectrum, 

IF INKEY$="p" THEN .... move the character right,  and so on for the other directions using the familiar keys 'o', 'q', and 'a'.


10 REM Keyboard control example 1 

20 LET y=10 : LET oy=y : LET x=15: LET ox=x  

30 LET k$=INKEY$

40 IF k$="o" AND x>0 THEN LET x=x-1 

50 IF k$="p" AND x<31 THEN LET x=x+1

60 IF k$="q" AND y>0 THEN LET y=y-1

70 IF k$="a" AND y<21 THEN LET y=y+1

80 IF (oy<>y OR ox<>x) THEN PRINT AT oy,ox;" "

90 PRINT AT y,x;"@"

100 LET oy=y : LET ox=x 

110 GO TO 30


The above program is just one example of achieving keyboard control to move a character, in this case at line 30 I have assigned the variable string k$ to INKEY$, so for example if key 'q' is pressed  IF k$="q"  then character "@" will move up the screen, y=y-1 the AND >0 is the parameter set to stop the travel so that is doesn't go off the screen.  in line 90 we PRINT the character to be moved, the y and x variables are the coordinates for the character, y is for the vertical direction and x is for the horizontal direction, oy and ox are the old positions, this is instructed in line 80 and basically it stops a trail happening when the character is moved, therefore it erases the characters moving trail with a blank space.  


Here is another example for Keyboard control

10 REM Keyboard control example 2

20 LET y=10 : LET oy=y : LET x=15 : LET ox=x

30 LET k$=INKEY$

40 LET x=x-(k$="o" AND x>0)+(k$="p" AND x<31) : LET y=y-(k$="q" AND y>0)+(k$="a" AND x<21)

50 PRINT AT oy,ox;" " AND (oy<>y OR ox<>x); AT y,x;"@" : LET oy=y : LET ox=x

60 GO TO 30


Example 2 delivers the same end result as example 1, if you read the code hopefully you'll understand the concept, plus I will revisit either example1, or 2 and make alterations as the project grows.


ZX Spectrum - Building a game in BASIC - PART 2

ZX Spectrum - Learning Sinclair BASIC   PART 2 PRINT,  FOR,  NEXT,  INT  and  RND In part 1 I shared examples of keyboard control using the ...