News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Consistent Game Timing (Timer Based Movement)

Started by kevin, July 24, 2007, 01:33:08 AM

Previous topic - Next topic

kevin

 Consistent Game Timing  (Timer Based Movement)



PlayBASIC Code: [Select]
; *=------------------------------------------------------------------------=*
; >> Set Games Ideal Frame Rate <<
; *=------------------------------------------------------------------------=*

Constant RequiredFrameRate=60
Constant MillisecondsPerFrame=1000/RequiredFrameRate




; *=------------------------------------------------------------------------=*
; >> Set up a Player <<
; *=------------------------------------------------------------------------=*
Type tObject
X#
Y#
Speed#
CurrentFrame ; anim frame
MaxFrame ; end of animation
EndType


Dim Player as tObject

; Init the Player properties
Player.x# =GetScreenWidth()/2
Player.y# =GetScreenHeight()/2
Player.speed# =1 ; move 1 pixel frame
Player.CurrentFrame=0
Player.MaxFrame=asc("Z")-asc("A")



; Simulate SLOWer computer by push down the rate bellow the Ideal rate
; SetFps 20


; *=------------------------------------------------------------------------=*
; >> Game Main Loop <<
; *=------------------------------------------------------------------------=*


NextFrame=Timer()

repeat

; Check if it's time to update the game logic & Redraw
if Timer()>NextFrame

; Calc the amount of time past since the last update
TimePast=timer()-NextFrame

; Calc the number of frames past since the last update/redraw
FramesPast =TimePast/MillisecondsPerFrame

; Update the logic/spawning code
for UpdateFrameslp=1 to Framespast
gosub Update_Logic
next

; Calc the time of next frame
NextFrame=NextFrame+(FramesPast*MillisecondsPerFrame)

; Draw the Display
gosub Draw_Scene

text 0,0,fps()

; Refresh the display
Sync
endif

until SpaceKey()=true


end





; *=------------------------------------------------------------------------=*
Update_Logic:
; *=------------------------------------------------------------------------=*

MovePlayer()
; Move Aliens
; move bullets etc

return





; *=------------------------------------------------------------------------=*
Draw_Scene:
; *=------------------------------------------------------------------------=*
Cls 0

DrawPlayer()
; Draw aliens
; draw bullets
; etc etc
return






; *=------------------------------------------------------------------------=*
; >> Control/Animation the Player <<
; *=------------------------------------------------------------------------=*

Function MovePlayer()

Speed#=PLayer.Speed#

if leftkey() then Player.X#=Player.X#-speed#
if rightkey() then Player.X#=Player.X#+speed#
if upkey() then Player.Y#=Player.Y#-speed#
if downkey() then Player.Y#=Player.Y#+speed#


if Player.X#<0 then Player.X#=0
if Player.X#>GetScreenWidth() then Player.X#=GetScreenWidth()
if Player.Y#<0 then Player.Y#=0
if Player.Y#>GetScreenHeight() then Player.Y#=GetScreenHeight()


; bump the animation counter
frame=Player.CurrentFrame+1
if Frame>Player.MaxFrame then frame=0
Player.CurrentFrame=frame

; You can simplify this by using the MOD btw
; Player.CurrentFrame=mod(Player.CurrentFrame+1,Player.MaxFrame)

EndFunction




; *=------------------------------------------------------------------------=*
; >> Draw the Current State (position/animation frame) of the Player<<
; *=------------------------------------------------------------------------=*

Login required to view complete source code




Related Examples
    
 Timer Based Movement Tutorial

 Timer Based Movement Example by Silvh

 Timer Based Movement with Vsync Tweening (Aug,2009)