Help with my code

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
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Help with my code

Post by Sinuvoid »

Can someone help me get rid of the "sprite" for Case's "w" and "s"? When I move up the last sprite stays there.

Code: Select all

CLS
SCREEN 13
DO
RANDOMIZE TIMER
LOCATE 12, 40
COLOR INT(RND * 15) + 1
PRINT "Welcome to Runaway!"
LOCATE 14, 40
PRINT "Press any key to continue"
kp$ = INKEY$
LOOP UNTIL kp$ <> ""
'Playing field
CLS
DIM field$(11)
field$(1) = " ________________________  "
field$(2) = "|                        |"
field$(3) = "|                        |"
field$(4) = "|                        |"
field$(5) = "|                        |"
field$(6) = "|                        |"
field$(7) = "|                        |"
field$(8) = "|                        |"
field$(9) = "|                        |"
field$(10) = "|                        |"
field$(11) = "|________________________|"
FOR area = 1 TO 11
LINE (0, 0)-(26, 11), 2, BF
COLOR 8
PRINT field$(area)
NEXT area
'warrior/runningman
x = 9
y = 9
LOCATE x, y
COLOR 4
PRINT ".0."
DO
kp$ = INKEY$
oldrow = x
oldcolumn = y
SELECT CASE kp$
{HERE)CASE IS = "w"
x = x - 1
LOCATE x, y
COLOR 4
PRINT ".0."
{AND HERE}CASE IS = "s"
x = x + 1
LOCATE x, y
COLOR 4
PRINT "   .0.   "
CASE IS = "a"
y = y - 1
LOCATE x, y
COLOR 4
PRINT " (.| "
CASE IS = "d"
y = y + 1
LOCATE x, y
COLOR 4
PRINT " |.) "
END SELECT
IF MID$(field$(x), y, 1) = "_" THEN
x = oldrow
y = oldcolumn
END IF
IF MID$(field$(x), y, 1) = "|" THEN
x = oldrow
y = oldcolumn
END IF
LOOP
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

The LOCATE command

Post by Stoves »

Looks like the LOCATE command is giving you some trouble. Unlike ALL the other QB commands that use (x,y) coordinates where the first coordinate is the horizontal position and the second coordinate is the vertical position, with LOCATE it's exactly the opposite. The first coordinate is the up and down position and the second coordinate is the left and right position. (0,0) is still top left corner and (25,40) is bottom right corner of the screen in screen 13.

I'd recommend switching all the x and y variables, since it's a lot easier to think of the y as the vertical row position and the x as the horizontal column position. Then it should be easier to clear the sprite when it moves.

Good luck.
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

Still doesnt solve how to get rid of the sprite problem. :?
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves »

Try out this code. I made 3 changes:

1. Switched x and y variables for clarity.
2. Added the section marked with 'BEGIN NEW CODE which erases the character if a key is pressed before changing the position.
3. Removed the extra spaces from the lines that draw the character.

Code: Select all

CLS 
SCREEN 13 
DO 
RANDOMIZE TIMER 
LOCATE 12, 40 
COLOR INT(RND * 15) + 1 
PRINT "Welcome to Runaway!" 
LOCATE 14, 40 
PRINT "Press any key to continue" 
kp$ = INKEY$ 
LOOP UNTIL kp$ <> "" 
'Playing field 
CLS 
DIM field$(11) 
field$(1) = " $$$  " 
field$(2) = "|                        |" 
field$(3) = "|                        |" 
field$(4) = "|                        |" 
field$(5) = "|                        |" 
field$(6) = "|                        |" 
field$(7) = "|                        |" 
field$(8) = "|                        |" 
field$(9) = "|                        |" 
field$(10) = "|                        |" 
field$(11) = "|$$$|" 
FOR area = 1 TO 11 
LINE (0, 0)-(26, 11), 2, BF 
COLOR 8 
PRINT field$(area) 
NEXT area 
'warrior/runningman 
y = 9 
x = 9 
LOCATE y, x 
COLOR 4 
PRINT ".0." 
DO 
kp$ = INKEY$ 
oldrow = y 
oldcolumn = x 
'BEGIN NEW CODE----
IF kp$ <> "" THEN   'If no key was pressed, don't clear sprite
  LOCATE y, x
  PRINT "   "
END IF
'END NEW CODE------
SELECT CASE kp$ 
CASE IS = "w" 
y = y - 1 
LOCATE y, x 
COLOR 4 
PRINT ".0." 
CASE IS = "s" 
y = y + 1 
LOCATE y, x 
COLOR 4 
PRINT ".0." 
CASE IS = "a" 
x = x - 1 
LOCATE y, x 
COLOR 4 
PRINT "(.|" 
CASE IS = "d" 
x = x + 1 
LOCATE y, x 
COLOR 4 
PRINT "|.)" 
END SELECT 
IF MID$(field$(y), x, 1) = "$$$" THEN 
y = oldrow 
x = oldcolumn 
END IF 
IF MID$(field$(y), x, 1) = "|" THEN 
y = oldrow 
x = oldcolumn 
END IF 
LOOP
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

THANK YOU SO MUCH STOVE!!!!!!!!
P.S. the reason why im asking so much is because im newbie (obvious) and been using QB for a week =)
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves »

You're welcome! Glad to help.
Post Reply