Using a timer in a game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Will The Gray
    New Member
    • Jul 2010
    • 8

    Using a timer in a game

    Hi guys

    I have a problem with programming a small game. The idea is that a question pops up, a timer starts to run randomly from one to a given number (that number I take from a database, every question has a different allowed time to "think"), when the timer ends, a random computer opponent between 1 and 2 answers the question randomly correct or incorrect. If the user presses a button, the timer stops, lets the user answer correct or incorrect.

    If it's correct, a new question pops up. If incorrect, the timer resets but the player that made an incorrect answer is no longer allowed to participate for that question.

    Now, with the following code I seem to end up in an endless loop (I only put down the relevant code):

    Code for the timer:

    Code:
     Private Sub Form_Timer()
    Static intCounter As Integer
    
    intCounter = intCounter + 1
    
    If intStop = 3 Then 'so when the user presses the button (the code for pressing the button is simply intStop = 3 on click)
    Me.TimerInterval = 0
    intCounter = 0
    End If
    
    If intCounter = intSeconden Then 'when the timer hits the max allowed seconds for the user to hit his button
    Me.TimerInterval = 0
    intCounter = 0
    intStop = Int((2 - 1 + 1) * Rnd + 1) 'randomizes which computer will answer
    End If
    Code for the game:
    Code:
    Set rsVragen = db.OpenRecordset("SELECT VraagNr, Vraag, Seconden, Categoriecode, Randomizer(VraagNr) FROM tblVragen WHERE categoriecode = '" & strCategorie & "' ORDER BY Randomizer(VraagNr)") 'Selects the questions out of a database
    
    intSeconden = Int((rsVragen.Fields(2) - 1 + 1) * Rnd + 1) 'randomizes the time given before the computer answers.
    Me.TimerInterval = 1000 'sets the timer to tick every second.
    
    Do Until intStop = 1 or intStop = 2 Or intstop = 3
    Loop 'Probably where the mistake is, I try to make the game "wait" until the timer reached the end or the user pressed the button
    
    Select Case intStop
        Case 1
         intGoedSlecht = Int((2 - 1 + 1) * Rnd + 1) 'randomizes whether the first computer's answer is right or wrong
        Case 2
         same thing here for comp 2
        Case 3
        here I have a working program to determine of the user's answer is correct or false
    End select
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    1. Try modifying the following Code Segment:
      Code:
      Do Until intStop = 1 Or intStop = 2 Or intStop = 3
        DoEvents
      Loop
    2. Keep in mind that intStop must be declared at the Modular, and not Procedural level, in order for the code to be effective.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32636

      #3
      Ideally, you would not have any loop going waiting for a state to exist. DoEvents is a good thing to use if you must, but a design that doesn't require any looping is more natural to how Access works.

      Consider code to :
      1. Initiate the timer and set up the form for the question.
      2. Be called by the timer to instigate the answer coming from either of the other two PCs. This code must clear away tidily and completely when completed. If it needs to reset or reinitiate the timer code then that's fine.

      The timer should reset itself when its job is completed.

      Ideally, it would be nice to have a reliable trigger such that when a state is reached it would start off the code to be called from within the timer routine, but this shouldn't be critical. It's not like it's proper interrupt coding (very fiddly indeed). It's more like DPCs (Deferred Procedure Calls) where the interrupt processing simply tidies things up but leaves a flag for the scheduler to say it should execute specific code when it gets the chance to outside of the interrup routine.

      Comment

      Working...