Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: DrewPee on June 13, 2009

Title: Goto routines . . .
Post by: DrewPee on June 13, 2009
Hello
I am in the process of writing a very basic game to test some drawing routines I am messing about with . . . I am stuck with re-starting a game once you die.
Do I need to use something like a goto command or is there something else I can use in Freebasic?

When I try to use goto I keep on getting this error

94: Branching to other functions or to module-level, to label: strt

Thanks in anticipation

DrewPee

Title: Re: Goto routines . . .
Post by: Shockwave on June 13, 2009
There's no need to use commands like Goto nowadays Drewpee.

You could structure your game code something like this;

Code: [Select]
QUIT  = 0
GAME = 0

MAIN LOOP (LOOPS UNTIL QUIT=1)
    IF QUIT = 0 THEN HANDLE TITLE SCREEN SUB
    IF GAME=1 THEN HANDLE GAME SUB
END OF MAIN LOOP

END PROGRAM


HANDLE GAME SUB
START OF LOOP UNTIL GAME=0
   CALL GAME SUBS
   UPDATE SCREEN
   IF DEAD THEN GAME=0
END OF LOOP UNTIL GAME=0
END OF HANDLE GAME SUB



HANDLE TITLE SCREEN SUB
START OF LOOP UNTIL GAME=1
   CALL TITLE SCREEN SUBS
   UPDATE SCREEN
   IF START BUTTON PRESSED THEN GAME=1
   IF QUIT BUTTON PRESSED THEN QUIT=1
END OF LOOP UNTIL GAME=1 OR QUIT=1
END OF HANDLE TITLE SCREEN SUB

Title: Re: Goto routines . . .
Post by: DrewPee on June 13, 2009
Shockwave, thanks (as always for your help!),

code removed for now - due to odd problems with some of it - I will re-post at a later date ;)

DrewPee
Title: Re: Goto routines . . .
Post by: DrewPee on June 13, 2009
Meant to say - this is still early code so there are bits in there that are not needed etc. ;)
Title: Re: Goto routines . . .
Post by: Shockwave on June 13, 2009
You need to understand what happens to the stack.

You branch to a sub and the memory address is stored
when the end sub is encountered, you go back to that last address.

Using goto the way you have in a sub potentially could cause a stack overflow which is why it's terrible programming practice and why freebasic wont let you do it.

The technique I posted is very simple to impliment, why not have a try yourself and post back if you run into problems.

Seriously, forget about goto, there is no need to use it... ever.
Title: Re: Goto routines . . .
Post by: DrewPee on June 13, 2009
Thanks Shockwave I will go and try it again.
Title: Re: Goto routines . . .
Post by: Shockwave on June 13, 2009
Good luck mate
Title: Re: Goto routines . . .
Post by: DrewPee on June 13, 2009
I have figured it out a little more and this is where I am at the moment - still early days but playable now! ;)

@Shockwave - thanks again for your help!

btw - the highest score so far is 2392, not by me I might add, by one of our members on RCM!

Drew
Title: Re: Goto routines . . .
Post by: Shockwave on June 13, 2009
It looks pretty cool indeed :)

The only flaw I can see is the method you used to detect your keypresses causes a delay in movement...

Use this method instead..

IF GETASYNCKEYSTATE( VK_UP ) Then do stuff.

:)

Title: Re: Goto routines . . .
Post by: Hotshot on June 14, 2009
very good.....Original game

GOTO is horrible but have you thought about using Select case?
Title: Re: Goto routines . . .
Post by: Shockwave on June 14, 2009
Select Case will not help here Hotshot, you'd use Selest Case the same as switch or if and then.

He just needs to have some controling subs and a containing loop.
Title: Re: Goto routines . . .
Post by: DrewPee on June 14, 2009
I am still 'cheating' a little - but it does work much better now . . . I have added some music, difficulty levels etc.

.exe attached
Title: Re: Goto routines . . .
Post by: Shockwave on June 14, 2009
Still has the slightly annoying movement delay when pressing a key (see above for the solution) but apart from that it's not bad at all.

It's great to see you doing some programming again Drewpee, you should do more but I guess that it's hard to get the time these days.
Title: Re: Goto routines . . .
Post by: DrewPee on June 14, 2009
I am using the commands you mentioned, I think I just have them in the wrong place - need to move the code around a little me thinks! ;)
Title: Re: Goto routines . . .
Post by: Shockwave on June 14, 2009
You should use GETASYNCKEYSTATE
Like this:

IF GETASYNCKEYSTATE( VK_UP ) THEN

(without the =-32768 bit), that why the controls are slightly broken atm.
Title: Re: Goto routines . . .
Post by: Clyde on June 14, 2009
If you are going the Select Case route, if you use Select Case As Const Choice

Choice being a variable of your choice ( hehe, get it ) it's apparently better / faster.