Page 1 of 2
Real time in games
Posted: Mon Jul 02, 2012 2:39 pm
by J@ckWhiteIII
Hey, I just wanted to ask if there's an EASY way of using real time in games (like an event that happens 10 seconds after a button was clicked). I don't want delay, I just want (for example) a counter that counts from 0 to 10 (in seconds). have searched for it on this forum and read the date help. I hope someone can help me.
Thank you in advance

Re: Real time in games
Posted: Mon Jul 02, 2012 2:56 pm
by kenmo
Look up ElapsedMilliseconds().... just record the time when a trigger occurs (such as a mouse click) and compare it with the current time as you go on.
Pseudo-code:
Code: Select all
Repeat
If MouseClicked
StartTime = ElapsedMilliseconds() ; record start time
EndIf
If StartTime > 0 ; if there is an active countdown
If ElapsedMilliseconds - StartTime > 3 * 1000 ; wait 3 seconds (3000 ms)
PlaySound(Alert)
StartTime = 0 ; disable timer again
EndIf
EndIf
Forever
Re: Real time in games
Posted: Mon Jul 02, 2012 3:52 pm
by shadow
Or just use the built in Timer library
Edit:
Sorry, have mixed that with an other programming language. The PureTools have a timer library.
Another way is to use AddWindowTimer if you have one.
http://www.purebasic.com/documentation/ ... timer.html
Re: Real time in games
Posted: Mon Jul 02, 2012 4:21 pm
by J@ckWhiteIII
@kenmo: I never hear a sound :/
...I added an own sound, of course...
Re: Real time in games
Posted: Mon Jul 02, 2012 4:57 pm
by Sirius-2337
J@ckWhiteIII wrote:@kenmo: I never hear a sound :/
...I added an own sound, of course...
Yes, that is because 'MouseClicked' never gets #True and because '(' and ')' are missing after the 2nd 'ElapsedMilliseconds'.
Try this Code
Code: Select all
OpenWindow (0, 0, 0, 400, 300, "Timer", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, WindowWidth(0), WindowHeight(0))
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
StartTime = ElapsedMilliseconds()
EndIf
EndSelect
StartDrawing(CanvasOutput(0))
If StartTime
Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
DrawText(5, 10, Str(ElapsedMilliseconds() - StartTime) + " ms", $000000, $FFFFFF)
DrawText(5, 30, Str((ElapsedMilliseconds() - StartTime) / 1000) + " Seconds", $000000, $FFFFFF)
If ElapsedMilliseconds() - StartTime > 3 * 1000 ; wait 3 seconds (3000 ms)
StartTime = 0 ; disable timer again
Circle(200, 50, 28, Random($FAFAFA))
EndIf
EndIf
StopDrawing()
Delay(10)
ForEver
Re: Real time in games
Posted: Mon Jul 02, 2012 5:07 pm
by J@ckWhiteIII
That works much better
This is how I'm using it:
Code: Select all
Procedure CountDown ()
If StartTime > 0 ; if there is an active countdown
If ElapsedMilliseconds () - StartTime > 3 * 1000 ; wait 3 seconds (3000 ms)
PlaySound(1)
StartTime = 0 ; disable timer again
EndIf
EndIf
EndProcedure
If circlex < 0
StartTime = ElapsedMilliseconds() ; record start time
PlaySound (0)
circlex = 320
circley = Random (480)
pseudomovex = movex
pseudomovey = movey
movex = 0
movey = 0
Score2 + 1
CountDown ()
movex = pseudomovex
movey = pseudomovey
EndIf
Re: Real time in games
Posted: Mon Jul 02, 2012 10:40 pm
by Nubcake
J@ckWhiteIII wrote:That works much better
This is how I'm using it:
Code: Select all
Procedure CountDown ()
If StartTime > 0 ; if there is an active countdown
If ElapsedMilliseconds () - StartTime > 3 * 1000 ; wait 3 seconds (3000 ms)
PlaySound(1)
StartTime = 0 ; disable timer again
EndIf
EndIf
EndProcedure
If circlex < 0
StartTime = ElapsedMilliseconds() ; record start time
PlaySound (0)
circlex = 320
circley = Random (480)
pseudomovex = movex
pseudomovey = movey
movex = 0
movey = 0
Score2 + 1
CountDown ()
movex = pseudomovex
movey = pseudomovey
EndIf
Why not make the CountDown a thread ?
Re: Real time in games
Posted: Mon Jul 02, 2012 10:46 pm
by J@ckWhiteIII
Because I don't understand threads yet. But if you can explain it in a more simple way than the manual...
How would using a thread make it work better?
And this code doesn't work properly...the ball (circlex/circley) keeps moving during the 3 seconds
By the way, i changed the code a bit, i'm gonna post the most recent code tomorrow
Re: Real time in games
Posted: Mon Jul 02, 2012 11:02 pm
by Thorium
Nubcake wrote:
Why not make the CountDown a thread ?
Because you get unnecessary overhead.
The easiest way to do it and the way any game does it is what kenmo described.
Re: Real time in games
Posted: Tue Jul 03, 2012 4:06 pm
by J@ckWhiteIII
Code: Select all
Procedure CountDown ()
If Blocky ; if there is an active countdown
If ElapsedMilliseconds () - StartTime > 3 * 1000 ; wait 3 seconds (3000 ms)
StartTime = 0 ; disable timer again
PlaySound(0)
movex = pseudomovex
movey = pseudomovey
EndIf
EndIf
EndProcedure
If circlex < 0
StartTime = ElapsedMilliseconds() ; record start time
PlaySound (0)
circlex = 320
circley = Random (480)
pseudomovex = movex
pseudomovey = movey
movex = 0
movey = 0
Score2 + 1
CountDown ()
EndIf
Code as it is now...But still now sound.
Re: Real time in games
Posted: Tue Jul 03, 2012 4:16 pm
by IdeasVacuum
...must have something to do with the Load Sound? Make it a Global if not already and use Enumeration so that it does not have the same ID as another item.
Re: Real time in games
Posted: Tue Jul 03, 2012 4:25 pm
by J@ckWhiteIII
Hm..Checked the sound, it's initialized and it has a unique ID now. But still, no sound/the circle doesn't move (seems like the countdown doesn't work) :/
Re: Real time in games
Posted: Tue Jul 03, 2012 4:37 pm
by IdeasVacuum
...Debug is your friend

Re: Real time in games
Posted: Tue Jul 03, 2012 5:02 pm
by J@ckWhiteIII
Okay...Did that now, haven't got any helpful results. So, I'm gonna re-write that part of the code

Re: Real time in games
Posted: Tue Jul 03, 2012 5:09 pm
by kenmo
Debug didn't help? Debug always helps!

Use it to make sure variable values are what you expect, and also print out some text statements just to make sure the right parts of code are being run as you expect.
I don't know what the problem with the sound is, but that was just an example to show you how to manage a simple timer. Here is a fully-working example:
Code: Select all
Background.i = #Black
BallY.i = 150
BallDir.i = 1
If InitSprite() And InitMouse()
If OpenWindow(0, 0, 0, 400, 300, "Left-click to set timer, right-click to exit", #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)
Repeat
ExamineMouse()
; Red screen when clicked
If (MouseButton(1))
Background = #Red
StartTime = ElapsedMilliseconds()
EndIf
; Blue screen after timeout
If (StartTime > 0)
If (ElapsedMilliseconds() - StartTime) > 2*1000
Background = #Blue
StartTime = 0
EndIf
EndIf
; Move ball
If (BallDir > 0)
BallY + 1
If BallY > 290
BallDir = -1
EndIf
Else
BallY - 1
If BallY < 10
BallDir = 1
EndIf
EndIf
; This is a cheap trick so the window won't lock up...
While WindowEvent() : Wend
; Draw screen
ClearScreen(Background)
If StartDrawing(ScreenOutput())
Circle(OutputWidth()/2, BallY, 10, #White)
StopDrawing()
EndIf
FlipBuffers()
Delay(16)
Until MouseButton(2)
HideWindow(0, #True)
Delay(250)
EndIf
EndIf
EndIf