Thank you in advance
Real time in games
- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Real time in games
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
Thank you in advance
Re: Real time in games
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:
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
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
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
ThinkPad T61 | PureBasic 4.51 | Windows 7 (32) | Kubuntu 11.10 (64) | Syllable (32)
- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Re: Real time in games
@kenmo: I never hear a sound :/
...I added an own sound, of course...
...I added an own sound, of course...
-
Sirius-2337
- User

- Posts: 59
- Joined: Sat May 14, 2011 10:39 am
Re: Real time in games
Yes, that is because 'MouseClicked' never gets #True and because '(' and ')' are missing after the 2nd 'ElapsedMilliseconds'.J@ckWhiteIII wrote:@kenmo: I never hear a sound :/
...I added an own sound, of course...
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- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Re: Real time in games
That works much better 
This is how I'm using it:
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
Why not make the CountDown a thread ?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
- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Re: Real time in games
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
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
Because you get unnecessary overhead.Nubcake wrote: Why not make the CountDown a thread ?
The easiest way to do it and the way any game does it is what kenmo described.
- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Re: Real time in games
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
-
IdeasVacuum
- Always Here

- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Real time in games
...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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Re: Real time in games
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) :/
-
IdeasVacuum
- Always Here

- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Real time in games
...Debug is your friend 
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
- J@ckWhiteIII
- Enthusiast

- Posts: 183
- Joined: Fri May 25, 2012 7:39 pm
Re: Real time in games
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
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:
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