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.