Page 1 of 1

Can't get inkey$ to work (sometimes)

Posted: Tue Jun 15, 2010 8:12 pm
by DrNick13
I have a problem getting input for my game.

I use

Code: Select all

IF (UserInput$ = "o") AND (Player1.Y > 1) AND (Get1Input = 1) THEN
       
                ' Move P1 up
                Player1.Y = Player1.Y - 2

                Get1Input = 0
               
                ELSEIF (INKEY$ = "l") AND (Player1.Y < 175) AND (Get1Input = 1) THEN

                ' Move P1 down
                Player1.Y = Player1.Y + 2

                Get1Input = 0

END IF
for the player and it only moves the player slightly up/down about every 5 or so seconds.

HOWEVER, with using this code to exit the program:

Code: Select all

LOOP UNTIL (UserInput$ = CHR$(27))
it works every time.

Please help!

Posted: Tue Jun 15, 2010 11:40 pm
by burger2227
Where is the rest of your loop? Post the entire INKEY$ loop.

Posted: Wed Jun 16, 2010 2:12 pm
by DrNick13
Those two locations are the only places where i use INKEY$

Except for

Code: Select all

UserInput$ = ""
UserInput$ = INKEY$
which I use at the beginning of the loop

Posted: Wed Jun 16, 2010 3:16 pm
by burger2227
Check your movements. The IF statement looks for a coordinate greater than 1 while your move is 2. Sooner or later it could be 0 or 1 which will not move anything.

Also where is the code to actually move the object? You just change the coordinate variable value in the code provided.

Dot variables are used for TYPE variables normally. Where is that code?

If you continue asking questions while supplying SPARSE information and code then I will ignore your posts and so may others.

Posted: Wed Jun 16, 2010 4:35 pm
by DrNick13
Woops... originally i used 1 for movement (just a box around the screen to test it) and realized it was too slow.

I do use TYPEs for the values:

Code: Select all

TYPE StandardSprite
        X AS INTEGER
        Y AS INTEGER
        OldX AS INTEGER
        OldY AS INTEGER
END TYPE
For moving the sprite, I just PUT it using those co-ordinates:

Code: Select all

PUT (Player1.X, Player1.Y), sprPlayer1
PUT (Player2.X, Player2.Y), sprPlayer2
Then I save the old co-ordinates and blank the sprites before drawing them in their new positions (on the next frame):

Code: Select all

' Save the old positions for blanking
Player1.OldX = Player1.X
Player1.OldY = Player1.Y
Player2.OldX = Player2.X
Player2.OldY = Player2.Y
      
' Blank the sprites
PUT (Player1.OldX, Player1.OldY), sprPlayer1
PUT (Player2.OldX, Player2.OldY), sprPlayer2

Posted: Wed Jun 16, 2010 5:37 pm
by burger2227
If you are using INKEY$ for 2 players pressing keys at the same time it will not work well.

Use Scancodes instead. You can store multiple presses into an array so that more keypresses can be checked. See my Q-basics demo download below. Check Chapter 12 for scan codes and a multikey code example. Chapter 13 shows how the GET and PUT routines can move sprites on a background. Even diagonally.

Code: Select all

DIM SC(127) AS INTEGER
DO
   t! = TIMER + .05
   DO         ' 1 Tick (1/18th second) keypress scancode read loop
     a$ = INKEY$ ' So the keyboard buffer won't get full and beep
     code% = INP(&H60) ' Get keyboard scan code from port 96
     IF code% < 128 THEN SC(code%) = 1 ELSE SC(code% - 128) = 0 ' place true/false values into array
   LOOP UNTIL  TIMER > t!' loop until one tick has passed

   ' coordinate change code

   ' GET and PUT code

LOOP UNTIL SC(1) = 1 'escape exit
Full code here: http://qb64.net/wiki/index.php?title=Scancodes

You can find the key codes you need in a simple INP print loop. It also returns the key release codes which are 128 more than the press codes.

Posted: Wed Jun 16, 2010 5:41 pm
by DrNick13
Thanks! :D