This discussion is archived
7 Replies Latest reply: Jan 1, 2011 1:32 PM by 812592 RSS

two players one keyboard

812592 Newbie
Currently Being Moderated
Hello! Im new here and in Java too ...
anyway i need to write something like racing game for two players ( this is just the beginning ) on one keyboard.
For one player is very easy but when i try to handle second user everything just crash or ain't working as expected.
My point is I cannot define two keyListeners run them as Threads and add to my JFrame ( I think that is my solution )
Can someone give me any advice?
  • 1. Re: two players one keyboard
    gimbal2 Guru
    Currently Being Moderated
    You give an "it doesn't work" problem description. That in itself is absolutely not helpful...

    My point is I cannot define two keyListeners run them as Threads and add to my JFrame
    Who would want to use threads? Use one keylistener and keep one key state, which you use to determine actions for both players.


    Using Google, search for a game called 'gods deluxe' (it should be the first hit). This is a free game written in Java (not by me) and it has full source code included. Look into the classes 'gods.sys.GameEngine' and 'gods.sys.UserInputListener' for an example of how I do things; I "borrowed" this logic for my own games ;)

    Edited by: gimbal2 on 11-nov-2010 2:21
  • 2. Re: two players one keyboard
    812592 Newbie
    Currently Being Moderated
    Thanks i love this game and i have source now =)
    My problem is liminitation of keyboard device i cannot press more than 3 buttons in the same time.
    When i plug-in second keyboard everything works fine but still i need to handle 2 players on one keyboard and right now i have no idea how to deal with this problem.
  • 3. Re: two players one keyboard
    gimbal2 Guru
    Currently Being Moderated
    Nosiu wrote:
    Thanks i love this game and i have source now =)
    My problem is liminitation of keyboard device i cannot press more than 3 buttons in the same time.
    When i plug-in second keyboard everything works fine but still i need to handle 2 players on one keyboard and right now i have no idea how to deal with this problem.
    I know the problem of the limited amount of keypresses (it is related to groups of keys also; sometimes you can manage four); I've had many frustrated 2 player Mortal Kombat sessions because of it. There isn't much you can do about this I'm afraid. When the hardware doesn't allow it, the software cannot do anything.
  • 4. Re: two players one keyboard
    812592 Newbie
    Currently Being Moderated
    gimbal2 wrote:
    I know the problem of the limited amount of keypresses (it is related to groups of keys also; sometimes you can manage four); I've had many frustrated 2 player Mortal Kombat sessions because of it. There isn't much you can do about this I'm afraid. When the hardware doesn't allow it, the software cannot do anything.
    There have to be a way to handle at least 4 buttons.
    About 10 years ago when i used to play with my brother NFS we manage to use 4 buttons! ( gas x2 and turns x2 at the same time)
    There have to be some kind buffering / delaying mechanism.
    When (if) i find solution Im gonna post it.

    Otherwise i ll try to use buttons on different 'lines' from the other groups ( http://www.sjbaker.org/wiki/index.php?title=Keyboards_Are_Evil )

    Edited by: Nosiu on 2010-11-11 07:53
  • 5. Re: two players one keyboard
    gimbal2 Guru
    Currently Being Moderated
    Nosiu wrote:
    gimbal2 wrote:
    I know the problem of the limited amount of keypresses (it is related to groups of keys also; sometimes you can manage four); I've had many frustrated 2 player Mortal Kombat sessions because of it. There isn't much you can do about this I'm afraid. When the hardware doesn't allow it, the software cannot do anything.
    There have to be a way to handle at least 4 buttons.
    About 10 years ago when i used to play with my brother NFS we manage to use 4 buttons! ( gas x2 and turns x2 at the same time)
    There have to be some kind buffering / delaying mechanism.
    When (if) i find solution Im gonna post it.

    Otherwise i ll try to use buttons on different 'lines' from the other groups ( http://www.sjbaker.org/wiki/index.php?title=Keyboards_Are_Evil )
    Good luck. I couldn't even do it with DirectInput (when pressing too many keys, Windows even gave an error beep...)
  • 6. Re: two players one keyboard
    827803 Newbie
    Currently Being Moderated
    Hey, I think I see what the problem is, and to be honest, I may be wrong but you seem to be looking past the obvious!

    There is no need at all to create two differant keylisteners, even if it means re-writing code, it will always be easier to initiate only one keylistener which responds to both sets of commands, eg UP/DOWN/LEFT/RIGHT and w/a/s/d , and then reacts to the corresponding method. If you're running an applet, initiation can be done inside the init method, however with JFrame I guess it's done during the main instancing method of that....

    Sorry if I couldn't help!
  • 7. Re: two players one keyboard
    812592 Newbie
    Currently Being Moderated
    The problem is very simple ;) keyboard (hardware) cannot handle 4 or more buttons in the same time (keeps beeping).
    I have to plug-in second keyboard to manage that.
    It's not Java problem as I thought.

    PS.

    Of course You are right about implementing only 1 keyListener.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points