Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  KeyListener, Multiplayer input  (Read 2357 times)
0 Members and 1 Guest are viewing this topic.
Offline Pyrodragoon

Junior Devvie




Art calculated with java...


« Posted 2010-02-07 13:43:29 »

Hi again  Wink

I wrote a little game where up to 4 players can
drive a little car and play against the other players.
You can use four keys to steer the car and two
keys to shoot some nasty little bullets (from a pneumatic stapler for example).
It works really well with different maps and special effects and runs
with up to 60 fps with Fullscreenmode.

The big problem is that sometimes the keyevents don't do what they should do.
The game should be played on  one keyboard and even with only two players
errors already occur.
Sometimes when I realease  the firekey, it keeps on firing
or I can't steer left or the other player is accelerating although he released the key.

Here's the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
public void keyPressed(KeyEvent e) {
      int key = e.getKeyCode();
      if(active){
         int i = 0;
         for(int[] q : SnS.keys){
            int i2 = 0;
            for(int q2 : q){
               if(q2 == key){
                  keyDown[i][i2] = true;
               }
               i2++;
            }
            i++;
         }
      }  
   }

   public void keyReleased(KeyEvent e) {
      int key = e.getKeyCode();
      if(active){
         int i = 0;
         for(int[] q : SnS.keys){
            int i2 = 0;
            for(int q2 : q){
               if(q2 == key){
                  keyDown[i][i2] = false;
                  if(i2 == 0 || i2 == 1){//release steeringwheel
                     smoothSteering[pl[i].plIndex] = 0;
                  }
               }
               i2++;
            }
            i++;
         }
      }        
   }


public static int maxSmooth = 10;
   public void move(){
      int i = 0;
      for(Player p : pl){
         if(p.alive){
            if(p.canMoveCar()){
               if(keyDown[i][0]){
                  if(smoothSteering[p.plIndex] < 0){
                     smoothSteering[p.plIndex] = 0;
                  }else{
                     smoothSteering[p.plIndex]++;
                     smoothSteering[p.plIndex] = Math.min(smoothSteering[p.plIndex],maxSmooth);
                  }
                  p.angle += p.getMoveAngle(Math.abs(smoothSteering[p.plIndex]));
               }
               if(keyDown[i][1]){
                  if(smoothSteering[p.plIndex] > 0){
                     smoothSteering[p.plIndex] = 0;
                  }else{
                     smoothSteering[p.plIndex]--;
                     smoothSteering[p.plIndex] = Math.max(smoothSteering[p.plIndex],-1*maxSmooth);
                  }
                  p.angle -= p.getMoveAngle(Math.abs(smoothSteering[p.plIndex]));            
               }
               if(keyDown[i][2]){
                  p.changeSpeed(-1);
               }
               if(keyDown[i][3]){
                  p.changeSpeed(+1);            
               }              
            }
            if(keyDown[i][4]){
               p.tryShot(p.angle);
            }
            if(keyDown[i][5]){
               p.tryBackShot();
            }
            p.angle = p.angle%360;
            move(p);
            i++;
            p.update();
         }
      }
      checkMapForCollisions();
      checkMapForWallCrash();
      resetCrashedPlayer();
   }


If you don't understand any of the code, please ask.
The smoothSteering is just so that the player has better
control of small angles.
int[][] keys contains the KeyEvent_VK... keycodes where the first dimension is for the 4 players
and the second dimension is for the 6 keys: up, down, left, right, shot, backshot.
boolean[][] keyDown is the same for the dimensions and just stores if a key is held down or not.

I played the red baron (http://marcin-kochanowski.com/RedBaron4K.html)
which was programmed for the java4k contest in 2009
with a friend for hours and it works well with many keys pressed at the same
time. (Up to 9 keys, when 3 people are playing)
Unfortunately the programmer didn't release the code, so
I couldn't learn from it...
If you read this: please help me  Wink

Offline Pyrodragoon

Junior Devvie




Art calculated with java...


« Reply #1 - Posted 2010-02-14 18:02:14 »

Doesn't anyone know
why java cannot handle multiple keys pressed at the same time ?
Or is my approach wrong ?

As it looks, there seem to be some keyReleased and keyPressed events
which aren't processed...
Otherwise the boolean values would't remain in their state.


I just read something about the problem with multiple keys
pressed at the same time.
I have WinXP and my keyboard was connected with the pc by PS2.
I read that USB doesn't have this problem (with 3 or more keys pressed
also a noisy beep appears).
So I changed the connection from PS2 to USB...
I hope it helps, couldn't try so far...
Offline Pyrodragoon

