Deep Thought 42,
I usually set all of my code into groups, which are nested inside if statements, in which all the groups are nested inside a single loop. To clarify, the main menu(the lines of code which make it run), may be inside its own if end statement. Then, the gameplay part of your code may be inside it own if end statement. In its end, a single variable's value is checked against in each of the if statements, to see whether it equals a specific value. If so, then that group of code is ran, and only that group. This method I learned from my Visual Basic 6.0 course, back some time. I call the method,
Form Focus, with the variable being named,
frmfocus.
The example below also includes the
Local Focus method, which is used inside each group of code. Its variable being named,
lclfocus. To make things run smoother(referencing to the after-states of lclfocus), each group of code is its own function. To explain further, each form(group of code) has specific focuses(what groups/lines of code to run) within that form. I usually only use the lclfocus variable to determine if objects and images need to be loaded, or unloaded, for that form. In other words, it is a setup and close down variable. For example, you may need to load specific creatures for the next level, or images for the menu buttons.
This whole method just makes it simpler to switch to different parts of your program. It seems more versatile, in that you can connect different sections of your program together, and change the connections again, easily. This is simply done by changing the form numbers, which are essentially labeled by frmfocus.
An example of this method lays below. It is fully executable and needs no media. However, there is a slight bug when returning to the main menu.
Full Example:
sync on
sync rate 60
REM << frmfocus will initially be set to 0
REM <<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
REM <<<<<<<<<<<<<<<<<< main loop >>>>>>>>>>>>>>>>>>>>>>>>
REM <<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
repeat
REM << main menu
if frmfocus = 0 then frmfocus = mainmenu(frmfocus)
REM << gameplay
if frmfocus = 1 then frmfocus = gameplay(frmfocus)
REM << gameover
if frmfocus = 2 then frmfocus = gameoverscreen(frmfocus)
REM << end program if frmfocus = 3
until frmfocus = 3
end
REM <<<<<<<<<<<<<<<<< end main loop >>>>>>>>>>>>>>>>>>>>>>
REM <<<<<<<<<<<>>>>>>>>>>>>
REM <<<<<< functions >>>>>>
REM <<<<<<<<<<<>>>>>>>>>>>>
function mainmenu(frmfocus)
REM << setup main menu if needed
if lclfocus = 0
lclfocus = 1
REM << make options image sprites
for t = 1 to 2
set text size 18
if t = 1 then string$ = "Play"
if t = 2 then string$ = "Exit"
ink rgb(255,0,0),0
box 0,0,text width(string$) + 6,text height(string$) + 4
ink rgb(255,255,255),0
text 3,2,string$
get image t,0,0,text width(string$) + 6,text height(string$) + 4
REM << make and place sprites onto screen
sprite t,0,0,t
sprite t,(screen width() - sprite width(t)) / 2,((screen height() - sprite height(t)) / 2) + yposadj,t
inc yposadj,50
cls
next t
REM << set text size for logo
set text size 72
REM << get logo screen coordinate positioning values
logox = screen width() / 2
logoy = 100
endif
REM <<<<<<< main loop of function(local group of code above will be bypassed after first pass) >>>>>
center text logox,logoy,"Logo"
REM << check for mouse/button collision
for t = 1 to 2
if mousex() => sprite x(t) and mousex() =< sprite x(t) + sprite width(t) and mousey() => sprite y(t) and mousey() =< sprite y(t) + sprite height(t)
if mouseclick() = 1
REM << close down the form, by (re)setting variables and deleting any media not needed on other forms
for s = 1 to 2
delete sprite s
next s
frmfocus = t
lclfocus = 0
yposadj = 0
cls
endif
endif
next t
REM << screen syncronization is controlled from within the function
sync
REM << return the value of frmfocus, which is enabled to be changed within menu
endfunction frmfocus
REM <<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
REM <<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function gameplay(frmfocus)
REM << setup gameplay if needed(create and place objects and set specific variables;initiation)
if lclfocus = 0
lclfocus = 1
backdrop on
inc objectnum
REM << make ground
make object plain objectnum,500,500
xrotate object objectnum,90
color object objectnum,rgb(0,200,0)
inc objectnum
REM << make boxes and place them in random locations around ground area
for t = objectnum to objectnum + 20
make object cube t,rnd(10) + 1
position object t,rnd(450) - rnd(450),object size y(t) / 2,rnd(450) - rnd(450)
next t
objectnum = t
REM << preposition camera
position camera 0,5,0
ink rgb(255,255,255),0
set text size 18
endif
REM <<<<<<<<<< main loop of function >>>>>>>>>>>>>>>>>>>
REM << player controls
playeryrot# = wrapvalue(playeryrot# + (mousemovex() * 0.50))
yrotate camera playeryrot#
if upkey() = 1 then move camera 2
if downkey() = 1 then move camera -2
REM << print instructions to screen
center text screen width() / 2,screen height() - 50,"Press spacebar to return to main menu"
REM << syncronization controlled by function
sync
REM << close down the form(use spacekey to return to main menu)
if spacekey() = 1
for t = 1 to objectnum - 1
delete object t
next t
cls
objectnum = 0
lclfocus = 0
playeryrot = 0
frmfocus = 0
REM << make sure to go back to fully 2d for the menu
backdrop off
cls
endif
endfunction frmfocus
REM <<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
REM <<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function gameoverscreen(frmfocus)
REM << setup this form(initiate timer and setup type set)
if lclfocus = 0
lclfocus = 1
time = timer()
red = 255
set text size 72
x = screen width() / 2
y = (screen height() - text height("Game Over!")) / 2
endif
REM <<<<< main function loop >>>>>>>>>>>>
REM << print game end to screen
ink rgb(red,0,0),0
center text x,y,"Game Over!"
REM << syncronization controllod from within function
sync
REM << change ink color over time to make words fade to black
if timer() - time > 50
time = timer()
dec red,5
endif
REM << end program
if red = 0 then frmfocus = 3
endfunction frmfocus
+NanoBrain+