LOADING...

SCREECH

Vintage & Modern Tech Blog

<5 Min BASIC #10 - Keyboard Control

May 20, 2016

We've made colourful graphics, beeper sounds, and interacted
with the computer, now - it's time for some arcade style control through the keyboard.

 

Restart the ZX Spectrum and enter this listing 
in BASIC:

 

1 REM Keyboard Control
2 REM Stephen Nichol
3 REM Loading Screech
4 REM 20th May 2016
10 LET y=16: LET x=18
20 PRINT AT y,x;"A"
30 IF INKEY$="p" THEN LET x=x+1
40 IF INKEY$="o" THEN LET x=x-1
50 PRINT AT y,x+1;" "
60 PRINT AT y,x-1;" "
70 IF x<1 THEN LET x=1
80 IF x>29 THEN LET x=29
90 GO TO 20

 

(Kindly observe, that lines 50 and 60 should have a single blank space in between the quotes.)

 

Explanation/Refresher
Lines 1 to 4 are REMarks, notes that do not affect the running of the program.


Line 10 sets up the initial co-ordinates of the player character.


Line 20 PRINTs the player character (a letter 'A' in this simple example).


Line 30 uses the INKEY$ ("IN-KEY-STRING" although I usually say

"In-keys") command to test the 'p' key. If 'p' is pressed then the player character is moved right by one character cell (8 pixels) by adding 1 to x (LET x=x+1).


Line 40 tests for a press of the 'o' key and moves the player left by
subtracting 1 from x.


Line 50 PRINTs an empty space to the right of the player.


Line 60 PRINTs an empty space to the left of the player.


Line 70 Stops the player going left out of screen. (If you're new to programming, this is known as 'error-catching').


Line 80 Stops the player going right out of the screen.


Line 90 Loops the program back to line 20.
 
Exercises

1. Replace the "A" in line 20 with a "B" and RUN - how does the program change?
2. Delete Lines 50 and 60 then RUN the program - what happens?  
3. Change Line 20 to PRINT at x,y;"A" (switching the y.x for x,y) and RUN - what changes?
4. Change Line 90 to GO TO 10 - why doesn't the program work?
5. In Line 30 change LET x=x+1 to LET x=x+6 and RUN - is this the way that you want your character to move?

Please reload

PLAY EXTENDED

  • YouTube Social  Icon

Custom Website Design and Content © 2014-2019 by Stephen Nichol. (Homepage theme in part inspired by 1992 issues of the computing magazine Your Sinclair). Articles, Captions, and Downloads by Stephen Nichol unless otherwise stated. Proudly created with Wix.com.

This site was designed with the
.com
website builder. Create your website today.
Start Now