PROBLEM - Block breaker with air hockey PLAYED with 2 HUMAN PLAYERS

Started by
3 comments, last by jbadams 11 years, 6 months ago
Hi Guys,
I just want to ask if can I check for "KEY"(keyboard) that is HOLD/PRESSED for a long time, because I am to create a clone of breakout with air hockey for 2 different human players.
Here's the list of my concern:

1. Do I need other/ 3rd party library for KEY HOLDS?

2. Is multi-threading needed? I don't know anything about this multi-threading stuff and
I don't think about using one(I'm just a NEWBIE).

3. One more thing, what if the two players pressed their respective key at the same time,
how can I program to avoid error or worse one player's key is prioritized first before
the the key of the other.
example:
Player 1 = W for UP & S for DOWN
Player 2 = O for UP & L for DOWN
(example: W & L is pressed at the same time)
Great thanks for help.
PS: I use GLUT for the visuals of the game.
Advertisement
You do need a library to get this kind of access to the keyboard. Each OS most likely provides a native way to do this, I would probably use a library that abstracts the details of the platform away, like SDL of SFML. No need to use multithreading for this.

Read a bit of documentation and maybe a tutorial on how to do keyboard input with SDL or SFML and post back if you can't figure it out.
#1 no... easy as pie, use 2 arrays, on keydown, mark key as down, on key up, mark key up... next frame, copy 1st array to the second, and repeat... A key that was down last frame and this frame is held, else it's a new button press.

#2 no...

#3 Input should be just like any other task such as physics, or rendering, ie: it will be processed during a slice of the timestep, so collect your key ups key downs when they occur, process them all at once.

As Alvaro mentioned, each os will have it's own method of providing keyboard input. In windows, this would be the windows message pump, and the keyup/keydown messages. This is where a prebuilt library will help, but it is not necessary.
thank you very much guys :D
I think the above answers have covered your question reasonably well, but I just wanted to note that if you're already using GLUT for graphics it does also provide support for keyboard input, so you wouldn't necessarily want SDL or SFML as suggested above -- although they are certainly good choices. As mentioned above, you don't have to use one of these libraries -- you could just use the calls provided by your operating system -- but using a library such as GLUT, SDL, SFML, Allegro, etc. will make things much easier on you and make it easier to write cross-platform code if you wish to do so once you're more experienced.

You can find a couple of tutorials on keyboard handling with GLUT here and here.


For your third question, certain keyboards can have trouble with missing key events if certain combinations of keys are pressed simultaneously, but this is a hardware problem that even effects commercial AAA titles and isn't something you should really worry about, and would only normally be a problem if you would expect a large number of keys to be held down simultaneously. As mentioned by others, the order of key-presses should not be a problem.


Let us know if you have trouble implementing your input and we'll give you some more tips, but hopefully you've got a good starting point. You should probably stick with a nice simple input system for your first couple of games, but once you've got a good handle on that you might consider more advanced functionality such as allowing your players to re-map the keys; there are some thoughts on how to do so in ApochPiQ's journal entry "designing a robust input handling system for games" and a link to a sample implementation of the code, although it may be a bit advanced till you've had some more practice.


Hope that's helpful! smile.png

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement