Real time in games

Just starting out? Need help? Post your questions and find answers here.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Real time in games

Post 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 :)
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Real time in games

Post 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
User avatar
shadow
User
User
Posts: 34
Joined: Thu Dec 16, 2010 1:25 pm
Location: Germany

Re: Real time in games

Post by shadow »

Or just use the built in Timer library :wink:

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)
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Real time in games

Post by J@ckWhiteIII »

@kenmo: I never hear a sound :/

...I added an own sound, of course...
Sirius-2337
User
User
Posts: 59
Joined: Sat May 14, 2011 10:39 am

Re: Real time in games

Post 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
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Real time in games

Post by J@ckWhiteIII »

That works much better :D

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
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Real time in games

Post by Nubcake »

J@ckWhiteIII wrote:That works much better :D

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 ?
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Real time in games

Post by J@ckWhiteIII »

Because I don't understand threads yet. But if you can explain it in a more simple way than the manual... :D
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
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Real time in games

Post 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.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Real time in games

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Real time in games

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Real time in games

Post 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) :/
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Real time in games

Post by IdeasVacuum »

...Debug is your friend 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Real time in games

Post by J@ckWhiteIII »

Okay...Did that now, haven't got any helpful results. So, I'm gonna re-write that part of the code :D
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Real time in games

Post by kenmo »

Debug didn't help? Debug always helps! :D 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
Post Reply