Friday, July 22, 2022
HomeGame Developmentc++ - Enter response is gradual in two participant snake recreation

c++ – Enter response is gradual in two participant snake recreation


I feel i name getKEYS2() however program tries to run getKEYS1() too and that is why response is gradual. Am i mistaken?

You’re mistaken. C++ doesn’t randomly name a totally completely different perform with the same identify until you ask it to.

The issue right here is your logic. In every of those strategies you get a key, any key that is been pressed, however then verify provided that it is a specific participant’s enter. If that key you detected was for the alternative participant, it is ignored. You are relying on _kbhit and _getch to filter to solely return you one participant’s keys, however these capabilities do not know something about that.

You understand a delay since you’re leaving it as much as probability which key presses you act on, primarily based on the order they arrive in.

As an instance participant 1 presses and holds w on the similar second participant 2 presses and holds u. For no matter {hardware} motive, the u will get detected first, so your console enter buffer this body reads uw.

You run getKeys1() and pull the primary character out of the buffer, u, then verify if it is any of participant 1’s keys. It is not, so you do not do something with it. You then run getKeys2() and pull the following character out of the buffer, w, and verify if it is any of participant 2’s keys. It is not, so this enter can be unhandled.

If the gamers hold holding the keys, the console will learn uwuwuwuwuw with new characters getting added on the configured key repeat fee. So this sample will repeat each body till one participant lets go of a key.

Participant 2’s enter will solely ever be appropriately dealt with on frames the place you are fortunate sufficient that participant 1 additionally pressed a key and it was added to the buffer earlier than participant 2’s.

So, that you must rethink the way in which you are dealing with this enter. Learn the enter buffer in a loop, and verify the ensuing character towards each gamers’ keys. That manner, the enter will get dealt with appropriately irrespective of what number of keys have been pressed this body, or the order they have been detected and saved.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments