Using Inkeys

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Using Inkeys

Post by MrVertigo »

If you’re making a game in BASIC on the ZX81, and you have a couple of movement routines controlling enemies, what is the best way to get an INKEYS movement from the player to feel responsive? Do you have to place a few inkeys checks throughout the game loop? Is it better to peek last_k, or does that still need to be placed a few times? (I know the real answer is use machine code for your game :) , but can this be done in basic?)
dr beep
Posts: 2080
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Using Inkeys

Post by dr beep »

10 LET K$=INKEY$
20 IF K$="q" THEN LET Y+Y-1
30 IF K$="a" THEN LET Y+Y+1
40 IF K$="o" THEN LET X+X-1
50 IF K$="p" THEN LET X+X+1
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Using Inkeys

Post by 1024MAK »

As dr beep indicates, ALWAYS put the value of INKEY$ in a string variable. It saves many problems. The INKEY$ should also be in the main loop (or in a subroutine called by the main loop). As long as the loop executes quickly enough, you don’t need any additional INKEY$ functions.

If you have 16K bytes (or more) RAM, learn to code for speed, rather than memory efficiency. Even then, when using BASIC, the tricky bit is getting the code to do what you want, but still having it execute fast enough so that the game is responsive.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Using Inkeys

Post by MrVertigo »

dr beep wrote: Sun Jul 23, 2023 5:11 pm 10 LET K$=INKEY$
20 IF K$="q" THEN LET Y+Y-1
30 IF K$="a" THEN LET Y+Y+1
40 IF K$="o" THEN LET X+X-1
50 IF K$="p" THEN LET X+X+1
That’s the method I’ve been using, but I have a few “enemy” movements within my loop, and the control keys are VERY unresponsive. So I tried having a few Inkeys functions within the loop, but it’s still the case that unless you hold the key down all the time, there is very little response.
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Using Inkeys

Post by MrVertigo »

I’ve found putting a For/Next loop around the Inkeys routine helps, but it seems like a bizarre solution :|
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Using Inkeys

Post by 1024MAK »

An alternative is to only perform one action per main loop other than checking the keyboard.

Use a counter to determine which action occurs on which iteration of the loop.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Using Inkeys

Post by XavSnap »

Hi,
Mr. Vertigo, prefer this "LET" syntaxe:
[equal symbol]
20 IF K$="q" THEN LET Y=Y-1

Should be: [FASTER]
10 LET X=X+(INKEY$ AND "P")-(INKEY$ AND "O")
20 LET Y=Y+(INKEY$ AND "A")-(INKEY$ AND "Q")
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Using Inkeys

Post by MrVertigo »

1024MAK wrote: Sun Jul 23, 2023 9:03 pm An alternative is to only perform one action per main loop other than checking the keyboard.

Use a counter to determine which action occurs on which iteration of the loop.

Mark
Nice idea!
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Using Inkeys

Post by MrVertigo »

XavSnap wrote: Sun Jul 23, 2023 9:48 pm Hi,
Mr. Vertigo, prefer this "LET" syntaxe:
[equal symbol]
20 IF K$="q" THEN LET Y=Y-1

Should be: [FASTER]
10 LET X=X+(INKEY$ AND "P")-(INKEY$ AND "O")
20 LET Y=Y+(INKEY$ AND "A")-(INKEY$ AND "Q")
This looks promising! I’ll give it a try. Thanks!
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Using Inkeys

Post by 1024MAK »

I prefer:

10 GOSUB 1000
20 FOR A=0 TO 0 STEP 0
30 LET OX=X
40 LET OY=Y
50 LET K$=INKEY$
60 LET X=X+(K$="P")-(K$="O")
70 LET Y=Y+(K$="Q")-(K$="A")
80 UNPLOT OX,OY
90 PLOT X,Y
100 REM OTHER CODE
110 NEXT A
1000 LET X=32
1020 LET Y=22
1030 RETURN
Listing
Listing
Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
Post Reply