Buggy

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
sha_kai
Newbie
Posts: 1
Joined: Sun May 22, 2005 7:27 am
Location: California

Buggy

Post by sha_kai »

Hi,

I've been thinking of working on a rpg, and so set to work deciding what features I wanted to have. One of the features I decided to include was mobile NPC's, so I set about creating a simple bit of code to facilitate this, however, I'm having some trouble clearing up an anoying little bug that stops the NPC sprite from moving. Please see the code sample posted below.

---------------------------Sample--------------------------------------

DIM SHARED eraser%(100)
DIM SHARED player%(100)
DIM SHARED enemy%(100)

playerx% = 15
playery% = 2
ploldx% = playerx%
ploldy% = playery%
enemyx% = 15
enemyy% = 17
enoldx% = enemyx%
enoldy% = enemyy%
enwait% = 10000

SCREEN 13

LINE (0, 0)-(10, 10), 0, BF
GET (0, 0)-(10, 10), eraser%

'Player pic
LINE (0, 0)-(10, 10), 15, BF
GET (0, 0)-(10, 10), player%

'NPC pic
LINE (0, 0)-(10, 10), 6, BF
GET (0, 0)-(10, 10), enemy%

CLS

PUT (playerx% * 10, playery% * 10), player%, PSET
PUT (enemyx% * 10, enemyy% * 10), enemy%, PSET

RANDOMIZE TIMER

'move the player

DO

SELECT CASE INKEY$

CASE CHR$(0) + CHR$(77)
playerx% = playerx% + 1 'right
ploldx% = playerx% - 1
ploldy% = playery%
GOSUB placeplayer

CASE CHR$(0) + CHR$(75)
playerx% = playerx% - 1 'left
ploldx% = playerx% + 1
ploldy% = playery%
GOSUB placeplayer

CASE CHR$(0) + CHR$(80)
playery% = playery% + 1 'up
ploldy% = playery% - 1
ploldx% = playerx%
GOSUB placeplayer

CASE CHR$(0) + CHR$(72) 'down
playery% = playery% - 1
ploldy% = playery% + 1
ploldx% = playerx%
GOSUB placeplayer

CASE CHR$(27)
quit = 1

END SELECT

'move enemy

enemystep% = enemystep% + 1

IF enemystep% >= enwait% THEN
enemystep% = 0
enemydir% = INT(RND * 4)

SELECT CASE enemydir%

CASE 0
enemyx% = enemyx% + 1
enoldx% = enemyx% - 1
enoldy% = enemyy%
GOSUB placeenemy

CASE 1
enemyx% = enemyx% - 1
enoldx% = enemyx% + 1
enoldy% = enemyy%
GOSUB placeenemy

CASE 2
enemyy% = enemyy% + 1
enoldy% = enemyy% - 1
enoldx% = enemyx%
GOSUB placeenemy

CASE 3
enemyy% = enemyy% - 1
enoldy% = enemyy% + 1
enoldx% = enemyx%
GOSUB placeenemy

END SELECT

END IF

LOOP UNTIL quit = 1

END

placeplayer:

IF playerx% < 1 THEN playerx% = 1
IF playerx% > 30 THEN playerx% = 30
IF playery% < 1 THEN playery% = 1
IF playery% > 18 THEN playery% = 18

PUT (ploldx% * 10, ploldy% * 10), eraser%, PSET
PUT (playerx% * 10, playery% * 10), player%, PSET

'LOCATE 1, 1: PRINT playerx%, playery%
'LOCATE 2, 1: PRINT ploldx%, ploldy%

RETURN

placeenemy:

IF enemyx% < 1 THEN enemyx% = 1
IF enemyx% > 30 THEN enemyx% = 30
IF enemyy% < 1 THEN enemyy% = 1
IF enemyy% > 18 THEN enemyy% = 18

PUT (enoldx% * 10, enoldy% * 10), eraser%, PSET
PUT (enemyx% * 10, enemyy% * 10), enemy%, PSET

'LOCATE 4, 1: PRINT enemydir%
'LOCATE 5, 1: PRINT enemyx%, enemyy%
'LOCATE 6, 1: PRINT enoldx%, enoldy%

RETURN
---------------------------End Sample---------------------------------

Things seem to work fine if enwait% is set to 500 or less, but over that threshold the NPC sprite stops moving until there is some sort of input (ie: a key is pressed), or text output (ie: enabling any of the 'PRINT' commands).

If anyone has any ideas as to what is causing this, and suggestions for how to fix it, I'd appreciate hearing them.

Thanks
See the happy moron,
He doesn't give a damn,
I wish I was a moron,
My god, perhps I am.

~unknown
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

dude... use the code tags, please...
Image
Post Reply