Ad blocker detected: Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker on our website.
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:
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
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.
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
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).
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.
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.
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.