Page 1 of 1

Help....again!

Posted: Thu Jul 21, 2005 4:47 pm
by Guest
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

Posted: Thu Jul 21, 2005 11:06 pm
by buff1
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>

Posted: Fri Jul 22, 2005 3:23 am
by Dr_Davenstein
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    

Posted: Fri Jul 22, 2005 5:24 am
by Ralph
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

Posted: Fri Jul 22, 2005 7:43 am
by Dr_Davenstein
Good job! :P

Posted: Fri Jul 22, 2005 8:34 pm
by Ralph
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.

Posted: Fri Jul 22, 2005 10:34 pm
by Guest
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]

Posted: Sat Jul 23, 2005 12:00 am
by Ralph
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.

Posted: Sat Jul 23, 2005 12:14 am
by Guest
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.

Posted: Sat Jul 23, 2005 12:57 am
by Ralph
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.

Posted: Sat Jul 23, 2005 1:53 am
by Dr_Davenstein
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? :idea: