; CreateTimer Example ; ------------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; CreateTimer(hertz) makes a timer that ticks at a fixed rate. Waiting ; on it with WaitTimer locks the main loop to that rate, so the game ; runs at the same speed on fast and slow computers. hertz=60 timer=CreateTimer(hertz) frames=0 fps=0 fps_time=MilliSecs() While Not KeyDown(1) Cls ; 1/2/3 rebuild the timer at a new rate (freeing the old one) new_hertz=0 If KeyHit(2) Then new_hertz=15 If KeyHit(3) Then new_hertz=60 If KeyHit(4) Then new_hertz=120 If new_hertz>0 Then FreeTimer timer hertz=new_hertz timer=CreateTimer(hertz) EndIf ; One step per loop: the timer's rate IS the square's speed x=(x+4) Mod 640 Rect x,220,40,40,True ; Count real frames per second to show the timer is in charge frames=frames+1 If MilliSecs()-fps_time>=1000 Then fps=frames frames=0 fps_time=MilliSecs() EndIf Text 0,0,"1: 15 Hz 2: 60 Hz 3: 120 Hz Esc: exit" Text 0,20,"timer=CreateTimer("+hertz+") measured "+fps+" fps" ; Flip False: don't also wait for the monitor - the timer sets the pace Flip False ; Wait here until the timer's next tick WaitTimer(timer) Wend End