UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on July 24, 2007, 01:33:08 AM

Title: Consistent Game Timing (Timer Based Movement)
Post by: kevin on July 24, 2007, 01:33:08 AM
 Consistent Game Timing  (Timer Based Movement)



[pbcode]

; *=------------------------------------------------------------------------=*
;                        >> 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<<
; *=------------------------------------------------------------------------=*


Function DrawPlayer()
            ; draw the player object
         
         C=Asc("A")+Player.CurrentFrame
         s$=chr$(c)         
         CenterText Player.x#,player.y#,s$
         Circle Player.x#,player.y#,30,false
EndFunction
[/pbcode]



Related Examples
    
 Timer Based Movement Tutorial (http://www.underwaredesign.com/forums/index.php?topic=1909.0)

 Timer Based Movement Example by Silvh (http://www.underwaredesign.com/forums/index.php?topic=2211.0)

 Timer Based Movement with Vsync Tweening (http://www.underwaredesign.com/forums/index.php?topic=3161.0) (Aug,2009)