Using Inkeys
Using Inkeys
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?)
Re: Using Inkeys
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
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
- 1024MAK
- Posts: 5118
- Joined: Mon Sep 26, 2011 10:56 am
- Location: Looking forward to summer in Somerset, UK...
Re: Using Inkeys
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
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.
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.
Re: Using Inkeys
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.
Re: Using Inkeys
I’ve found putting a For/Next loop around the Inkeys routine helps, but it seems like a bizarre solution
- 1024MAK
- Posts: 5118
- Joined: Mon Sep 26, 2011 10:56 am
- Location: Looking forward to summer in Somerset, UK...
Re: Using Inkeys
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
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.
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.
Re: Using Inkeys
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")
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)
- 1024MAK
- Posts: 5118
- Joined: Mon Sep 26, 2011 10:56 am
- Location: Looking forward to summer in Somerset, UK...
Re: Using Inkeys
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
Mark
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
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.
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.