Help structuring movement (RPG)

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Spivey38
Newbie
Posts: 2
Joined: Tue Sep 11, 2012 4:49 pm

Help structuring movement (RPG)

Post by Spivey38 »

Hello! I am an beginner level coder using Qbasic. I am currently attemping to make a RPG for my young son so when he is old enough to read I should have something playable hehe. It is a very simple RPG using PUT/GET tiles, characters etc. The problem i have is animating the enemies on the screen. My code is set up like this...

Program looks for a keyboard action
If none, it moves the enemy a tile in a direction
Repeat

You can probably see the problem i am having. It scans the keyboard for input and moves the enemy about a thousand times per second! I can put a delay in (for d=1 to 1000000:next d). It will slow everything down, but it wont always catch a keyboard input if it is pressed during the delay part.

The code for keyboard commands (move/inventory/gear etc) i got from another Qbasic site and reads...

DO
press$=INKEY$
IF press$="I" then inventory '(subroutine)
IF press$="s" then savegame '(subroutine)

<ENEMY>

LOOP UNTIL press=CHR$(27) 'loops forever until i press escape

I have the enemy movement code sandwiched in the loop which makes it go way to fast. If i put that chunk of code outside the loop, the enemy never moves because every action is contained in the loop. Not sure what to do here. If you need an example of what i want, think the original Legend of Zelda =)

Thanks alot!

-Bryan
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

DO
K$ = INKEY$

IF LEN(K$) THEN

'move enemy

'move sprite

END IF
LOOP

The sprite would move the key direction pressed. The enemy would move toward the current sprite position. If you want to be able to move diagonally use INP(96) scan codes:

http://qb64.net/wiki/index.php?title=Keyboard_scancodes

Code: Select all

DECLARE SUB Delay (dlay!)
DECLARE FUNCTION ScanKey% (code%)                   'NOT required in QB64

CLS
x = 40: px = x
y = 20: py = y
xx = 41: pxx = xx
yy = 21: pyy = yy

DO: Delay .05 ' program or game loop
  COLOR 15
  LOCATE 8, 14: PRINT "W A S D": LOCATE 8, 52: PRINT "ARROW PAD"
  IF ScanKey%(17) THEN
    LOCATE 2, 15: PRINT " UP "
    IF y > 1 THEN y = y - 1
  ELSE LOCATE 2, 15: PRINT "----" 'W key
  END IF
  IF ScanKey%(31) THEN
    LOCATE 6, 15: PRINT "DOWN"
    IF y < 25 THEN y = y + 1
  ELSE LOCATE 6, 15: PRINT "----" 'S key
  END IF
  IF ScanKey%(30) THEN
    LOCATE 4, 12: PRINT "LEFT"
    IF x > 1 THEN x = x - 1
  ELSE LOCATE 4, 12: PRINT "----" 'A key
  END IF
  IF ScanKey%(32) THEN
    LOCATE 4, 18: PRINT "RIGHT"
    IF x < 80 THEN x = x + 1
  ELSE LOCATE 4, 18: PRINT "---- " 'D key
  END IF
  IF ScanKey%(72) THEN
    LOCATE 2, 55: PRINT " UP "
    IF yy > 1 THEN yy = yy - 1
  ELSE LOCATE 2, 55: PRINT "----" 'arrow
  END IF
  IF ScanKey%(80) THEN
    LOCATE 6, 55: PRINT "DOWN"
    IF yy < 25 THEN yy = yy + 1
  ELSE LOCATE 6, 55: PRINT "----" 'arrow
  END IF
  IF ScanKey%(75) THEN
    LOCATE 4, 52: PRINT "LEFT"
    IF xx > 1 THEN xx = xx - 1
  ELSE LOCATE 4, 52: PRINT "----" 'arrow
  END IF
  IF ScanKey%(77) THEN
    LOCATE 4, 58: PRINT "RIGHT"
    IF xx < 80 THEN xx = xx + 1
  ELSE LOCATE 4, 58: PRINT "---- " 'arrow
  END IF
  LOCATE py, px: PRINT SPACE$(1);
  LOCATE pyy, pxx: PRINT SPACE$(1);
  COLOR 10: LOCATE y, x: PRINT CHR$(1);
  COLOR 12: LOCATE yy, xx: PRINT CHR$(2);
  px = x: py = y: pxx = xx: pyy = yy
LOOP UNTIL ScanKey%(1)
zerocodes% = ScanKey%(0) 'reset all array values to zero for next part of program
END

FUNCTION ScanKey% (scancode%)
STATIC Ready%, keyflags%()
IF NOT Ready% THEN REDIM keyflags%(0 TO 127): Ready% = -1
i% = INP(&H60) 'read keyboard states
IF (i% AND 128) THEN keyflags%(i% XOR 128) = 0
IF (i% AND 128) = 0 THEN keyflags%(i%) = -1
K$ = INKEY$ 'clears key buffer to prevent beeps
ScanKey% = keyflags%(scancode%)
IF scancode% = 0 THEN Ready% = 0 'allows program to reset all values to 0 with a REDIM
END FUNCTION

SUB Delay (dlay!)
start! = TIMER
DO WHILE start! + dlay! >= TIMER
  IF start! > TIMER THEN start! = start! - 86400
LOOP
END SUB
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Spivey38
Newbie
Posts: 2
Joined: Tue Sep 11, 2012 4:49 pm

Thanks

Post by Spivey38 »

I will give this a try when i get time! Thanks alot!
Post Reply