Help with another 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 another code!

Post by Sinuvoid » Thu Jul 26, 2007 9:43 am

If you run this code and direct teh little "man" to the right he'll stop for some reason, can someone figure out how to make him go PAST the invisble wall?
by teh way controls:
"w" = up
"s" = down
"a" = left
"d" = right
"q" = quit

Code: Select all


CLS
DIM field$(15)
field$(1) = " __________________________________________________"
field$(2) = "|                                                  |"
field$(3) = "|                                                  |"
field$(4) = "|                                                  |"
field$(5) = "|                                                  |"
field$(6) = "|                                                  |"
field$(7) = "|                                                  |"
field$(8) = "|                                                  |"
field$(9) = "|                                                  |"
field$(10) = "|                                                  |"
field$(11) = "|                                                  |"
field$(12) = "|                                                  |"
field$(13) = "|                                                  |"
field$(14) = "|                                                  |"
field$(15) = "|__________________________________________________|"
x = 7
y = 7
enemyx = 2
enemyy = 2
FOR mz = 1 TO 15
PRINT field$(mz)
NEXT mz
DO
kp$ = INKEY$
oldx = x
oldy = y
LOCATE y, x
PRINT ".0."
LOCATE y, x
PRINT "   "
IF kp$ = "q" THEN END
IF kp$ = "w" THEN y = y - 1
IF kp$ = "s" THEN y = y + 1
IF kp$ = "a" THEN x = x - 1
IF kp$ = "d" THEN x = x + 1
LOCATE y, x
PRINT ".0."
IF MID$(field$(x), y, 1) = "|" THEN
LOCATE y, x
PRINT "___"
x = oldx
y = oldy
END IF
IF MID$(field$(x), y, 1) = "_" THEN
LOCATE y, x
PRINT "|"
x = oldx
y = oldy
END IF
LOOP
Thanks in advance :D :D :D :D

User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves » Thu Jul 26, 2007 10:15 am

Here's the 2 lines with the problem:

Code: Select all

IF MID$(field$(x), y, 1) = "|" THEN 
...
IF MID$(field$(x), y, 1) = "$$$" THEN
To fix the issue, switch the x and the y, so the lines should be changed to:

Code: Select all

IF MID$(field$(y), x, 1) = "|" THEN 
...
IF MID$(field$(y), x, 1) = "$$$" THEN
The problem is that when you set up your field$ array, each array element refers to a different row on the screen, but the x variable refers to columns.

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

Post by Sinuvoid » Thu Jul 26, 2007 10:38 am

Thanks again, that just solved some of my other problems =)
but heres another one. when you "walk" over to the right side (where the "|'s" are) I can walk over them THEN it stops me, i need it to stop me before i hit it like the other walls =)
P.S. run the code to see what i mean

Code: Select all

CLS
DIM field$(15)
field$(0) = "                                                   "
field$(1) = " __________________________________________________"
field$(2) = "|                                                  | "
field$(3) = "|                                                  | "
field$(4) = "|                                                  | "
field$(5) = "|                                                  | "
field$(6) = "|                                                  | "
field$(7) = "|                                                  | "
field$(8) = "|                                                  | "
field$(9) = "|                                                  | "
field$(10) = "|                                                  | "
field$(11) = "|                                                  | "
field$(12) = "|                                                  | "
field$(13) = "|                                                  | "
field$(14) = "|                                                  | "
field$(15) = "|__________________________________________________| "

health = 20
hx = 20
hy = 20
x = 7
y = 7
enemyx = 10
enemyy = 10
kp$ = INKEY$

DO
LOCATE 1, 1
FOR mz = 1 TO 15
PRINT field$(mz)
NEXT mz

LOCATE hy, hx
PRINT health

LOCATE y, x
PRINT ".8."

LOCATE enemyy, enemyx
PRINT "|8."
IF ((x = enemyx) AND (y = enemyy)) THEN
health = health - 1
END IF

oldx = x
oldy = y

DO
kp$ = INKEY$
LOOP UNTIL kp$ <> ""

IF kp$ = "q" THEN END
IF kp$ = "w" THEN y = y - 1
IF kp$ = "s" THEN y = y + 1
IF kp$ = "a" THEN x = x - 1
IF kp$ = "d" THEN x = x + 1
IF MID$(field$(y), x, 1) = "|" THEN
LOCATE y, x
PRINT "|"
x = oldx
y = oldy
END IF
IF MID$(field$(y), x, 1) = "_" THEN
LOCATE y, x
PRINT "___"
x = oldx
y = oldy
END IF
IF health <= 0 THEN
LOCATE 12, 40
PRINT "YOU'RE DEAD!"
END IF
LOOP

