Here's an example of a simple game using the select and case commands to control the state of a game
// demo showing how to use the Select and Case commands to control the state of a game
// use wasd or the oncsreen joystick to move the green box
// and collect the red boxes in the fastest time possible
gosub _basic_setup
do
select game_state
case INITIATE
game_state = _f_initiate(game_state)
endcase
case START
game_state = _f_start(game_state)
endcase
case PLAY
game_state = _f_play(game_state)
endcase
case GAME_OVER
game_state = _f_game_over(game_state)
endcase
endselect
Sync()
loop
_basic_setup:
// screen
SetWindowTitle( "simple_game" )
SetWindowSize( 800, 800, 0 )
SetVirtualResolution( 800, 800 )
// game state variable and values
game_state as integer
#constant INITIATE 0
#constant START 1
#constant PLAY 2
#constant GAME_OVER 3
// set the first game state
game_state = INITIATE
// score and times
global start_time
global current_time
global fastest_time
fastest_time = 999999
// set up variables for the player and the targets
type _box_type
spr as integer
xp# as float
yp# as float
life as integer
endtype
global player as _box_type
dim target[10] as _box_type
player.spr = createSprite(0)
SetSpriteSize(player.spr,20,20)
SetSpriteColor(player.spr,0,255,0,255)
for i = 1 to 10
target[i].spr = createSprite(0)
SetSpriteSize(target[i].spr,20,20)
SetSpriteColor(target[i].spr,255,0,0,255)
next i
// hud text
createText(1,"")
SetTextSize(1,30)
SetTextPosition(1,400,350)
SetTextAlignment(1,1)
createText(2,"")
SetTextSize(2,25)
SetTextPosition(2,400,400)
SetTextAlignment(2,1)
createText(3,"FASTEST TIME = " + str(fastest_time))
SetTextSize(3,20)
SetTextPosition(3,0,0)
createText(4,"CURRENT TIME ")
SetTextSize(4,20)
SetTextPosition(4,0,20)
return
function _f_initiate(game_state)
// set the intial message
SetTextString(1,"THE TIMER WILL START WHEN YOU MOVE")
SetTextString(2,"use wasd keys to move")
SetTextString(4,"CURRENT TIME = 0")
// position player at the centre of the screen
player.xp# = 400
player.yp# = 400
SetSpritePositionByOffset(player.spr,player.xp#,player.yp#)
// randomly position the targets
for i = 1 to 10
target[i].life = 1
target[i].xp# = random(10,790)
target[i].yp# = random(10,790)
SetSpritePositionByOffset(target[i].spr,target[i].xp#,target[i].yp#)
SetSpriteVisible(target[i].spr,1)
next i
// go to the next game state
game_state = START
endfunction game_state
function _f_start(game_state)
// set hud visible
SetTextVisible(1,1)
SetTextVisible(2,1)
// if the player moves the joystick (or press either w,a,s or d key then start the game
if GetJoystickX() <> 0 or GetJoystickY() <> 0
// set hud text invisible
SetTextVisible(1,0)
SetTextVisible(2,0)
// set start time
start_time = GetMilliseconds()
// go to the next game state
game_state = PLAY
endif
endfunction game_state
function _f_play(game_state)
// move player
player.xp# = player.xp# + GetJoystickX()*5
player.yp# = player.yp# + GetJoystickY()*5
// keep player on the screen
if player.xp# < 10
player.xp# = 10
endif
if player.xp# > 790
player.xp# = 790
endif
if player.yp# < 10
player.yp# = 10
endif
if player.yp# > 790
player.yp# = 790
endif
// position player sprite
SetSpritePositionByOffset(player.spr,player.xp#,player.yp#)
// local variable used to determine if all the targets have been hit (reset to zero
total_target_life = 0
// detect hit against target
for i = 1 to 10
if target[i].life = 1
if GetSpriteCollision(player.spr,target[i].spr)
target[i].life = 0
SetSpriteVisible(target[i].spr,0)
endif
endif
// add together all the life values of the targets
total_target_life = total_target_life + target[i].life
next i
// get the current time
current_time = GetMilliseconds() - start_time
SetTextString(4,"CURRENT TIME = " + str(current_time))
// the game ends when all the targets have been hit
if total_target_life = 0
game_state = GAME_OVER
endif
endfunction game_state
function _f_game_over(game_state)
SetTextVisible(1,1)
SetTextString(2,"press space to play again")
SetTextVisible(2,1)
if current_time < fastest_time
SetTextString(1,"!!! NEW FASTEST TIME !!!")
else
SetTextString(1,"GAME OVER")
endif
// press space to play again
if GetButtonPressed(1) = 1
// set new fastest time
if current_time < fastest_time
fastest_time = current_time
SetTextString(3,"FASTEST TIME =" + str(fastest_time))
endif
// change game state
game_state = INITIATE
endif
endfunction game_state
Hope this helps.