Results 1 to 2 of 2

Thread: [VB] Yet Another Game Loop Thread

  1. #1

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Question [VB] Yet Another Game Loop Thread

    To pick up on this link and this link, I'm wrapping up on my first Snakes game based on NoteMe's Snakes 0.4 Tutorial. I added some basic collision detection (Wall & Snake Parts), added some different kinds of food, and a basic pill/power-up system. However, I started going off-road with some of the game loop mechanics.

    I wanted to set it up where the speed of the snake could be affected without changing the duration/affect of the food and pills. At that point, I learned the hard way that setting up different main timer loops--not timer the object--for each element was a recipe for trouble and pain. Then, I put everything back on a main timer loops and provided secondary (?) timers for each element. My thinking was along the lines of speeding up the snake really didn't affect the duration of the items in the game. Now, I could slow the snake down, decrease the duration of the food, and affect the scoring if you are able to get to said food. From that point, you could expand the concept using the power-up system to affect the scoring depending if the power-up made it easier or more challenging for you. (More details can be provided if necessary, and I've provided a zip file with the entire project)

    Now the source code (VB6) for the main loop taken from Cyborg's Tutorial :

    VB Code:
    1. QueryPerformanceFrequency (lcFreq)  'Get Frequency
    2.     ldInterval = lcFreq / cFPS          'Calculate interval based on FPS and Frequency
    3.  
    4.     While gbStatus(geStatus.fRunning)
    5.    
    6.         Sleep (goInterval(geDataSet.Snake).pSpeed / 10)
    7.         If Not gbStatus(geStatus.fPause) Then
    8.            
    9.             QueryPerformanceCounter (lcTimer)
    10.            
    11.             If lcTimer >= lcTimer2 + ldInterval Then
    12.            
    13.                 If Not CheckCollision Or gbStatus(geStatus.fdebug) Then
    14.                     ' Individual Timers are called within the individual UDT's Section
    15.                     Call GL_Snake
    16.                     Call GL_Food
    17.                     Call GL_Pill
    18.                     Call GL_CleanUp
    19.                    
    20.                 End If
    21.                
    22.                 QueryPerformanceCounter (lcTimer2)
    23.                
    24.             Else
    25.                 Call EndGame
    26.                 gbStatus(geStatus.fRunning) = False
    27.  
    28.             End If
    29.         End If
    30.        
    31.         DoEvents
    32.     Wend

    Are there simplier or easier ways to handle these timers within timers? More importantly, is there a way to make it behave more predictably?

    One of the other things that concerns me is that if I don't use Sleep() calls, the CPU will just munch on itself indefinately, and since this game is simple, I don't see the need to make it take over the CPU. Are there other options to make sure the program plays nice with the other apps? (If I was writing an in-depth FPS, then I may need those cycles, but as it is now, I don't.)
    Attached Files Attached Files
    Last edited by Fedhax; Aug 8th, 2006 at 09:03 AM. Reason: VB Version Clarification

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: [VB] Yet Another Game Loop Thread

    There is a class module out there somewhere called clsTick. With that, you can easily setup a few timed intervals. I'm using it for a game atm, a clsTick for my main game loop, and a few more for things like AI thinking routines, etc.

    EDIT: "a few timed intervals"...not just one

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width