By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,718 Members | 1,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,718 IT Pros & Developers. It's quick & easy.

Using a timer in a game

P: 8
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:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form_Timer()
  2. Static intCounter As Integer
  4. intCounter = intCounter + 1
  6. If intStop = 3 Then 'so when the user presses the button (the code for pressing the button is simply intStop = 3 on click)
  7. Me.TimerInterval = 0
  8. intCounter = 0
  9. End If
  11. If intCounter = intSeconden Then 'when the timer hits the max allowed seconds for the user to hit his button
  12. Me.TimerInterval = 0
  13. intCounter = 0
  14. intStop = Int((2 - 1 + 1) * Rnd + 1) 'randomizes which computer will answer
  15. End If 
Code for the game:
Expand|Select|Wrap|Line Numbers
  1. 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
  3. intSeconden = Int((rsVragen.Fields(2) - 1 + 1) * Rnd + 1) 'randomizes the time given before the computer answers.
  4. Me.TimerInterval = 1000 'sets the timer to tick every second.
  6. Do Until intStop = 1 or intStop = 2 Or intstop = 3
  7. 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
  9. Select Case intStop
  10.     Case 1
  11.      intGoedSlecht = Int((2 - 1 + 1) * Rnd + 1) 'randomizes whether the first computer's answer is right or wrong
  12.     Case 2
  13.      same thing here for comp 2
  14.     Case 3
  15.     here I have a working program to determine of the user's answer is correct or false
  16. End select
Aug 5 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 8,704
  1. Try modifying the following Code Segment:
    Expand|Select|Wrap|Line Numbers
    1. Do Until intStop = 1 Or intStop = 2 Or intStop = 3
    2.   DoEvents
    3. 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.
Aug 5 '10 #2

Expert Mod 15k+
P: 31,769
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.
Aug 5 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.