## game help

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

Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

### game help

ok well I was practicing my programming, so one of the challenges was to move an ascii character from one side of the screen to the other, pretty easy. So I decided to make a game out of it.

I think i did pretty well on it (for as simple as it was). But I found it didn't run smoothly, like when you would hold down the arrow key or something like that. This happened to me also when i was making a pacman clone.

Are there any techniques to make my games work better? thanx.

*******************************************************

Code: Select all

``````CLS

playerx = 40
playery = 23
row = 12
column = 79

DO

FOR column = 1 TO 79

keyed\$ = INKEY\$

LOCATE playery, playerx
PRINT CHR\$(153)

oldplayery = playery
oldplayerx = playerx

IF keyed\$ = CHR\$(0) + CHR\$(72) THEN playery = playery - 1
IF keyed\$ = CHR\$(0) + CHR\$(80) THEN playery = playery + 1
IF keyed\$ = CHR\$(0) + CHR\$(75) THEN playerx = playerx - 1
IF keyed\$ = CHR\$(0) + CHR\$(77) THEN playerx = playerx + 1
IF keyed\$ = CHR\$(27) THEN END

LOCATE oldplayery, oldplayerx
PRINT " "

LOCATE playery, playerx
PRINT CHR\$(153)

LOCATE row, column
PRINT " "; CHR\$(141)
LOCATE row + 1, column
PRINT " "; CHR\$(140); CHR\$(140)
LOCATE row + 2, column
PRINT " "; CHR\$(144)
FOR nothing = 1 TO 50000
NEXT

IF row = playery OR row + 1 = playery OR row + 2 = playery AND column = playerx THEN
PRINT "HA HA! The ASCII symbols caught you!"
PRINT "YOu LOsE!"
END
END IF

NEXT

LOOP

``````
Fruit Pickin'

Anonymous
Hi

I ran your code and didn't see any problem other than the collision detection not working due to lack of () in the if. The only thing I can think of that you mean is that you either want a more responsive keyboard input or to not use for next for a delay. Here are a couple of tuts

http://www.petesqbsite.com/sections/tut ... ASTKEY.BAS

http://www.petesqbsite.com/sections/tut ... yboard.txt

http://www.petesqbsite.com/sections/tut ... -time.html

http://www.petesqbsite.com/sections/tut ... misc.shtml

Good luck

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:
Raspberrypicker wrote:But I found it didn't run smoothly, like when you would hold down the arrow key or something like that. This happened to me also when i was making a pacman clone.
That could be from using INKEY\$ for your input. INKEY\$ is only best for simple keyboard input. If you want instantaneous multi-key-combo handling, then you'll have to use something more advanced. You would need a library or some assembler handle for QBASIC. With FreeBASIC, you can just use MULTIKEY.
Raspberrypicker wrote:Are there any techniques to make my games work better? thanx.
Well, you're statement is a little broad, but I can give you some pointers.

Code: Select all

``FOR nothing = 1 TO 50000 ``
This isn't a good idea. It's fine if you're temporarily testing something on your computer, but if you want other people to play your game the way it's intended, then you need to use something more constant for speed control. Because code like that will run at different speeds on other computers. It was flying by blazing fast on mine. I had to adjust it, but the point is that I shouldn't have to. Since you're in text mode, using timers to control speed would probably be the best option. If graphics mode, waiting for the vertical retrace also works.

You shouldn't have to draw the player twice in the same loop. Just choose one place to draw and it should still work fine. Make sure wherever you put the drawing code, that the erasing code should be right behind it.

If you want to reduce flicker, only erase and redraw the player if he moved. There's no need to constantly do that if the player's not moving. The same goes for any other moving objects.

Also, I noticed an error in your logic:

Code: Select all

``IF row = playery OR row + 1 = playery OR row + 2 = playery AND column = playerx THEN ``
Should be:

Code: Select all

``IF (row = playery OR row + 1 = playery OR row + 2 = playery) AND column = playerx THEN ``
The difference is that there are parenthesis around the OR statements. If you don't do this, then the compiler will interpret each case in between the ORs as separate, and if any of them are true it'll execute the code inside the block whether the last case is true or not . So if "row = playery" then it won't matter if "column = playerx".

Besides that, it's not too shabby.

Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida
Thanx guys, this was extremely helpful.
Fruit Pickin'