Hello Everyone,
I would like to offer something to all of you, because I see so many young developers not really having a good grasp on the basics of the game loop. This is not the only way, nor probably the best way, but it is my way. I find that it is clean, fast, and effective code management.
'//' comments will error, these are just some notes to explain what can go where, you will have to remove them.
This code in and of itself will not run, or if it does it will loop indefinately. You'll have to learn how to take user input and apply it towards changing those flag conditions. Ultimately this is to show and example of how the loop works, but if you really need help, perhaps I can put together a fully working version of the loop.
The gosubs are not neccessary, but I find that they are an excellent way to move code around, so you can clearly see your main loop, and focus on each individual section very easily. A gosub has no better or worse value used in this fashion. Using them in more essentric ways can be very nasty.
And here it is. Please feel free to comment.
REM Version Header
// Put some information on what this code is for.
REMSTART
GameLoop Template by Pheonixx of Ausukusa.
CREATED: Thursday, July 14th, 2005
Free for use AS IS and WITHOUT WARRENTY to the DBP Community.
REMEND
REM CONSTRUCTION
// Establish Constants, Globals, Dims, and Typedefs.
GLOBAL GAMESTATE AS INTEGER
GLOBAL GAMECONDITION AS INTEGER
GLOBAL SCRIPTFLAG AS INTEGER
GAMESTATE = 0 `Introduction
GAMECONDITION = 0 `Default
SCRIPTFLAG = 0 `Initialize
REM MAIN PROGRAM LOOP
DO
USER$ = UPPER$(INKEY$())
SELECT GAMESTATE
CASE 0
REM INTRODUCTION
GOSUB INTRODUCTION
ENDCASE
CASE 1
REM TITLE SCREEN
GOSUB TITLE
ENDCASE
CASE 2
REM SETUP GAME
GOSUB GAMESETUP
ENDCASE
CASE 3
REM PLAY GAME
GOSUB GAMEPLAY
FOR AILOOP = 1 TO NUMBER OF AI GROUPS
GOSUB AIPLAY
NEXT AILOOP
ENDCASE
CASE 4
REM GAME RESULTS
GOSUB GAMERESULT
ENDCASE
CASE 5
REM SHUTDOWN GAME
EXIT
ENDCASE
CASE 6
REM INSTRUCTION
GOSUB INSTRUCTIONS
ENDCASE
CASE 7
REM CONFIGURATION
GOSUB CONFIGURATION
ENDCASE
CASE DEFAULT
CENTER TEXT 400,300,"ERROR ~ GAMESTATE VARIABLE IS NOT PROPERLY SET!!!"
WAIT KEY
ENDCASE
ENDSELECT
LOOP
REM DESTRUCTION
// release objects, memory, and files.
END
INTRODUCTION:
REM LOAD MEDIA
// display a welcome screen or something.
IF IMAGE EXIST(1) = 0 THEN LOAD IMAGE "TITLE.JPG", 1
IF MUSIC EXIST(1) = 0 THEN LOAD MUSIC "TITLE.MP3", 1
PASTE IMAGE 1,0,0
IF MUSIC PLAYING(1) = 0 THEN PLAY MUSIC 1
WAIT 2500 `Dramatic Pause, you can make this longer, slower, or change it to wait for user input.
GAMESTATE = 1 `progress to Title
RETURN
TITLE:
REM DISPLAY TITLE
// this is the top level of the game.
SELECT USER$
CASE 1
REM START A NEW GAME
SCRIPTFLAG = 0 `Initialize
GAMESTATE = 2 `progress to Game Setup
ENDCASE
CASE 2
REM LOAD A GAME
SCRIPTFLAG = 1 `Load from a saved game index
GAMESTATE = 2 `progress to Game Setup
ENDCASE
CASE 3
REM QUE INSTRUCTIONS
GAMESTATE = 6 `progress to Instructions
ENDCASE
CASE 4
REM QUE CONFIGURATIONS
GAMESTATE = 7 `progress to Configuration
ENDCASE
CASE 5
REM SHUT DOWN GAME
GAMESTATE = 5 `progress to QUIT GAME
ENDCASE
CASE DEFAULT
REM DO NOTHING
ENDCASE
RETURN
GAMESETUP:
REM SETUP
// load up objects, maps, and init variables based on scripts.
SELECT SCRIPTFLAG
CASE 0
REM Setup for first gameplay and then progress to GamePlay
GAMESTATE = 3
ENDCASE
CASE 1
REM Setup for return gameplay and then progress to GamePlay
GAMESTATE = 3
ENDCASE
CASE 2
REM Progress to X level and then progress to GamePlay
GAMESTATE = 3
ENDCASE
CASE DEFAULT
CENTER TEXT 400,300,"SCRIPTING ERROR ON GAME SETUP"
ENDCASE
RETURN
GAMEPLAY:
REM PlayersLoop
// All gameplay outside of the AI
REM PlayerQuit
...
GAMESTATE = 4 `Progress to Results.
GAMECONDITION = 0 `Player has Quit
...
REM PlayerDied
...
GAMESTATE = 4 `Progress to Results.
GAMECONDITION = 1 `You have died.
...
REM PlayerWins
...
GAMESTATE = 4 `Progress to Results.
GAMECONDITION = 2 `You have won.
...
RETURN
AIPLAY:
REM Do AI Stuff
// Pretty self explanitory
RETURN
GAMERESULT:
REM Show Player Results
// All Major Events
SELECT GAMECONDITION
CASE 0
REM You quit...
// show'm how they did, and go back to title
GAMESTATE = 1 `Progress to Title
SCRIPTFLAG = 0 `Initialize on next run
ENDCASE
CASE 1
REM You died...
// show'm how they did.
IF PLAYERLIFE > -1
REM Give'm another Chance
// what happens here is up to you
...
GAMESTATE = 3 `Progress to Gameplay
ELSE
REM Send them home crying
// again, what happens here is up to you
...
GAMESTATE = 1 `Progress to Title
SCRIPTFLAG = 0 `Initialize on next run
ENDIF
ENDCASE
CASE 2
REM You won...
// show'm how they did.
GAMESTATE = 2 `Progress to Game Setup
SCRIPTFLAG = 2 `Setup next level, win game, whatever.
ENDCASE
CASE DEFAULT
CENTER TEXT 400,300,"GAMESTATE ERROR ON RESULTS"
ENDCASE
RETURN
INSTRUCTIONS:
REM Helpfile
// show your player what they are supposed to try to do and the rules would be nice.
RETURN
CONFIGURATION:
REM Configfile
// players like to be able to have their own configuration when playing a game.
RETURN
REM ***** FUNCTIONS *****
// add all of your functions here, or above the subs, whatever you like best.
REM ***** DATAFIELD *****
// always have your data at the bottom of your code.
http://ausukusa.breakset.com