wyrd
Senior Contributor
EDIT: Nevermind, I simply forgot Application.DoEvents *slaps self* .. works fine now.
I have my main game code inside a class (Game). The form simply creates a new instance of Game (passing in this.CreateGraphics() which the game class uses for displaying images), and then calls Game.Start() to start the game. The problem is the game loop goes nuts and never returns control of the form to the user (the form freezes and you can't do anything to it).
At first I thought maybe it was because the loop was inside a class, so I put the game loop inside the form and just called this.Refresh() for game updates. That didn't seem to work.
It's been a while since I've made a game loop, so am I missing something? Did I do my game loop wrong?
Would passing in this.CreateGraphics() (from the form) cause this problem? I am correct in using this graphics object to display images right? I'll debug the rest of my code just to see if this problem could be from something else, but I'm pretty convinced it's my game loop (it's been a while since I've made one, so I just hacked this thing together).
I have my main game code inside a class (Game). The form simply creates a new instance of Game (passing in this.CreateGraphics() which the game class uses for displaying images), and then calls Game.Start() to start the game. The problem is the game loop goes nuts and never returns control of the form to the user (the form freezes and you can't do anything to it).
At first I thought maybe it was because the loop was inside a class, so I put the game loop inside the form and just called this.Refresh() for game updates. That didn't seem to work.
It's been a while since I've made a game loop, so am I missing something? Did I do my game loop wrong?
Would passing in this.CreateGraphics() (from the form) cause this problem? I am correct in using this graphics object to display images right? I'll debug the rest of my code just to see if this problem could be from something else, but I'm pretty convinced it's my game loop (it's been a while since I've made one, so I just hacked this thing together).
C#:
/// <summary>
/// Starts the game.
/// </summary>
public void Start() {
// Set initial mode.
_mode = GameModes.FallingShape;
// Time to next update.
int nextTime = 0;
// Go until game ends.
while (_mode != GameModes.End) {
// Check if game needs update.
if (Environment.TickCount > nextTime) {
_update();
// Get next update time.
nextTime = (1000 / GameSpeed) + Environment.TickCount;
}
}
}
Last edited: