Domanda

I am trying to make a maze in Qbasic but when the pointer touches the maze lines then the program is not ending. I want that when the circle (which is the pointer ) touches the ends of the maze then the program should go to an end.The Program is this:-

cls
screen 12
DIM p AS STRING
DIM A1 AS STRING
15 print"What do you want to do?"
print"A:Draw AN IMAGE"," B:PLAY A MAZE GAME";
PRINT
PRINT"TYPE 'A' OR 'B'IN CAPITAL FORM"
GOTO 102
99 print "rules to play the maze game:"
print
print "1 use 'W' to move the ball foward"
print "2 use 'S' to move the ball backward"
print "3 use 'A' to move the ball leftward"
print "4 use 'D' to move the ball rightward"
INPUT A1
CLS
goto 10

102  INPUT P
if p="A"then
cls
goto 20
elseif p="B" then
cls
goto 99
elseif  p<>"A"  AND p<>"B" then
print "Choose between A and B"
GOTO 70
end if


10 pset(120,120)
draw "r100"
pset (120,140)
draw"r80"
pset (200,140)
draw "d100"
pset (220,120)
draw"d140"
pset (220,260)
draw "l90"
pset (200,240)
draw "l50"
pset (130,260)
draw"u50l120u90r60u40l50u60r300d90l35d260l60d30l80
h20l20h20u30r40u5l70d60f40r250u90h40u45r40u40r50u130h40r225d65l50d60l15
d130l40d50l20d15r45d40r20u45r10u10r10u90r100"

pset(150,240)
draw"u50l120u50r60u80l50u20r260d50l35d260l60d30l40h20l20h10r
40u50l120d98f50r290u115h40u20r40u40r50u160h10r140d20l50d60l15
d130h40d90l20d60r45d45r70u45r10u10r10u90r75"

20 dim k as string
x = 110
y = 105
do
k = ucase$(inkey$)
if k="W"then

y = y - 2

elseif k= "S" then

y = y + 8

elseif k="A"then

x = x - 8

elseif k="D" then

x = x + 5

end if
circle (x,y),7,10
loop until k ="Q"
GOTO   45
70 CLS
GOTO 15
if x=120 and y=120 then goto 45
40  cls

45 END

Pls Help

Thanks in Advance....

È stato utile?

Soluzione

Ok, let's take a peak at your game loop presented below and reformated a bit for readability:

do
    k = ucase$(inkey$)

    if k="W"then
        y = y - 2
    elseif k= "S" then
        y = y + 8
    elseif k="A"then
        x = x - 8
    elseif k="D" then
        x = x + 5
    end if

    circle (x,y),7,10
loop until k ="Q"

Your win case (if x=120 and y=120 then goto 45) doesn't actually occur within the loop but outside it.

With do loops, only the code between the do and loop statement will execute unless the "until" statement returns true. In order words:

do
    'This code will execute 
loop until k = "Q"
    'This code will only execute after k=Q

Put the win case in the do loop and it should work.

If I recall correctly, QBasic allows whitespace in the beginning of a line. I recommend using whitespace to organize your code visually so you can see what's going on. Look at how I formatted your main loop. Everything that the do loop controls is tabbed to the right of the do and loop statement. This way you can easily see what the do loop is doing. Everything in the if statement gets the same treatment for similar reasons.

If you get in the habit of indenting your code, you can start to see the code's logic laid out cleanly.

Edit: It seems you're new to programming. If you enjoy it, I recommend learning Python through codecademy rather than QBasic. QBasic encourages some very bad habits, like goto statements.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top