Talk about the control of game speed and game input

xiaoxiao2021-03-06  22

The speed control of the game and the processing of the keyboard are the key links. Many people don't know how to do it, I will talk about some of my experience and opinions here.

Many people use Delay functions and clear keyboard cuffs to handle game speed and game input, actually a misunderstanding.

In the game programming, it is generally not used to control the speed of the game, this method is the most stupid, because the delay function is actually unsemprecious, the CPU is a waste of the CPU resource, if you can use the CPU The delay time is liberated, and another mechanism is used to control the game speed, then the utilization rate of the game on the CPU resource will be relatively high, so that the game's response to various events will be faster:

Look at the following code:

Main () {Int key;

While (1) {while (! kbhit ()) {// No keyboard input game loop code // The following uses a delay function to control the game speed DELAY (10000);

Key = getCH ();

/ / Is the code Switch (key) {CASE 'A': Break; Case 'B': Break; Default: Break;}}}}

The above code is a game loop code I have used early, using this code to implement the function of the game cycle, and can respond to the keyboard input. But this is an extremely stupid approach that its maximum disadvantage is above the control of the game speed. In the inner While loop, if there is no key, the program is always executed in this While loop, because in order to control the game speed, add the delay function in this While loop, do a delay. If the delay is 0.5 seconds, the execution time of the entire While cycle is at least 0.5 seconds. If the program enters this while loop because there is no keyboard input, then this time, no matter how you tap the keyboard, you will not respond before the code in the While loop, so your keyboard input is deposited. The keyboard buffer is going. When the code in the While is executed, it has already been 0.5 seconds, this time the program is returned to while (! Kbhit ()) execution, if you press the keyboard at this time, the program will jump to key = getch () Pentress. If the execution time of while (! Kbhit ()) is 0.0001 seconds (actually shorter than this is short), the entire can respond to input is only 0.0001 / (0.5 0.0001), which is a quite small chance. So most of the buttons can not be handled in time, and is sent to the keyboard buffer. So there will be a problem with the response of the game. Many people think that you need to clear the keyboard buffer, which is actually a misunderstanding. The problem is not in the keyboard buffer, but the structure of the program has a problem. This kind of program structure is quite low.

The usual delay function is a loop of the following:

Void delay (long time) {while (- time! = 0) {}}

Such a delay is actually in a constant loop, in which such a delay function is simply a waste of valuable CPU resources. If your game cycle code is only 0.005 seconds, but this speed is inevitable to players. In order to stabilize the speed of the game cycle to 0.5 seconds, use the delay function to control the game speed, this most The CPU resource is actually gave the delay function, and the Delay function occupies a lot of CPU time, and only very few times are used on your game loop code to complete the calculation of game data and respond to input events. . Such a result will inevitably lead to a serious impact in all aspects of the game, especially the response to the button input. This is what any horizontal game programmer cannot be tolerated. Even if you adopt a clear keyboard buffer technology, this problem is still unable to solve, because this is the defect on the program structure, in addition to the structure of the program, there is no other way. It can be seen that even if the keyboard buffer is cleared, it will still be in response, just avoiding the delay response, is the previous button response. Don't believe it, you can try it. Therefore, the most fundamental approach is to change the structure of the program, some people put forward the use of keyboard interrupts and clock interruptions, which is indeed a good solution. But actually there is no need to modify the method of modifying the keyboard. I don't like it very much. One is trouble, the second is not safe, the clock interruption is even more. The following is my solution, the effect is not more than the keyboard is broken:

Main () {int Timer; // Clock cycle int cur_time; // clock counter

// The following initialization clock Timer = 10000; CUR_TIME = Timer;

While (1) {while (! kbhit ()) {// below the code count IF (CUR_TIME == 0) {CUR_TIME = Timer;} else {CUR_TIME -

// If counting to 1, execute the game cycle IF (CUR_TIME == 0) {// game loop code}}

Key = getCH ();

/ / Is the code Switch (key) {CASE 'A': Break; Case 'B': Break; Default: Break;}}}}

The program structure is basically the same as the previous program, just on the speed of the control game, no longer uses the Delay function, but uses a clock to implement. This clock has a cycle, which is the value of the Timer variable, and a counter is the cur_time variable, and people who have learned 8054 timers should not be unfamiliar. Every time I experience a while loop, the value of the counter will be reduced until it is reduced to zero, and it will return to the new timing starting point.

In this new structure, if the cur_time is not equal, the While (! Kbhit ()) is executed in the loop is only if (cur_time == 0) {CUR_TIME = Timer;} else {cur_time--;} This section is short The code is quite short, so there is more time to detect the keyboard input (program execution to while (! Kbhit ()) is the detection keyboard input), turn it to the Switch statement immediately after detecting the input Treatment. Due to the introduction of the timer, the speed of the game has also been well controlled. While (! Kbhit ()) will be executed every cycle Timer, which will execute "// game loop code" once, which modifies the value of Timer. It is convenient to control the speed of the game. There is no waste of the delay function, while (! Kbhit ()) is quite short, so detecting the chance of playing the player's keyboard input, so the speed of the response is quite fast, quite ideal There is no feeling of delay at all.

In order to accurately control the speed of the game, it is also possible to use this method to combine the clock interrupt together. The specific principle is that the clock interrupt program is responsible for counting the clock, and in the While loop is only responsible for determining the count to 1, if it is 1, execute the game cycle, then set the count value to 0 after execution; if count The value does not have anything to 1.

The speed control of your program can be accurately used by the exact count of the clock interruption. In practice, it is usually used in this combined clock interruption game control method. You can think about it. The basic principle is that I have so much above, you can think of it.

This approach has been verified in a game I have made, so everyone does not have to doubt the correctness of the above statement. Since this game has not yet been completed, it will not be released, if you really want to see, you can send me email.

The above is my handling method. If you encounter trouble in the game speed control and keyboard input control, you may wish to refer to my way. The above approach is also summarized by some game code for Crazy Bug. Of course, if you have a better way, you may wish to write into the article published in the forum, share sharing with you, don't be too selfish.

Author: RockCarry studio Chen Kai 20053.23 Copyright, please do not reprint

转载请注明原文地址:https://www.9cbs.com/read-43738.html

New Post(0)