Timer Based Movement Example

Started by Silvh, January 12, 2008, 12:38:34 PM

Previous topic - Next topic

Silvh

Alright, after a little bit of messing around. I did something I never managed to do in DBPro, Timer based movement!

This movement system will not depend your speed on how fast your computer goes through the frames(Frames Per Second), but will depend on your computers speed itself! Well, enough talking. Here is the example with a simple box moving around by the arrow keys.

PlayBASIC Code: [Select]
; PROJECT : Player Movement
; AUTHOR : Silvester
; CREATED : 12-1-2008
; EDITED : 12-1-2008
; ---------------------------------------------------------------------

;Set FPS Speed
SetFPS 60

;Player Globals
Global Player_X = 400
Global Player_Y = 300
Global Speed# = 1.5

;Dim Timer stuff
Dim timer1#(0)
Dim timer2#(0)
Dim SpeedofTimer#(0)

;Main Loop
Do
;Clear screen from old data
Cls 0

;Refresh Timer
timer1#(0)=Timer()
SpeedofTimer#(0)=(timer1#(0)-timer2#(0))/1000.0
SpeedofTimer#(0)=SpeedofTimer#(0)*100
timer2#(0)=Timer()

;Make Cube at correct position
Box Player_X, Player_Y, Player_X+40, Player_Y+40, 1

;Control Cube
If UpKey()=1 Then Player_Y = Player_Y - Speed#*SpeedOfTimer#(0)
If DownKey()=1 Then Player_Y = Player_Y + Speed#*SpeedOfTimer#(0)
If LeftKey()= 1 Then Player_X = Player_X - Speed#*SpeedOfTimer#(0)
If RightKey()=1 Then Player_X = Player_X + Speed#*SpeedOfTimer#(0)

;Paste Debug data on screen
Text 0,0,"Screen FPS : "+Str$(FPS())
Text 0,20,"Timer Speed : "+Str$(SpeedOfTimer#(0))
Text 0,40,"Player X : "+Str$(Player_X)
Text 0,60,"Player Y : "+Str$(Player_Y)

;Place drawn objects on screen
Sync
Loop



Hope you enjoy it!



Related Examples

 Timer Based Movement Tutorial

 Consistent Game Timing


"All we have to do, is decide what to do. With the code that is given to us"