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.

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 ...