Please use this Board for QBasic related requests ( file research, programming, etc.)
Moderators:Administrator, Global Moderator
-
Guest
Help....again!
Post
by Guest » Thu Jul 21, 2005 4:47 pm
My computer is being stupid. Maybe I enter something wrong but my computer is telling me I have a WEND without a WHILE. Clearly, I have both, but it doesn't get it. Can you help me? Here's the code:
Code: Select all
Start:
SCREEN 0
WIDTH (80)
x = 15
y = 40
x2 = 2
y2 = 79
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
Begin:
SCREEN 0
WIDTH (80)
a$ = ""
WHILE a$ = "" ' HERE"S THE WHILE! SEE I TOLD YOU IT WAS THERE!
Start:
SCREEN 0
WIDTH (80)
x = 15
y = 40
x2 = 2
y2 = 79
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
Begin:
SCREEN 0
WIDTH (80)
a$ = ""
WHILE a$ = ""
x2 = px2
y2 = py2
delay = .5
finish = TIMER + delay
IF TIMER = finish THEN
IF x2 = x THEN x2 = x2
IF y2 = y THEN y2 = y2
IF x2 > x THEN x2 = x2 - 1
IF y2 > y THEN y2 = y2 - 1
IF x2 < x THEN x2 = x2 + 1
IF y2 < y THEN y2 = y2 + 1
' Only one inkey$ call per loop.
a$ = INKEY$
x = x + xd
y = y + yd
IF x > 23 THEN x = 23
IF x < 1 THEN x = 1
IF y < 1 THEN y = 1
IF y > 80 THEN y = 80
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
px = x
py = y
xd = 0
yd = 0
' If we have a two-character code
IF LEN(a$) = 2 THEN
' Get the second character
a2$ = RIGHT$(a$, 1)
' Check against each arrow key
IF a2$ = "H" THEN xd = -.5
IF a2$ = "P" THEN xd = .5
IF a2$ = "K" THEN yd = -.5
IF a2$ = "M" THEN yd = .5
' H = up arrow
' P = down arrow
' K = left arrow
' M = right arrow
x = x + xd
y = y + yd
LOCATE px, py
PRINT " "
LOCATE px2, py2
PRINT " "
END IF
' If an arrow key was pressed
IF xd <> 0 OR yd <> 0 THEN GOTO Begin
IF x = 2 AND y = 79 THEN GOTO finish ELSE GOTO Begin
WEND ' HERE'S THE WEND!
finish:
CLS
LOCATE 12, 39
COLOR 15
PRINT "You won!"
SLEEP 2
CLS
x2 = px2
y2 = py2
delay = .5
finish = TIMER + delay
IF TIMER = finish THEN
IF x2 = x THEN x2 = x2
IF y2 = y THEN y2 = y2
IF x2 > x THEN x2 = x2 - 1
IF y2 > y THEN y2 = y2 - 1
IF x2 < x THEN x2 = x2 + 1
IF y2 < y THEN y2 = y2 + 1
' Only one inkey$ call per loop.
a$ = INKEY$
x = x + xd
y = y + yd
IF x > 23 THEN x = 23
IF x < 1 THEN x = 1
IF y < 1 THEN y = 1
IF y > 80 THEN y = 80
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
px = x
py = y
xd = 0
yd = 0
' If we have a two-character code
IF LEN(a$) = 2 THEN
' Get the second character
a2$ = RIGHT$(a$, 1)
' Check against each arrow key
IF a2$ = "H" THEN xd = -.5
IF a2$ = "P" THEN xd = .5
IF a2$ = "K" THEN yd = -.5
IF a2$ = "M" THEN yd = .5
' H = up arrow
' P = down arrow
' K = left arrow
' M = right arrow
x = x + xd
y = y + yd
LOCATE px, py
PRINT " "
LOCATE px2, py2
PRINT " "
END IF
' If an arrow key was pressed
IF xd <> 0 OR yd <> 0 THEN GOTO Begin
IF x = 2 AND y = 79 THEN GOTO finish ELSE GOTO Begin
WEND
finish:
CLS
LOCATE 12, 39
COLOR 15
PRINT "You won!"
SLEEP 2
CLS
-
buff1
Post
by buff1 » Thu Jul 21, 2005 11:06 pm
Looks like you've got a lot of duplicate code to me. Duplicate line labels,
not enough END IF statements for your multiline ifs.
That's probably what is causing it. (multiline if's without end if).
Be sure you indent to help you see where you should get back to
<PRE>
If something then
a$=inkey$
END IF
</PRE>
-
Dr_Davenstein
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
-
Contact:
Post
by Dr_Davenstein » Fri Jul 22, 2005 3:23 am
I just ran this through FBIde's auto indenter and put the missing keywords in. I also renamed the duplicate labels. However, it still doesn't work. If you look at the indentation, you should be able to figure out where you went wrong. This kind of code is commonly known as
Spaghetti Code because of its unstructured nature. You might think about using SUBs and Functions, instead of GOTOs.
Code: Select all
Start:
SCREEN 0
WIDTH (80)
x = 15
y = 40
x2 = 2
y2 = 79
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
Begin1:
SCREEN 0
WIDTH (80)
a$ = ""
WHILE a$ = "" ' HERE"S THE WHILE! SEE I TOLD YOU IT WAS THERE!
Start1:
SCREEN 0
WIDTH (80)
x = 15
y = 40
x2 = 2
y2 = 79
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
Begin:
SCREEN 0
WIDTH (80)
a$ = ""
WHILE a$ = ""
x2 = px2
y2 = py2
delay = .5
finish = TIMER + delay
IF TIMER = finish THEN
IF x2 = x THEN x2 = x2
IF y2 = y THEN y2 = y2
IF x2 > x THEN x2 = x2 - 1
IF y2 > y THEN y2 = y2 - 1
IF x2 < x THEN x2 = x2 + 1
IF y2 < y THEN y2 = y2 + 1
' Only one inkey$ call per loop.
a$ = INKEY$
x = x + xd
y = y + yd
IF x > 23 THEN x = 23
IF x < 1 THEN x = 1
IF y < 1 THEN y = 1
IF y > 80 THEN y = 80
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
px = x
py = y
xd = 0
yd = 0
' If we have a two-character code
IF LEN(a$) = 2 THEN
' Get the second character
a2$ = RIGHT$(a$, 1)
' Check against each arrow key
IF a2$ = "H" THEN xd = -.5
IF a2$ = "P" THEN xd = .5
IF a2$ = "K" THEN yd = -.5
IF a2$ = "M" THEN yd = .5
' H = up arrow
' P = down arrow
' K = left arrow
' M = right arrow
x = x + xd
y = y + yd
LOCATE px, py
PRINT " "
LOCATE px2, py2
PRINT " "
END IF
' If an arrow key was pressed
IF xd <> 0 OR yd <> 0 THEN GOTO Begin
IF x = 2 AND y = 79 THEN GOTO finish ELSE GOTO Begin
End If
WEND ' HERE'S THE WEND!
finish1:
CLS
LOCATE 12, 39
COLOR 15
PRINT "You won!"
SLEEP 2
CLS
x2 = px2
y2 = py2
delay = .5
finish = TIMER + delay
IF TIMER = finish THEN
IF x2 = x THEN x2 = x2
IF y2 = y THEN y2 = y2
IF x2 > x THEN x2 = x2 - 1
IF y2 > y THEN y2 = y2 - 1
IF x2 < x THEN x2 = x2 + 1
IF y2 < y THEN y2 = y2 + 1
' Only one inkey$ call per loop.
a$ = INKEY$
x = x + xd
y = y + yd
IF x > 23 THEN x = 23
IF x < 1 THEN x = 1
IF y < 1 THEN y = 1
IF y > 80 THEN y = 80
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
px = x
py = y
xd = 0
yd = 0
' If we have a two-character code
IF LEN(a$) = 2 THEN
' Get the second character
a2$ = RIGHT$(a$, 1)
' Check against each arrow key
IF a2$ = "H" THEN xd = -.5
IF a2$ = "P" THEN xd = .5
IF a2$ = "K" THEN yd = -.5
IF a2$ = "M" THEN yd = .5
' H = up arrow
' P = down arrow
' K = left arrow
' M = right arrow
x = x + xd
y = y + yd
LOCATE px, py
PRINT " "
LOCATE px2, py2
PRINT " "
END IF
' If an arrow key was pressed
IF xd <> 0 OR yd <> 0 THEN GOTO Begin
IF x = 2 AND y = 79 THEN GOTO finish ELSE GOTO Begin
End If
finish:
CLS
LOCATE 12, 39
COLOR 15
PRINT "You won!"
SLEEP 2
CLS
Wend
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!
-
Ralph
- QBasic God
- Posts:134
- Joined:Sat Nov 06, 2004 2:27 am
- Location:Katy, Texas
Post
by Ralph » Fri Jul 22, 2005 5:24 am
Hello, Dr_Davenstein! I just finished answering your PM, when I saw this! Precisely, Destructo's posting was my inspiration. I guess I'll post my solution, which works in QuickBASIC with Windows XP, as well as in Free BASIC, here:
Code: Select all
RANDOMIZE TIMER
top:
CLS
PRINT " CHASE, a simple QuickBASIC program, in which a white, moving ";
PRINT "rectangle is "
PRINT " moved until it touches a fixed, red rectangle."
PRINT
'set colors for moving rectangle and fixed rectangle
cmr = 15 'white
cfr = 4 + 8 'bright red
'begin program
top1:
LOCATE 21, 10
PRINT " Press <Esc> to terminate program, <Enter> to continue"
again1:
k$ = "": WHILE k$ = "": k$ = INKEY$: WEND
IF k$ = CHR$(27) THEN GOTO fin:
IF k$ <> CHR$(13) THEN GOTO again1:
'make screen all blue
BG = 1 'BackGround is blue
FG = 15 'ForeGround is white
COLOR BG, FG
CLS
FOR r = 1 TO 22 'rows
FOR c = 1 TO 80 'columns
PRINT CHR$(219); 'fill each row with a white rectangle
NEXT c
NEXT r
'initial positions:
'moving rectangle
y = INT(RND * 21) + 2'note that y is normally vertical, or rows
x = INT(RND * 80) 'note that x is normally horizontal, or columns
'The syntax for LOCATE is: LOCATE row #, column #, or LOCATE y,x
LOCATE y, x: COLOR cmr: PRINT CHR$(219)
'fixed rectangle
yfixed = INT(RND * 21) + 2 'this leaves top two rows for text
xfixed = INT(RND * 80)
LOCATE yfixed, xfixed: COLOR cfr: PRINT CHR$(219)
'navigate with the arrow keys
again2:
k$ = "": WHILE k$ = "": k$ = INKEY$: WEND
IF k$ = CHR$(27) THEN SYSTEM
IF LEN(k$) <> 2 THEN GOTO again2: 'arrow key not pressed
LOCATE y, x: COLOR BG: PRINT CHR$(219) 'erase old white 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 = yfixed AND x = xfixed THEN GOTO won:
LOCATE y, x: COLOR cmr: PRINT CHR$(219)
LOCATE 1, 38: PRINT " ": count = count + 1
LOCATE 1, 38: COLOR 1, 15: PRINT count
GOTO again2:
won:
LOCATE 19, 30: PRINT "Hooray, you won!";
GOTO top1:
'terminate program
fin:
CLS
SYSTEM
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.
-
Dr_Davenstein
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
-
Contact:
Post
by Dr_Davenstein » Fri Jul 22, 2005 7:43 am
Good job!
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!
-
Ralph
- QBasic God
- Posts:134
- Joined:Sat Nov 06, 2004 2:27 am
- Location:Katy, Texas
Post
by Ralph » Fri Jul 22, 2005 8:34 pm
Thank you. I just noticed one little glitch, though. If the randomly-generated values of x and y for both the fixed and the moving reactangle happen to coincide, a probability of 1/(21*80) = 1/1680, the game will simply report the "You won!". I guess I should have provided for this, by ignoring these values and generating new ones, whenever it happens. I'll leave that up to whoever wants to use my code, ha, ha.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.
-
Guest
Post
by Guest » Fri Jul 22, 2005 10:34 pm
I fixed the code by adding an END IF but the stupid red square still won't move. Here's the code again...
Code: Select all
Start:
SCREEN 0
WIDTH (80)
x = 15
y = 40
x2 = 2
y2 = 79
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
Begin:
SCREEN 0
WIDTH (80)
a$ = ""
WHILE a$ = ""
px2 = x2
py2 = y2
delay = .5
finish = TIMER + delay
IF TIMER = finish THEN
IF x2 = x THEN x2 = x2
IF y2 = y THEN y2 = y2
IF x2 > x THEN x2 = x2 - 1
IF y2 > y THEN y2 = y2 - 1
IF x2 < x THEN x2 = x2 + 1
IF y2 < y THEN y2 = y2 + 1
END IF
' Only one inkey$ call per loop.
a$ = INKEY$
x = x + xd
y = y + yd
IF x > 23 THEN x = 23
IF x < 1 THEN x = 1
IF y < 1 THEN y = 1
IF y > 80 THEN y = 80
LOCATE x, y
COLOR 1
PRINT CHR$(219)
LOCATE x2, y2
COLOR 4
PRINT CHR$(219)
px = x
py = y
xd = 0
yd = 0
' If we have a two-character code
IF LEN(a$) = 2 THEN
' Get the second character
a2$ = RIGHT$(a$, 1)
' Check against each arrow key
IF a2$ = "H" THEN xd = -.5
IF a2$ = "P" THEN xd = .5
IF a2$ = "K" THEN yd = -.5
IF a2$ = "M" THEN yd = .5
' H = up arrow
' P = down arrow
' K = left arrow
' M = right arrow
x = x + xd
y = y + yd
LOCATE px, py
PRINT " "
LOCATE px2, py2
PRINT " "
END IF
' If an arrow key was pressed
IF xd <> 0 OR yd <> 0 THEN GOTO Begin
IF x = 2 AND y = 79 THEN GOTO finish ELSE GOTO Begin
WEND
finish:
CLS
LOCATE 12, 39
COLOR 15
PRINT "You won!"
SLEEP 2
CLS
[/code]
-
Ralph
- QBasic God
- Posts:134
- Joined:Sat Nov 06, 2004 2:27 am
- Location:Katy, Texas
Post
by Ralph » Sat Jul 23, 2005 12:00 am
Destructo2000:
What is it that you want, exactly?
1. Code that works. Then, see my post, above.
2. To get your code to work, and only your code. Then, explain why.
3. Is to learn? If so, study mine, and come back with specific questions.
4. Is it because you can accept no other code? Then, explain why it has to be.
5. If not one of the above, please post your explanation of what you are after, and, it would be nice to know, what for.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.
-
Guest
Post
by Guest » Sat Jul 23, 2005 12:14 am
I want my code to work, not somebody else's. I want mine to work because I want my game to work. Not a game where I copied half of it from somebody i don't even know. I want it to be mine. Anybody could copy some other program.
-
Ralph
- QBasic God
- Posts:134
- Joined:Sat Nov 06, 2004 2:27 am
- Location:Katy, Texas
Post
by Ralph » Sat Jul 23, 2005 12:57 am
I understand. But, if we just go ahead and make it work, it really won't be "your code" anymore. Hoever, if you sweat and pore over it, then come up with specific questions, we would all be happy to try to explain how to solve the problem and, most importantly, you will have learned. I hope you agree with me, and follow this path towards knowledge and excellence.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.
-
Dr_Davenstein
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
-
Contact:
Post
by Dr_Davenstein » Sat Jul 23, 2005 1:53 am
Honestly, I think you should start over. Your program isn't very big yet, so now would be the best time to do it.
Do you know how to use a SUB or FUNCTION?
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!