Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with inkey$ problem

Status
Not open for further replies.

Curly

MIS
Mar 29, 2001
2
US
My head hurts. Please help!
Here is my problem.
I have writen a game that works the same way as pac man (yet another clone)!

As the main character moves along , I store the last key press and use it again if no key is pressed. This avoids the player tapping keys all the time.
The trouble is that in most pac man clones (good ones atleast) the player can press a key in advance of where they want to move - so the player goes in along a wall and presses a key that will move him in to a new path as soon as a break in the wall is found.

Here is an example of my code (simple text gfx)

CLS
'FIRST OFF, SET UP FOUR WALLS
PRINT "#######################################################################"
PRINT "# #"
FOR COUNT = 1 TO 20
PRINT "# # # #"
NEXT COUNT
PRINT "#######################################################################"
'SOMETHING TO BUMP INTO
LOCATE 13, 26: PRINT "##########"
LOCATE 14, 26: PRINT "##########"

Y = 2: X = 2 'SET PLAYER START LOCATION
LOCATE Y, X: PRINT "@";

DO
KEYB$ = INKEY$ 'GET A KEY PRESS
IF KEYB$ = "" THEN KEYB$ = OLD.KEY$ 'IF NO KEY PRESS USE OLD KEY PRESS
IF KEYB$ = CHR$(0) + "H" THEN GOSUB UP
IF KEYB$ = CHR$(0) + "P" THEN GOSUB DOWN
IF KEYB$ = CHR$(0) + "K" THEN GOSUB LEFT
IF KEYB$ = CHR$(0) + "M" THEN GOSUB RIGHT
IF KEYB$ = CHR$(27) THEN END ' EXIT
OLD.KEY$ = KEYB$ ' STORE OLD KEY
FOR DELAY! = 0 TO 10000: NEXT DELAY! ' SLOW THINGS DOWN!
LOOP

UP:
IF SCREEN(Y - 1, X) <> 32 THEN RETURN ' LOOK UP - IF ITS A WALL DONT MOVE
LOCATE Y, X: PRINT &quot; &quot;; ' CLEAR OLD LOCATION
Y = Y - 1: LOCATE Y, X: PRINT &quot;@&quot;; ' SET NEW LOCATION
RETURN

DOWN:
IF SCREEN(Y + 1, X) <> 32 THEN RETURN
LOCATE Y, X: PRINT &quot; &quot;;
Y = Y + 1: LOCATE Y, X: PRINT &quot;@&quot;;
RETURN

LEFT:
IF SCREEN(Y, X - 1) <> 32 THEN RETURN
LOCATE Y, X: PRINT &quot; &quot;;
X = X - 1: LOCATE Y, X: PRINT &quot;@&quot;;
RETURN

RIGHT:
IF SCREEN(Y, X + 1) <> 32 THEN RETURN
LOCATE Y, X: PRINT &quot; &quot;;
X = X + 1: LOCATE Y, X: PRINT &quot;@&quot;;
RETURN

Can someone add to this code to perform the advance key presses and moves?

Thanx for any help!
Curly



 
I would do it this way,

When a key is pressed, check to see if the player can go in that direction,

if he can, change the direction to that direction

if not, advance in the current direction, once at the new spot, check to see if the player can go in the direction they last specified, if not, advance in the current direction, and repeat this process until the player can go in the direction that they chose or they hit a wall or choose a new direction.

So, instead of writing code to handle this specific event, write your main moving loop like this and it will take care of all events.

Hope that helps.

 
you could make a map for each level

example:

dim map$(20,5)

map$(1,1)=&quot;##########&quot;
map$(2,1)=&quot;# #&quot;
map$(3,1)=&quot;# # # # #&quot;
map$(4,1)=&quot;# # # # #&quot;
map$(5,1)=&quot;# # # ## #&quot;
map$(6,1)=&quot;# ### # #&quot;
map$(7,1)=&quot;# # # #&quot;
map$(8,1)=&quot;# ## #&quot;
map$(9,1)=&quot;# ### ## #&quot;
map$(10,1)=&quot;##########&quot;

level=1

for d=1 to 10
print map$(d,level)
next d
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top