Page 1 of 1

question from a student

Posted: Fri Sep 30, 2005 10:55 am
by lostoros
Hi-

I am a high school student working on a quick basic game for school. THe game is a rip-off of pacman where the character eats punctuation and is chased by a giant percentage sign...but ive run into a problem. The structure of the program is as follows:

intructions...intro, etc.

maze designs (2 at this point)

THen a DO...LOOP which contains the actual commands to move the character, the printing of the maze, score calculations, AI for the mosnters, etc.

Here is my problem. In the loop is a command which recognizes whether the player has won or lost. But after you win, I need to be able to exit the loop and go back to the maze design section so the computer will recalculate the level variable and print a new maze. I am unable at this point to get it to exit the loop and go back to that point. Please help...thanks in advance...

Posted: Fri Sep 30, 2005 11:54 am
by Guest
There are two ways I would suggest to a beginner to go about this:

The first would be to use the GOTO statement (Seeing as your obviously thinking alone those lines).

This command works as follows:

After the Do...Loop write the command

GOTO top

And back above in the program code where you want the program to restart at type

top:

This is called a lable. Obviously if you call the label something other then top you'd goto the new label name.


This, however, is deemed HORRENDUS PROGRAMMING PRACTICE, and I shouldn't really be telling you it even exists.

The alternative would be to encase the entire program in another DO...LOOP command, so that when the game loop exits the program goes back to the start. Seeing as you are already familiar with the DO...LOOP command, and not goto, I'd suggest taking this option.

If you'd like me to explain in more detail, just ask,

matt - feeling nice and good because he sent his Rising Stars of Manga entery in on time

Posted: Fri Sep 30, 2005 11:58 am
by m2j
WOOPS, ignore everything I just posted,

I misread what you were asking

The command you are looking for is called


EXIT DO


when you call this the program will leap out of the loop and on to the next statements


Sorry to have treated you like an idiot,

Matt


PS: EXIT also works for loops like FOR, WHILE and for SUBroutines and FUNCTIONs

Posted: Fri Sep 30, 2005 2:18 pm
by {Nathan}
m2j wrote:WOOPS, ignore everything I just posted,

I misread what you were asking

The command you are looking for is called


EXIT DO


when you call this the program will leap out of the loop and on to the next statements


Sorry to have treated you like an idiot,

Matt


PS: EXIT also works for loops like FOR, WHILE and for SUBroutines and FUNCTIONs
Yes, and if you need to skip from somewhere in your program to the end of the loop (just before the DO) then you are forced to use goto.

If I am wronge about that, then don't blame me - that is what my 620 QB book said. Well, not QB but QBASIC... but it is still basically the same thing...

...

Posted: Fri Sep 30, 2005 7:15 pm
by Xerol
GOTOs aren't necessarily 100% bad - there's situations where the code comes out much cleaner using gotos.

Posted: Fri Sep 30, 2005 9:24 pm
by {Nathan}
Xerol wrote:GOTOs aren't necessarily 100% bad - there's situations where the code comes out much cleaner using gotos.
Wow... someone finally took a stand!!!
I AGREE BROTHER!!!

Really, sometimes GOTOs can kick ass...

Posted: Fri Sep 30, 2005 10:34 pm
by Nodtveidt
Nathan1993 wrote:
Xerol wrote:GOTOs aren't necessarily 100% bad - there's situations where the code comes out much cleaner using gotos.
Wow... someone finally took a stand!!!
I AGREE BROTHER!!!

Really, sometimes GOTOs can kick ass...
I've been saying this for years...

Posted: Sat Oct 01, 2005 10:35 am
by {Nathan}
Nekrophidius wrote:
Nathan1993 wrote:
Xerol wrote:GOTOs aren't necessarily 100% bad - there's situations where the code comes out much cleaner using gotos.
Wow... someone finally took a stand!!!
I AGREE BROTHER!!!

Really, sometimes GOTOs can kick ass...
I've been saying this for years...
I have a bad memory...

Posted: Sat Oct 01, 2005 4:59 pm
by Zamaster
Yeah, GOTO's have their purposes but then again... well... check out "Haunted Halloween" from www.qbasic.com under "action games" in the "download" section. This has to be the WORST use of GOTO's I have ever seen. It must've taken this guy a year to finish it.

Posted: Sun Oct 02, 2005 11:53 am
by {Nathan}
Zamaster wrote:Yeah, GOTO's have their purposes but then again... well... check out "Haunted Halloween" from www.qbasic.com under "action games" in the "download" section. This has to be the WORST use of GOTO's I have ever seen. It must've taken this guy a year to finish it.
Yes, for the beginner GOTOs are horrible, but for any seasoned programmer, they know how and when to use GOTOs.

Posted: Mon Oct 03, 2005 3:27 am
by Guest
In every GOTO situation I've come across, it can also be done another way with loops, by changing the structure slightly. The problem with GOTO's is they fascilitate (not make, but allow), badly structured code... If I am using labels I tend more towards using GOSUBs... But I have written an entire game (currently in a year long beta-tesing stage) that is built around a GOTO structure... so yeah, I'm firmly on the fence for this one.

matt