User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves » Thu Jul 26, 2007 12:09 pm

Just need to check the character's position before moving him. Since he takes up 3 spaces (".8."), we have to be careful when checking if he bumps into the far right wall, because his (x,y) position only defines where drawing the man begins. He extends for another 2 positions, so you need to check for collision with the wall a couple columns earlier.

Here's the code that was changed to fix the issue:

IF kp$ = "w" AND y - 1 > 1 THEN y = y - 1
IF kp$ = "s" AND y <14> 0 THEN x = x - 1
IF kp$ = "d" AND x + 3 < 52 THEN x = x + 1

And here's the completed code:

Code: Select all

CLS
DIM field$(15)
field$(0) = "                                                   "
field$(1) = " $$$"
field$(2) = "|                                                  | "
field$(3) = "|                                                  | "
field$(4) = "|                                                  | "
field$(5) = "|                                                  | "
field$(6) = "|                                                  | "
field$(7) = "|                                                  | "
field$(8) = "|                                                  | "
field$(9) = "|                                                  | "
field$(10) = "|                                                  | "
field$(11) = "|                                                  | "
field$(12) = "|                                                  | "
field$(13) = "|                                                  | "
field$(14) = "|                                                  | "
field$(15) = "|$$$| "

health = 20
hx = 20
hy = 20
x = 7
y = 7
enemyx = 10
enemyy = 10
kp$ = INKEY$

DO
LOCATE 1, 1
FOR mz = 1 TO 15
PRINT field$(mz)
NEXT mz

LOCATE hy, hx
PRINT health

LOCATE y, x
PRINT ".8."

LOCATE enemyy, enemyx
PRINT "|8."
IF ((x = enemyx) AND (y = enemyy)) THEN
health = health - 1
END IF

oldx = x
oldy = y

DO
kp$ = INKEY$
LOOP UNTIL kp$ <IF> 1 THEN y = y - 1
IF kp$ = "s" AND y <14> 0 THEN x = x - 1
IF kp$ = "d" AND x + 3 < 52 THEN x = x + 1
IF MID$(field$(y), x, 1) = "|" THEN
LOCATE y, x
PRINT "|"
x = oldx
y = oldy
END IF
IF MID$(field$(y), x, 1) = "$$$" THEN
LOCATE y, x
PRINT "$$$"
x = oldx
y = oldy
END IF
IF health <= 0 THEN
LOCATE 12, 40
PRINT "YOU'RE DEAD!"
END IF
LOOP

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

Post by Sinuvoid » Thu Jul 26, 2007 12:18 pm

How did you figure out the calculations for the AND statements?
IF kp$ = "w" AND y - 1 > 1 THEN y = y - 1
IF kp$ = "s" AND y <14> 0 THEN x = x - 1
IF kp$ = "d" AND x + 3 < 52 THEN x = x + 1
By the way the "s" key stops working when i put in the statment for it
so i took it out.

User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves » Thu Jul 26, 2007 1:54 pm

Sorry, didn't notice the code was messed up when I posted. It's a problem with this forum's message board system apparently. When it encounters a less than sign, it must think it's the beginning of a tag or something. I've replaced the troublesome less than sign with LESS THAN below. hope that clarifies what the code should look like.

Corrected code change:
IF kp$ = "w" AND y - 1 > 1 THEN y = y - 1
IF kp$ = "s" AND y LESS THAN 14 THEN y = y + 1
IF kp$ = "d" AND x + 3 < 52 THEN x = x + 1

Corrected code:
(If you copy and paste this, remember to replace LESS THAN with the actual less than sign.)

Code: Select all

CLS 
DIM field$(15) 
field$(0) = "                                                   " 
field$(1) = " $$$" 
field$(2) = "|                                                  | " 
field$(3) = "|                                                  | " 
field$(4) = "|                                                  | " 
field$(5) = "|                                                  | " 
field$(6) = "|                                                  | " 
field$(7) = "|                                                  | " 
field$(8) = "|                                                  | " 
field$(9) = "|                                                  | " 
field$(10) = "|                                                  | " 
field$(11) = "|                                                  | " 
field$(12) = "|                                                  | " 
field$(13) = "|                                                  | " 
field$(14) = "|                                                  | " 
field$(15) = "|$$$| " 

