wood burning stoves 2.0*
The moose likes Game Development and the fly likes Multiplayer Pong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Multiplayer Pong" Watch "Multiplayer Pong" New topic
Author

Multiplayer Pong

W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
I've searched the forums and haven't been able to find an answer to this problem I'm having. I am building a Pong knock off and I want to use A and Z to move paddle 1 and up and down arrows to move paddle 2. Can someone describe to me the method to acquire simultaneous key presses in Java? So far I am using KeyListener's KeyPressed(KeyEvent e) method with a couple of if statements but when I'm holding A to move one paddle up and then hit the up arrow to move paddle 2, paddle 1 stops moving. Thanks for any help.


SCJP 6, SCWCD 5
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
W Pearce wrote:I've searched the forums and haven't been able to find an answer to this problem I'm having. I am building a Pong knock off and I want to use A and Z to move paddle 1 and up and down arrows to move paddle 2. Can someone describe to me the method to acquire simultaneous key presses in Java? So far I am using KeyListener's KeyPressed(KeyEvent e) method with a couple of if statements but when I'm holding A to move one paddle up and then hit the up arrow to move paddle 2, paddle 1 stops moving. Thanks for any help.


When you press a key, does it set a variable, perhaps an instance variable of a paddle object, that tells the paddle how to move?

presumably keyReleased(KeyEvent e) sets a variable which tells the paddle to stop moving?

Are you using some kind of threaded animation loop? How does it work?

I don't have an answer for you, just feeling my way, but I find the question interesting. These are the sorts of things I would think about if it was my problem.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
Yes, I am using a threaded animation loop in a class called GamePanel that extends JComponent and implements KeyListener.
The code for keyPressed(KeyEvent e) is listed here:



When you press one of the move buttons, it calls that Paddle's move() method and tells it to move 3 points up (negative) or down. 3 is arbitrary, I just picked it because it seems to work reasonably well. There is nothing in keyTyped and keyReleased methods. Let me know if you need more information. Thanks.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
One thing I have noticed is that you are not setting an instance variable with your key pressed method. you are sending a constant to the move method of a player object.

what about the following...

in your keypressed method, instead of passing a value to a method, just set an instance variable of the player to this same value. when the key is released, let keyReleased() clear the value of the instance variable, or set it to 0, or whatever.

the move() method of the player will use the instance variable instead of accepting a value from keyPressed()

the move method of the player is called not from keyPressed(), but from the animation loop.

I'm not 100% convinced this is your answer, but I have a feeling it will solve your problems.

Is it clear to you what I am suggesting? let us know how it goes.

W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
Mr Hamilton,
That worked like a charm. Thank you very much for your help. Both paddles move flawlessly now.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Cool, just one observation, You mentioned using a three point increment because it seems to work well. Another thing you can play with is the sleep method in your animation loop. e.g.

try {
Thread.sleep(n);
} catch (InterruptedException e) {
break;
}

where n is a variable of type 'long' by lowering the value of n, you increase the frequency of your animation loop, thus speeding up your program.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
I wouldn't recommend manually changing the value of the sleep to get the required game speed. In a simple app like a pong clone it will probably work fine but with more complex logic it can give erratic frame rates.

In Killer Game Programming in Java, the author uses a framework where you configure your desired framerate. You then time the logic/draw operations in the game and then sleep for the amount of time required to achieve the desired framerate. Andrew Davison made some of his early draft chapters available here though its still worth getting the full book.

From what I can remember the basic game framework stuff is covered in chapters 1-4.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Indeed Paul, my remark about sleep() was a little careless. There is more to it than I have stated.

I haven't read your suggestions, but one article that deals well with this exact topic is the following.

http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation.html

Some of the information about threads is outdated, but the discussion of frame rates and the like seems pretty good.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
The way Paul mentioned doing the animation loop is more of how I have it set up, with a timer before and after the sleep to gauge whether or not additional state updates should be performed without redrawing the frame, but I appreciate the link you provided, Fred, as I've been looking for some good information to go into animated gifs / starting work with sprites. Thanks to both of you for your input.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
One thing to keep in mind, the article, being somewhat dated deals with Applet class, not Swing and JApplet. Nonetheless it is very well written, and much of the info applies to swing also. It is my understanding that the information about off screen graphics applies to Applet, it is not necessary to do off screen drawing in Swing. Perhaps someone else here can confirm that.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
While JPanels are double buffered, I still use a method of active rendering to do all my drawing mainly for the sole reason that calls to paintComponent(Graphics g) or redraw() (can't remember which) are requests only, meaning that some frames may be skipped unintentionally and thus produce undesirable effects. My animation loop calls a stateUpdate() method, that updates the game components, usually by calling their respective move() methods, a stateRender() method that draws the image to an off screen buffer, and finally a paintScreen() method that actually gets a graphics context to the panel (I actually used a JComponent) and paints the full image from the buffer before sleeping for a specified amount of time.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Interesting, I'll keep it in mind. I know I have had some problems with "jumpy" motion in some of my Swing animations, so maybe this applies. Anyways, it sounds like you've been doing your homework. The thread started out you learning from me, now it's the other way around.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
The thread started out you learning from me, now it's the other way around.


Now let's just hope I can take it all and apply it to something a little more complex than Pong!

Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiplayer Pong
 
Similar Threads
Beginner question - Key events
The game "Breakout", stuck with collision checking
Java Intersects
Pong problems
trouble with mouseEvents