Game Movement with Delta Timing


Attention! 👉Starting November 2024, BlitzCoder.org will now be BlitzBasic.org.

 

Tweet blitz3d blitzplus code-archives miscellaneous tutorials
BlitzCoder

A quick example with 2D game movement using delta timing. This should apply on 3D as well.

Game Movement with Delta Timing

xspd#=.5 ; speed
x# = 0

Graphics 640,480,0,2
SetBuffer BackBuffer()

count = 100

oldmillisecs#=MilliSecs()
While Not KeyHit(1)
    ;get time elapsed since last frame

    thistime# = MilliSecs()
    deltatime#=thistime#-oldmillisecs#
    oldmillisecs#=thistime#
    ;graphics with timing
    Cls

    If KeyHit(57) enable = Not enable 
    If KeyHit(78) count = count + 200
    If KeyHit(74) count = count - 200

    If count <= 100 Then count = 100

    If enable 
        x=x + deltatime*xspd   ; this is movement by time elapsed.
    Else
        x = x + xspd
    End If

    If x>640 Then x=0
    Color 255,255,255
    Rect x,y,16,16,1

    ;uncomment for testing. movement will reach a to b in the same time despite how much is on screen
    For i=0 To count
        Color Rand(0,255),Rand(0,255),Rand(0,255)
        Rect Rand(0,600),Rand(90,440),40,40
    Next 

    Color 255,255,255
    Text 0,30, "<space> Toggle Delta Timing Movement: " + enable + "  [+/-] Add/Remove 200 Rectangles"
    Text 0,50, "Movement Increment Speed: " + xspd
    Text 0,70, "Rectangle Count: " + count

    Flip 0

Wend
End

Reply To Topic (minimum 10 characters)

Please log in to reply