• Advertisement
Sign in to follow this  

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

This topic is 1904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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. Edited by alvaro

Share this post


Link to post
Share on other sites
#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.

Share this post


Link to post
Share on other sites
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 [i]are[/i] 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 [url="http://www.swiftless.com/tutorials/opengl/keyboard.html"]here[/url] and [url="http://www.lighthouse3d.com/tutorials/glut-tutorial/keyboard/"]here[/url].


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 "[url="http://www.gamedev.net/blog/355/entry-2250186-designing-a-robust-input-handling-system-for-games/"]designing a robust input handling system for games[/url]" 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! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement