Getting a key without the GETKEY function

Is there a function similar to the GET KEY function that doesn't stop the program until a key is entered?

I'm making a snake game and I want the snake figure to keep moving and allow you to press a key to change directions.

If I were to do a GET KEY, it would completely stop moving until I press a key.

Comments

GET KEY

Hi,
You need to use:

IF key input then

GET KEY k

END IF

Big John

hey bigjohn, thanks for the

hey bigjohn, thanks for the feedback, but I cant seem to get this to work. I've tried multiple times to implicate this, but can't figure it out... Is there any other way to accomplish this? If not, I can post a sample of what I'm doing if you're willing to look at it?

key input

Hi,

Post a sample of your code and I will check it out.

Big John

Okay, im still fairly new

Okay, im still fairly new with true basic so it wont look too pretty

SUB Game
CALL macpensize(2,2)
WINDOW #4
LET a = int(rnd*11)+1
LET b = int(rnd*11)+1
CALL mactextsize(36)
PLOT TEXT, AT a+.3,b+.23:"O"
CALL mactextsize(12)
PAUSE .01
LET move = 100
LET xsnake1 = 4
LET xsnake2 = xsnake1+1
LET ysnake1 = 4
LET ysnake2 = ysnake1+1
DO
SET COLOR "black"
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
IF xsnake2 >= 12 or xsnake1 <= 0 or ysnake2 >= 12 or ysnake1 <= 0
LET collide = 1
ELSE
IF move = 100 then
DO
IF key input then
GET KEY move
ELSE
PAUSE .1
SET COLOR 250
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
SET COLOR "black"
LET xsnake1 = xsnake1 + 1
LET xsnake2 = xsnake2 + 1
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
END IF
LOOP until move <> 100
ELSE IF move = 97 then
DO
IF key input then
GET KEY move
ELSE
PAUSE .1
SET COLOR 250
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
SET COLOR "black"
LET xsnake1 = xsnake1 - 1
LET xsnake2 = xsnake2 - 1
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
END IF
LOOP until move <> 97
ELSE IF move = 119 then
DO
IF key input then
GET KEY move
ELSE
PAUSE .1
SET COLOR 250
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
SET COLOR "black"
LET xsnake1 = ysnake1 + 1
LET xsnake2 = ysnake2 + 1
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
END IF
LOOP until move <> 100
ELSE IF move = 115 then
DO
IF key input then
GET KEY move
ELSE
PAUSE .1
SET COLOR 250
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
SET COLOR "black"
LET xsnake1 = ysnake1 - 1
LET xsnake2 = ysnake2 - 1
BOX LINES xsnake1,xsnake2,ysnake1,ysnake2
END IF
LOOP until move <> 115
END IF
END IF
END IF
LOOP while collide = 0
END SUB

mcmb03 ... The code you posted is incomplete ...

mcmb03 ... The code you posted cannot RUN. The second code line is:

CALL macpensize(2,2)

The subroutine 'macpensize' doesn't exist in your posted program listing. Accordingly, your posted program cannot be debugged. Regards ... Tom M

Snakes program

Hi,

You need to learn to use the DO FORMAT option so as to indent your code. This will tell you that you have a missing THEN at the end of an IF clause. It will also tell you that you have too many END IF statements.
Cure those things and IF key input will work.

You also need to look at your first IF...THEN clause that checks for a collision. This needs an END IF statement before you start checking for keystrokes.

You need to plan the structure of your program more carefully - try drwing out the flow of your program on a bit of paper to plan what you want the program to do.

Big John

Earlier in my program i open

Earlier in my program i open the mac tools library so the Call mac pen size works fine and is not for a subroutine. this Call function simply refers to that library and changes the line thickness

Also i had to type all of this out and tried to ad the formatting because at our school we're working on extremely old macs (like from 2000) with no internet. I tried using spaces to format it on here but after posting, the forum automatically deleted all of them.

Snakes program

Hi,

I removed the CALLs to the Mac Toolbox, and after I corrected the missing THEN, your program worked, but not for many cycles around the DO...LOOP because the first IF...THEN clause signalled a collision. Remove the collision clause and the rest works OK.

Big John

Thanks!

okay thank you very much i will try that out. so you're saying to remove the whole IF..THEN at the beginning? and then I'm not too sure what you mean when you say it didn't work for many cycles around the do loop?

Snakes program

Hi,

Basically the code you posted has an outer DO....LOOP structure. The first clause in the IF...THEN structure looks for collisions. When I tested your program it only went through the outer DO...LOOP about twice before the first clause indicated a collision and the program stopped because it made an EXIT from the outer DO..LOOP.

If you want to test out all the IF key input statements then you have to comment out the first collision clause so that you can check that each key input statement works properly.

Big John