Timer Question

Blitz3D Forums/Blitz3D Beginners Area/Timer Question

Happy Llama(Posted 2012) [#1]
I have 4 images. A picture of a 3, a 2, a 1, and "Go!". How would I make it so before the game starts the numbers count down then after go, the game starts?


Guy Fawkes(Posted 2012) [#2]
i wondered the same thing, myself. we need an easy countdown timer code.


Rob the Great(Posted 2012) [#3]
Try something like this:
Graphics 640,480

SetBuffer BackBuffer()

;Load your images
Global one = LoadImage("one.jpg")
Global two = LoadImage("two.jpg")
Global three = LoadImage("three.jpg")
Global go = LoadImage("Go.jpg")

Local pregame = 0 ;make a condition for an intro mini-loop
Local currentimage = 3 ;make a variable to know which image to draw
Local timer = Millisecs() ;Store the current time in milliseconds

While pregame = 0
   If KeyDown(1) Then End ;this avoids getting locked in this loop
   Cls
   If Millisecs() >= timer + 1000 ;If 1 second has passed...
      currentimage = currentimage - 1 ;Here's how you count down!
      timer = Millisecs() ;reset the timer so that it will wait another second
   EndIf
   If currentimage = 3 ;If the current image is "3"
      DrawImage(three)
   EndIf
   If currentimage = 2 ;If the current image is "2"
      DrawImage(two)
   EndIf
   If currentimage = 1 ;If the current image is "1"
      DrawImage(one)
   EndIf
   If currentimage = 0 ;If the current image is "0"
      DrawImage(Go) ;Draw the go image
   EndIf
   If currentimage < 0 ;If the current image has already drawn "Go"
      pregame = 1 ;You're now ready to leave the mini loop and start the game
   EndIf
   Flip
Wend

;And here's your main loop!
While Not KeyDown(1)
   ;...
Wend
End


Last edited 2012


Happy Llama(Posted 2012) [#4]
The images flash on and off. How do you fix this?


Kryzon(Posted 2012) [#5]
Because he's only drawing IF the timer has ticked.

Always keep logic code separate from rendering code.

AutoMidHandle True

Dim IMG_countDown(3)

IMG_countDown(3) = LoadImage(...) ;"3"
IMG_countDown(2) = LoadImage(...) ;"2"
IMG_countDown(1) = LoadImage(...) ;"1"
IMG_countDown(0) = LoadImage(...) ;"Go"

Local countDownTimer = Millisecs()
Local currentCount = 3

While currentCount >= 0 ;Corrected!
	Cls

	;Rendering (Corrected!)
	DrawImage IMG_countDown(currentCount),GraphicsWidth()/2,GraphicsHeight()/2

	;Logic
	If (countDownTimer + 1000) <= Millisecs() Then
		countDownTimer = Millisecs()
		currentCount = currentCount - 1
	EndIf

	Flip
Wend

;[...]


Last edited 2012


Rob the Great(Posted 2012) [#6]
Whenever 2D graphics flicker like what you've had trouble with recently, it's usually a sign that something's not right with the double buffering system. Note that the above example is pseudo, meaning that it's meant more for concept rather than to be able to execute properly. To ensure that double buffering is set up, you need to do the following in this order:

1. Set the backbuffer. You will only call this once unless you change the buffer in another routine, in which you will need to change it back when the routine is done.
2. Clear the screen. This is done by calling Cls, usually at the top of the loop.
3. Draw your game after Cls. This is done with commands such as DrawImage, Plot, ect.
4. Flip the front buffer (the screen graphics) with the back buffer (what you just drew but isn't on the screen until now). This is done with the "Flip" command, usually at the end of the loop.
5. Repeat steps 2-4 until the end of the game.

The reason why the screen was flickering with the above example is because I didn't set the back buffer and I also forgot to call Cls at the top of the loop. I've edited the post above to fix the flickering problem. Let me know if it still gives you trouble.

Edit: Kryzon's right. That's a pretty silly mistake on my end, so go with his example.

Last edited 2012


Kryzon(Posted 2012) [#7]
Mine was wrong as well, I just updated it :o)


stayne(Posted 2012) [#8]
Always keep logic code separate from rendering code.


Best advice ever.


Happy Llama(Posted 2012) [#9]
OK it works! Instead of the background being black, is there a way to have it be my game in the background?


Kryzon(Posted 2012) [#10]
That loop is counting down and drawing images. If you don't add more instructions to it, that's all it'll do.

You can add a block of code to draw and update characters\environment etc.

Last edited 2012


Happy Llama(Posted 2012) [#11]
I put in my "setup code" into the loop and the game gets all weird and I get debug messages that say stuff doesn't exist. If I put "setup code" into a loop, doesn't it keep rendering/drawing it every frame. Can someone show how you can do this?