Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (498)
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  
  2 players from one keyboard  (Read 4378 times)
0 Members and 1 Guest are viewing this topic.
Offline Kakashi266

Junior Member





« Posted 2007-12-11 20:54:53 »

Hi, everyone. I'm searching for a way to have two players being able to control two separate characters from the same keyboard (awsd vs. left/up/right/down). I have searched google several times, and the only site that I found offered no useful information. I've also searched this forum, but couldn't find anything helpful at all. On a forum, somebody said threads were the key, but did not elaborate. I would be thankful if someone can point me in the right direction.

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2007-12-12 00:08:59 »

You don't need any extra threads. What you really need are configurable controls. How many keys and which keys can be pressed together is a hardware thing. Very old keyboards only allowed up to 2 or 3 key presses at the same time. Newer ones support more. However, some combinations simply won't work because the "lanes" of the key matrix are re-used and therefore it's unable to detect specific combinations.

Eg famous Cherry keyboard no go combos are:
up+left+space¹
left+up+right
a+s+x
a+s+z (or a+s+y with a qwertz layout)
a+s+w
a+s+q

[¹ That's the reason why you shouldn't use cursors+space. If you use space also allow ctrl.]

弾幕 ☆ @mahonnaiseblog
Offline Kakashi266

Junior Member





« Reply #2 - Posted 2007-12-12 01:43:59 »

Alright, that's interesting to know, thanks. I have more or less given up on a two player game, in favor of an AI opponent. I think I should also mention that I'm making this game as an application, not an applet. I think there are different ways that the two work, because I have seen applets where more than one key can be pressed at the same time (like player one is pressing Left while Two is pressing D, and both characters move correctly).

Specifically, I wanted a layout like A and D to move left and right. W is jump, and S is duck. Ctrl is punch, and Shift is kick. There's a couple things I'm not sure of.

1.) When a directional key is pressed, there is a half second gap before smooth movement.
2.) If I'm moving right, and I press the button to jump, the character will land and stop moving, even if the right key is still being held.

That's all I can think of. I'm not a computer science student (education major), so I can only program in my spare time. My skills atrophy a lot between major attempts at programming. I'm really thankful for this forum, and anyone who takes the time to answer and help.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #3 - Posted 2007-12-12 02:02:14 »

If you're going to write a game with two or more players on one keyboard, you need to read this. Basically, keyboards suck and as oNyx said, certain combinations will lock up and cause weird behaviour.

Other than that, all you really need to do is attach a keyboard listener to your canvas/frame and have it log key press and release states. That'll all happen in your AWT event thread. Then just read the states in your main thread with your game loop. A traditional n00b error is trying to trigger movement off of the keyboard events (like key repeat) which is generally a bad idea because it causes that odd half-second pause before moving and ties your movement to your OS's key repeat setting. For example code have a search around the forum.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2007-12-12 02:03:24 »

>I think there are different ways that the two work[...]

Nop, it's pretty much the same thing.

Basically it's done like this... you get the event, you change the value of some flag... then in your main loop you act accordingly to the value of that flag. It's done like that everywhere.

>Warning - while you were typing a new reply has been posted. You may wish to review your post.

bla... /me submits anyways Tongue

弾幕 ☆ @mahonnaiseblog
Offline Kakashi266

Junior Member





« Reply #5 - Posted 2007-12-12 02:14:23 »

Other than that, all you really need to do is attach a keyboard listener to your canvas/frame and have it log key press and release states. That'll all happen in your AWT event thread. Then just read the states in your main thread with your game loop. A traditional n00b error is trying to trigger movement off of the keyboard events (like key repeat) which is generally a bad idea because it causes that odd half-second pause before moving and ties your movement to your OS's key repeat setting. For example code have a search around the forum.

Ooh, could you expand on this a little bit? I'm working primarily off of a high school comp sci class's knowledge, which is not much at all. Trying to learn off of code by my self is literally like learning a new language. I need someone to explain a couple things. I would be thankful if you could provide at least a small code example.

EDIT- Oh, and I'm not slacking or anything. Right now I'm looking through two or three different forums looking for exactly what I need.

Offline Kakashi266

Junior Member





« Reply #6 - Posted 2007-12-12 03:04:47 »

I'm gonna go ahead and double post this, so that no one overlooks a small edit and wastes their time.

I have actually figured it out, thanks to someone's code example, and a lot of squinting at the screen on my part.

I would like to thank the people who took their time to help me, you guys are great.

Sticko
Guest
« Reply #7 - Posted 2007-12-14 16:08:09 »

You don't need any extra threads. What you really need are configurable controls. How many keys and which keys can be pressed together is a hardware thing. Very old keyboards only allowed up to 2 or 3 key presses at the same time. Newer ones support more. However, some combinations simply won't work because the "lanes" of the key matrix are re-used and therefore it's unable to detect specific combinations.

Eg famous Cherry keyboard no go combos are:
up+left+space¹
left+up+right
a+s+x
a+s+z (or a+s+y with a qwertz layout)
a+s+w
a+s+q

[¹ That's the reason why you shouldn't use cursors+space. If you use space also allow ctrl.]

Oh, man!!!  I'd been pulling my hair out thinking I was doing something wrong with up+left+space.  THANKYOU!!!!!   Smiley Smiley
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.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (22 views)
2014-04-15 03:46:01

UprightPath (38 views)
2014-04-14 17:39:50

UprightPath (20 views)
2014-04-14 17:35:47

Porlus (36 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!