health = 20 
hx = 20 
hy = 20 
x = 7 
y = 7 
enemyx = 10 
enemyy = 10 
kp$ = INKEY$ 

DO 
LOCATE 1, 1 
FOR mz = 1 TO 15 
PRINT field$(mz) 
NEXT mz 

LOCATE hy, hx 
PRINT health 

LOCATE y, x 
PRINT ".8." 

LOCATE enemyy, enemyx 
PRINT "|8." 
IF ((x = enemyx) AND (y = enemyy)) THEN 
health = health - 1 
END IF 

oldx = x 
oldy = y 

DO 
kp$ = INKEY$ 
LOOP UNTIL kp$ <IF> 1 THEN y = y - 1 
IF kp$ = "w" AND y - 1 > 1 THEN y = y - 1
IF kp$ = "s" AND y LESS THAN 14 THEN y = y + 1
IF kp$ = "a" AND x - 1 > 0 THEN x = x - 1
IF kp$ = "d" AND x + 3 < 52 THEN x = x + 1 
IF MID$(field$(y), x, 1) = "|" THEN 
LOCATE y, x 
PRINT "|" 
x = oldx 
y = oldy 
END IF 
IF MID$(field$(y), x, 1) = "$$$" THEN 
LOCATE y, x 
PRINT "$$$" 
x = oldx 
y = oldy 
END IF 
IF health <= 0 THEN 
LOCATE 12, 40 
PRINT "YOU'RE DEAD!" 
END IF 
LOOP

User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves » Thu Jul 26, 2007 2:25 pm

Lee wrote:How did you figure out the calculations for the AND statements?
Your game board is 15 rows by 52 columns, so when we check whether we can move the guy to the next space, we go through the following for each direction:

1. Move LEFT (x=x-1)

Make sure our x value stays to the right of column 0 on the screen, so check that x-1 > 0. Hence the code:

IF kp$ = "a" AND x - 1 > 0 THEN x = x - 1

2. Move RIGHT (x=x+1)

Make sure our x value stays to the left of the last column (52) on the screen, so check that x+1 LESS THAN 52. Hence the code:

IF kp$ = "d" AND x + 3 LESS THAN 52 THEN x = x + 1

3. Move UP (y=y-1)

Make sure our y value stays below row 1 on the screen, so check that y-1 > 1. Hence the code:

IF kp$ = "w" AND y - 1 > 1 THEN y = y - 1

4. Move DOWN (y=y+1)

Make sure our y value stays above the last row (15) on the screen, so check that y+1 LESS THAN 15. Hence the code:

IF kp$ = "s" AND y + 1 LESS THAN 15 THEN y = y + 1

(***IF kp$ = "s" AND y LESS THAN 14 THEN y = y + 1 accomplishes the same thing.)


Sometimes, it helps make the code clearer if you define variables for the beginning and last screen rows and columns at the start of the program and refer to those variables in your collision checking:

Code: Select all

firstScreenRow = 1
lastScreenRow = 15
firstScreencolumn = 0
lastScreencolumn = 52
...
IF kp$ = "w" AND y - 1 > firstScreenRow THEN y = y - 1 
IF kp$ = "s" AND y + 1 LESS THAN lastScreenRow THEN y = y + 1 
IF kp$ = "a" AND x - 1 > firstScreenColumn THEN x = x - 1 
IF kp$ = "d" AND x + 3 < lastScreenColumn THEN x = x + 1

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

Post by Sinuvoid » Thu Jul 26, 2007 7:16 pm

Sorry for bugging you AGAIN but when my life goes down past 10 the " / 20" changes to "/ 200" and another question is how do i make an enemy pop up after an amount of time?
run the code to see what i mean

Code: Select all

 
CLS
DIM field$(15)
field$(0) = "                                                   "
field$(1) = " __________________________________________________"
field$(2) = "|                                                  | "
field$(3) = "|                                                  | "
field$(4) = "|                                                  | "
field$(5) = "|                                                  | "
field$(6) = "|                                                  | "
field$(7) = "|                                                  | "
field$(8) = "|                                                  | "
field$(9) = "|                                                  | "
field$(10) = "|                                                  | "
field$(11) = "|                                                  | "
field$(12) = "|                                                  | "
field$(13) = "|                                                  | "
field$(14) = "|                                                  | "
field$(15) = "|__________________________________________________| "

hx = 25
hy = 16
kp$ = INKEY$

intro:
CLS
DO

health = 20
x = 7
y = 7
enemyx = 10
enemyy = 10

kp$ = INKEY$
COLOR INT(RND * 15) + 1
LOCATE 8, 10
PRINT "Welcome to RUN AWAY!"
LOCATE 14, 10
COLOR 15
PRINT "1 Play"
LOCATE 16, 10
COLOR 6
PRINT "2 Instructions"
LOCATE 18, 10
COLOR 3
PRINT "3 Quit"
IF kp$ = "1" THEN GOTO game
IF kp$ = "2" THEN GOTO instruc
IF kp$ = "3" THEN END
LOOP

instruc:
CLS
COLOR 15
FOR d = 1 TO 5
PRINT
NEXT d
PRINT "Oneday the guards of a town caught a man trying to a steal an apple, so then he    had to RUN AWAY! (This is where YOU come in =>)"
PRINT
PRINT
PRINT "CONTROLS"
PRINT "w = up"
PRINT "s = down"
PRINT "a = left"
PRINT "d = right"
DO
kp$ = INKEY$
COLOR 2
LOCATE 10, 25
PRINT ".8. = YOU"
LOCATE 12, 25
COLOR 4
PRINT "|8. = ENEMY!"
IF kp$ = "q" THEN GOTO intro
LOOP

game:
CLS
DO
LOCATE 1, 1
FOR mz = 1 TO 15
COLOR 7
PRINT field$(mz)
NEXT mz

LOCATE hy, hx
COLOR 10                 '<<<<--------------HERE!!!!!!!!!!!!
PRINT health; "/ 20"

LOCATE y, x
COLOR 15
PRINT ".8."

LOCATE enemyy, enemyx
COLOR 12
PRINT "|8."
IF ((x = enemyx) AND (y = enemyy)) THEN
health = health - 1
END IF

oldx = x
oldy = y

DO
kp$ = INKEY$
LOOP UNTIL kp$ <IF> 1 THEN y = y - 1
IF kp$ = "s" THEN y = y + 1
IF kp$ = "a" THEN x = x - 1
IF kp$ = "d" AND x + 3 < 52 THEN x = x + 1
IF MID$(field$(y), x, 1) = "|" THEN
LOCATE y, x
PRINT "|"
x = oldx
y = oldy
END IF
IF MID$(field$(y), x, 1) = "_" THEN
LOCATE y, x
PRINT "___"
x = oldx
y = oldy
END IF
IF health = 0 THEN
LOCATE 8, 10
COLOR 12
PRINT ",`YOU'RE DEAD!'."
LOCATE 9, 10
PRINT "`,            ',"
SLEEP 2
GOTO intro
END IF
' ghost AI
IF RND < .1 THEN
oldy = enemyy
oldx = enemyx
SELECT CASE enemyx
CASE IS <x> x
enemyx = enemyx - 1
END SELECT
SELECT CASE enemyy
CASE IS <y> y
enemyy = enemyy - 1
END SELECT
IF MID$(field$(enemyy), enemyx, 1) = "_" THEN
enemyx = oldx
enemyy = oldy
LOCATE enemyy, enemyx
PRINT "_"
END IF
END IF
LOOP

nkk_kan
Veteran
Posts: 57
Joined: Thu Jun 01, 2006 10:45 am
Location: Gujrat,India,Asia
Contact:

Post by nkk_kan » Fri Jul 27, 2007 5:43 am

just change this line

if health = 0

to

if health <= 0

so even if the health goes below 0 , it will kill the player.
and if you're planning to make this a big rpg sort of game..
you should use a variable called MaxHealth which stores "20" and print the variable instead of "/20"..

and instead of using labels like "instruc:"...you should use Subs..they're easier and look tidier..
a lot of "GOTO"'s in your program can give you headaches later on..

and about "making an enemy pop up after an amount of time"..
you can use a variable and put in loop and randomize it ..when its value is some specific value..pop a enemy..get it? if you don't...try vic's tuts..


i would recommend you ..."Vic's tutorials" ..they're in Download section of pete's site..they're really helpful

nkk_kan
Veteran
Posts: 57
Joined: Thu Jun 01, 2006 10:45 am
Location: Gujrat,India,Asia
Contact:

Post by nkk_kan » Fri Jul 27, 2007 5:53 am

oops not Download section :oops:

anyways here's the link..

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

and that Darkdread's tutorial is also excellent..

Post Reply