Junior Devvie




Art calculated with java...


« Reply #2 - Posted 2010-02-14 19:12:43 »

Well,
I made some tests
and here's the result:
When too many keys are held down,
some of them aren't recognized by java.
Also it seems to depend a little on which keys are pressed.

For example the first player
steers to the right while accelerating and
firing some bullets and the second player
steers right but can't fire,
because pressing the fire-key doesn't have any effect.

(I had WASD for player one and the cursorkeys for player two for moving
and B / NUM_0 for firing with player one / two)

 Cry

I hate this, the game would be lots of fun
but instead it's straining my nerves even before playing.



Would it work with slick ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2010-02-14 19:45:18 »

This isn't a problem with java, it's a limitation of the keyboard hardware.
Offline Pyrodragoon

Junior Devvie




Art calculated with java...


« Reply #4 - Posted 2010-02-14 20:25:16 »

Awesome !!!
Your link was really helpful, I understand the actual problem now.
Now it should be possible to change my keyboardsettings
in order to fit every players movement.
I will post it here, when the game works with 2 or maybe 3 players.
Maybe anyone else with a similar game can use
the layout I choose for the movement.

Thank you a lot  Smiley

P.S.
You may think: "Who in the world is playing a game with two people on one keyboard nowadays?"
Actually it is quite fun to play such games in a little tournament with a friend once in a while.
Red Baron in J4k is one of our favorite games in the tournament,
it is written for one keyboard as well  Wink
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2010-02-14 22:41:59 »

Just do this:

in keyPressed:
Add the keycode to an ArrayList.

in keyReleased:
Remove that keycode from the ArrayList.

Every game loop update:
Perform actions based on all the keycodes in the ArrayList.

See my work:
OTC Software
Offline Gudradain
« Reply #6 - Posted 2010-02-14 23:20:38 »

Open a text entry window of some kind.
Press down (and hold down) the 'E' key.
Without letting go, press down (and hold down) the 'C' key.
Now tap repeatedly on the 'U' key. Do you see any u's coming out on the screen? No? I didn't think so.


Completely unable to reproduce this problem... It's strange

Oh oups, on my keyboard it bug at 7 keys. So no more than 6 key down at a time for me Sad
And on my laptop F, G, and H produce a problem but not on my desktop. So I guess that all keyboard are not the same...
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2010-02-15 00:45:34 »

Awesome !!!
Your link was really helpful, I understand the actual problem now.
Now it should be possible to change my keyboardsettings
in order to fit every players movement.
You may be able to work around it for your keyboard, but you can be sure other keyboards will behave differently. There are even specialized keyboards that do not have this problem, Google "steelkeys" or "keyboard ghosting". I believe all keyboards will support two keys at once, after that sometimes it will work, sometimes it won't. Of course, the ctrl, alt, and shift modifier keys are separate from this problem.

Offline Pyrodragoon

Junior Devvie




Art calculated with java...


« Reply #8 - Posted 2010-02-16 11:06:26 »

It's working now.
I managed to choose the right keys.
That's also possible because a player won't need
to press the steer-left-key and the steer-right-key at the same time.
On other keyboards it may fail again, but
I think most keyboards are the same concerning at least the letters (I don't use 'Z' or 'Y').
When the problem appears on another keyboard one can
change the keysettings ingame (This isn't the nicest way but it works).

If anyone wants to know:
I chose  W,A,S,D for moving the first player and NUM_5,NUM_1,NUM_2,NUM_3 for the second one.
F and G are for firing bullets with the first player and for the second one it's NUM_+ and NUM_Enter.
I'm trying to find the right keys for the 3rd player now, but the game will
mostly be played by just two players.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (20 views)
2014-12-23 03:34:11

rwatson462 (52 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (81 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (93 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (150 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!