I have recently asked a question here about programming a game so it will be efficient and won't lag.
I have made a game that lags like hell. I suspect that the way that I programmed it's game-loop, could be the source of the problem.
I want to describe to you in general the situation in my game, and then present my way of programming it's game loop.
It's a game for two players. Each player controls a tank. Each tank can shoot missiles. Each tank can collect 'gifts' from the field.
- Each missile of tank1 can collide with tank2, and each missile of tank2 can collide with tank1.
- Each missile can collide with a boundary of the screen.
- Each tank can collide with the other tank.
- Each tank can collide with 'gifts'.
- All missiles constantly move.
- Tanks move when specific buttons on the keyboard are pushed.
My game loop (Each stage is inside a method called from the game loop):
- Loop through all the missiles on the screen, and update their location (move them).
- Loop through all of tank1's missiles, and for every one check if collides with tank2.
- Loop through all of tank2's missiles, and for every one check if collide with tank1.
- Loop through all the missiles on the screen, and check if collide with screen boundaries.
- Check if specific keys are pressed on the keyboard. If so, move tanks.
- Check if the two tanks collide.
- Loop through all of the 4 'gifts' on the screen, and check if they touch a tank.
Questions:
- This game-loop is probably inefficient. How inefficient is it? A little, or a lot?
- How can I improve the game loop and/or the way it's methods work? How can I achieve the same thing, more efficiently?
- How likely is it for a game-loop to be the main cause for poor performance level? How common is it in games for this to be the source of the problem?
Appreciate your help.