Using a timer and game loop

I create a simple console game, there is a player who moves when a key is pressed, and there are enemies that move automatically, each type of enemy moves once in X milliseconds.

As I understand it, I should use a timer , but I really do not know how to do this in the game loop (not yet built, because I do not know how to do this with a timer, but it should be a while loop I think). the game ends when the opponent โ€œtouchesโ€ the player (the same x and y).

One important thing: I canโ€™t take you to this exercise in Thread , but if you have other suggestions instead of using a timer , you can.

Thanks.

+6
source share
2 answers

Usually you do not use regular timers in games. Games have a completely different mechanism for processing their logic and the time that has passed, they usually do not work with timers or not in the way you expect:

Games usually have something called a game loop. Generally speaking, these are three main functions that are called one after another in a loop:

 while(running) { HandleUserInput(); ChangeWorld(); Render(); } 

You get user input, you change the world of the game accordingly and draw it on the screen. Now, the faster your computer, the faster this cycle. This is good for graphics (I think FPS), but bad for the game. Imagine Tetris where each frame moves. Now I would not want to buy a faster computer, the game would become more complex.

Thus, in order to maintain a constant speed of the game regardless of the power of the computer, the cycle takes into account the elapsed time:

 while(running) { var timePassedSinceLastLoop = CalculateTimeDelta(); HandleUserInput(); ChangeWorld(timePassedSinceLastLoop); Render(); } 

Now imagine a cooldown for something in the game. The player pressed "a", some cool action happened, and although he can press "a" again, nothing will happen within the next 5 seconds. But the game still works and does everything that can happen. This is not an ordinary timer. This variable allows you to call it ActionCooldown, and as soon as the player starts the action, he sets the value to 5 seconds. Every time the world changes, timePassed is subtracted from this number to zero. The game runs all the time and processes input and rendering. But only once ActionCooldown reaches zero, another press of the "a" button will start this action again.

The ChangeWorld method includes all automatic changes in the world. Enemies, missiles, any movements without interacting with the player. And He moves with time. If the adversary moves one square per second, you need to make its coordinate a float and add part of the square every time the cycle starts.

Let's say you have 30 frames per second, so your cycle runs 30 times per second. Now your enemy needs to move 1/30 square for each cycle. Then he eventually moved one full square per second.

+16
source

A common premise behind a timer is to repeat some code every n.

To create a timer, use this:

  System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); // Set the Interval to 1 millisecond. Note: Time is set in Milliseconds aTimer.Interval=1; aTimer.Enabled=true; 

Then you implement this method:

 private static void OnTimedEvent(object source, ElapsedEventArgs e) { //Whatever you need repeated } 

A complete example can be found here: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.71).aspx

0
source

Source: https://habr.com/ru/post/948602/


All Articles