Help!

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
Guest
Help!

Post by Guest » Sun Jul 17, 2005 12:38 am

I'm just getting into games with QBASIC. i'm using 1.1 and trying to draw a blue and red box. When the blue box touches the red box you win. but whenever I run this program, I always win without doing anything. Please give suggestions or whatever. Here's the code:

Code: Select all

Start:
SCREEN 12
WIDTH (80)
x = 15
y = 40
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE 2, 79
COLOR 4
PRINT CHR$(219)

Direction:

SUB Direction
Begin:
SCREEN 12
WIDTH (80)
CLS
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE 2, 79
COLOR 4
PRINT CHR$(219)

xd = 0
yd = 0
IF INKEY$ = "H" THEN xd = -1
IF INKEY$ = "P" THEN xd = 1
IF INKEY$ = "K" THEN yd = -1
IF INKEY$ = "M" THEN yd = 1
x = x + xd
y = y + yd
IF INKEY$ = "H" OR INKEY$ = "P" OR INKEY$ = "K" OR INKEY$ = "M" THEN GOTO Begin ELSE GOTO Finish
Finish:
LOCATE 15, 26
COLOR 1
PRINT "You win!"
END SUB

buff1

Post by buff1 » Sun Jul 17, 2005 3:58 am

Not into games myself but one observation.

You would have to have fingers moving as fast as the computer to keep
it from going to finish

You might put inkey$ in a variable a$=inkey$
then check a$

Also, if inkey$ or a$ is "" what should it do ?

Your four statements
IF INKEY$ = "H" THEN xd = -1
IF INKEY$ = "P" THEN xd = 1
IF INKEY$ = "K" THEN yd = -1
IF INKEY$ = "M" THEN yd = 1

WOULD all be checking a different inkey$ (even though they will probably all be the same) Confusing ? each statement checks inkey$ anew. Inkey$
does not keep the value like other string variables.

That's why you see statments similar to
a$=""
while a$="":a$=inkey$:wend
at this point a$ has a value other than null.
and it's whatever you pressed on the keyboard.

Guest

Post by Guest » Sun Jul 17, 2005 4:19 pm

I don't get it. Where would I put that code? I tried it but it doesn't seem to work.

Guest

Post by Guest » Sun Jul 17, 2005 5:13 pm

I figured out why it wasn't working and fixed it. Only problem now, nothing moves when you hit the arrow keys. Here's the code:

Code: Select all

DECLARE SUB Direction ()
Start:
SCREEN 12
WIDTH (80)
x = 15
y = 40
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE 2, 79
COLOR 4
PRINT CHR$(219)

Direction:
Direction

SUB Direction
Begin:
SCREEN 12
WIDTH (80)
a$ = ""
WHILE a$ = ""
a$ = INKEY$
x = 15
y = 40
x = x + xd
y = y + yd
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE 2, 79
COLOR 4
PRINT CHR$(219)

xd = 0
yd = 0
IF INKEY$ = "H" THEN xd = -1
IF INKEY$ = "P" THEN xd = 1
IF INKEY$ = "K" THEN yd = -1
IF INKEY$ = "M" THEN yd = 1
' H = up arrow
' P = down arrow
' K = left arrow
' M = right arrow
x = x + xd
y = y + yd
IF INKEY$ = "H" OR INKEY$ = "P" OR INKEY$ = "K" OR INKEY$ = "M" THEN GOTO Begin
IF x = 2 AND y = 79 THEN GOTO Finish ELSE GOTO Begin
WEND
Finish:
CLS
COLOR 15
PRINT "You won!"
END SUB

buff1

Post by buff1 » Sun Jul 17, 2005 5:35 pm

Instead of If inkey$="H".....
Try If A$="H" ... etc.

buff1

Post by buff1 » Sun Jul 17, 2005 5:39 pm

Since you have it in a while loop I don't think you need the goto begin
besides the fact that that is dangerous to use a goto to exit a while loop.
and that you don't need to re-state screen 12 (done when going back to begin).

buff1

Post by buff1 » Sun Jul 17, 2005 5:42 pm

Instead, why not test for yd after the wend. at that poing you can GOTO
just above the while wend statement.

Guest

Post by Guest » Sun Jul 17, 2005 6:09 pm

That didn't work. You know, you'd think this would be easy but the computer has to be stupid.

Guest

Post by Guest » Sun Jul 17, 2005 6:22 pm

You know... since I can't get it to work, can somebody just write a code from scratch that place a blue charecter on the screen and a red charecter on the screen and when they touch, it prints "you win!"
That'd help a lot.

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Mon Jul 18, 2005 11:33 pm

This one works. If you like it, study it and ask questions, such as "Why is this and that?" That way, you will learn more.

Code: Select all

CLS
PRINT " CHASE, a simple QuickBASIC program, in which a blue rectangle is ";
PRINT "moved until"
PRINT " it touches a red rectangle."
PRINT


   'begin program
PRINT " Press <Esc> to terminate program, Press arrow keys to navigate"
k$ = "": WHILE k$ = "": k$ = INKEY$: WEND

  
   'make screen all white
BG = 15 'BackGround is white
FG = 1  'ForeGround is blue
COLOR BG, FG
CLS
FOR r = 1 TO 22   'rows
  FOR c = 1 TO 80 'columns
    PRINT CHR$(219);
  NEXT c
  PRINT
NEXT r


   'initial positions:
'The syntax for LOCATE is: LOCATE line #, column #, or LOCATE y,x, so:
 'blue rectangle
y = 5  'note that y is normally vertical, or rows
x = 10  'note that x is normally horizontal, or columns
LOCATE y, x: COLOR 1: PRINT CHR$(219)
 'red rectangle
yred = 10
xred = 40
LOCATE yred, xred: COLOR 4: PRINT CHR$(219)


   'navigate with the arrow keys
again:
k$ = "": WHILE k$ = "": k$ = INKEY$: WEND
IF k$ = CHR$(27) THEN SYSTEM
IF LEFT$(k$, 1) <> CHR$(0) THEN GOTO again: 'an arrow key was not pressed

LOCATE y, x: COLOR BG: PRINT CHR$(219) 'erase old blue rectangle
k$ = RIGHT$(k$, 1)
IF k$ = "H" THEN y = y - 1
IF k$ = "P" THEN y = y + 1
IF k$ = "M" THEN x = x + 1
IF k$ = "K" THEN x = x - 1

 'do not allow off limits values
IF x < 1 THEN x = x + 1
IF y < 1 THEN y = y + 1
IF x > 80 THEN x = x - 1
IF y > 22 THEN y = y - 1
'''PRINT x, y 'used for debugging only
IF y = yred AND x = xred THEN GOTO fin:
LOCATE y, x: COLOR 1: PRINT CHR$(219)
GOTO again:

 
'terminate program
fin:
LOCATE 3, 10: PRINT "Hooray, you won!"
SYSTEM
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Guest

Post by Guest » Tue Jul 19, 2005 4:00 pm

Thanks! But I uh....kinda got it to work like, 5 minutes ago. But thanks for the help anyway!

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Tue Jul 19, 2005 11:51 pm

Destructo2008:
That's very good that you "got it to work". But, remember that "getting it to work" is not the same as "knowing why it works". Do study your program that works, and try to understand each line of code, what it does, how it does it, and, could you use other, perhaps easier to understand or, maybe, even reduce the number of lines of code that you are using. Doing this to your program will teach you more than you can imagine!
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Post Reply