Making a pause code isn't quite as straight forward as you'd think. The problem is, that if your program uses timer based movement/physics, and you press pause, the timer still keeps running - because timers are based on the computers internal clock which you can't stop.
So, if you want to pause your game, and don't want all your timer based objects to have moved on when you return, you will need to include a timing routine into your pause code which stores the amount of time you paused for, and this can then be subtracted from the main timer in the main program loop. This pause time then needs to be added to a total pause time to enable multiple pauses throughout the game/program.
This snippet is the code for incorporating a timer and a pause subroutine into your program:
`initialise the timer - this should be the very last thing
`before the main loop
starttime#=timer()
`main program loop
do
`start the timer: t# is current time in seconds
t#=(timer()-starttime#-pausetime#-totalpausetime#)/1000
dt#=t#-ot# `dt# (delta time) is the change in time since last loop
`which you may need to use in your movement commands
ot#=t# `ot# (old time) was time on previous loop
if spacekey()=1
gosub pause
endif
loop
`pause subroutine
pause:
`pausetime# is the time for this pause
`totalpausetime# is the total time for all pauses
totalpausetime#=totalpausetime#+pausetime#
startpausetime#=timer()
do
pausetime#=timer()-startpausetime#
if returnkey()=1
return
endif
loop