1 Attachment(s)
[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:
QueryPerformanceFrequency (lcFreq) 'Get Frequency
ldInterval = lcFreq / cFPS 'Calculate interval based on FPS and Frequency
While gbStatus(geStatus.fRunning)
Sleep (goInterval(geDataSet.Snake).pSpeed / 10)
If Not gbStatus(geStatus.fPause) Then
QueryPerformanceCounter (lcTimer)
If lcTimer >= lcTimer2 + ldInterval Then
If Not CheckCollision Or gbStatus(geStatus.fdebug) Then
' Individual Timers are called within the individual UDT's Section
Call GL_Snake
Call GL_Food
Call GL_Pill
Call GL_CleanUp
End If
QueryPerformanceCounter (lcTimer2)
Else
Call EndGame
gbStatus(geStatus.fRunning) = False
End If
End If
DoEvents
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.)
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