UnderwareDesign
July 29, 2010, 06:43:53 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Meals Of The Dragon by Laskiapina (19,June,2010)
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Consistent Game Timing (Timer Based Movement)  (Read 1394 times)
kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« on: July 24, 2007, 01:33:08 AM »

 Consistent Game Timing  (Timer Based Movement)



Code:

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



Related Examples
     
  Timer Based Movement Tutorial

  Timer Based Movement Example by Silvh

  Timer Based Movement with Vsync Tweening (Aug,2009)

« Last Edit: August 28, 2009, 11:43:50 PM by kevin » Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!