question from a student

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
lostoros
Newbie
Posts: 5
Joined: Fri Sep 30, 2005 10:46 am

question from a student

Post 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...
Guest

Post 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
m2j

Post 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
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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...

...
Image
User avatar
Xerol
Veteran
Posts: 81
Joined: Tue Jan 04, 2005 6:27 pm
Location: Timonium, MD
Contact:

Post by Xerol »

GOTOs aren't necessarily 100% bad - there's situations where the code comes out much cleaner using gotos.
If you need music composed in MP3, WAV, or MIDI format, please contact me via email.

Xerol's Music - Updated Regularly!
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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...
Image
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post 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...
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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...
Image
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post 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.
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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.
Image
Guest

Post 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
Post Reply