-
Pong
Hi, I'm creating a version of Pong in java, but I have a problem. When I hold down the buttons to move the left and right paddles at the same time, only the one that I hit most recently is registered. I was wondering how I could get them to work simultaneously. Thanks for the help.
Here is my code if it helps:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.URL;
public class PongPanel extends JPanel
{
private final int WIDTH = 600, HEIGHT = 400;
private final int DELAY = 20, IMAGE_SIZE = 30;
private final int MOVE = 5;
private final int REC_WIDTH = 10, REC_HEIGHT = 50;
private int LRecX = 10, RRecX = 580;
private int LRecY = 180, RRecY = 180;
private Timer timer;
private ImageIcon image;
private int scoreL = 0, scoreR = 0;
private int x, y;
private double moveX = 3, moveY = 3;
public PongPanel()
{
addKeyListener (new DirectionListener());
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon ("Smiley.gif");
x = 50;
y = 50;
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.black);
timer.start();
setFocusable(true);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
image.paintIcon (this, page, x, y);
page.setColor (Color.white);
page.fillRect (LRecX, LRecY, REC_WIDTH, REC_HEIGHT); // paints paddles
page.fillRect (RRecX, RRecY, REC_WIDTH, REC_HEIGHT);
page.drawString("" + scoreL, 288, 10);
page.drawString("" + scoreR, 310, 10);
}
private class ReboundListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x += moveX; // moves ball
y += moveY;
Rectangle rectL = new Rectangle(LRecX, LRecY, REC_WIDTH, REC_HEIGHT);
Rectangle rectR = new Rectangle(RRecX, RRecY, REC_WIDTH, REC_HEIGHT);
Rectangle imageRect = new Rectangle(x, y, IMAGE_SIZE, IMAGE_SIZE);
if(rectL.intersects(imageRect)) // reverses direction of ball if it hits left paddle
{
if (moveX < 0)
moveX -= .3;
else
moveX += .3;
if (moveY < 0)
moveY -= .3;
else
moveY += .3;
moveX = moveX * -1;
}
if(rectR.intersects(imageRect)) // reverses direction of ball if it hits right paddle
{
if (moveX < 0)
moveX -= .3;
else
moveX += .3;
if (moveY < 0)
moveY -= .3;
else
moveY += .3;
moveX = moveX * -1;
}
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE) // bounces of top and bottom of screen
{
moveY = moveY * -1;
}
if (x < 0) // starts over if right scores
{
scoreR += 1;
x = 50;
y = 50;
moveX = 4;
moveY = 4;
}
if (x > WIDTH-IMAGE_SIZE) // starts over if left scores
{
scoreL += 1;
x = 50;
y = 50;
moveX = 4;
moveY = 4;
}
repaint();
}
}
private class DirectionListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode()) // moves paddles
{
case KeyEvent.VK_UP:
RRecY -= MOVE; // up and down for right paddle
break;
case KeyEvent.VK_DOWN:
RRecY += MOVE;
break;
case KeyEvent.VK_A:
LRecY -= MOVE; // a and z for left paddle
break;
case KeyEvent.VK_Z:
LRecY += MOVE;
break;
}
repaint();
}
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
-
Make a boolean for the movekeys that is set to true for keyPressed and set to false for keyReleased
-
Thanks for the help! It works great now.
Similar Threads
-
By lordanki in forum Java
Replies: 3
Last Post: 04-04-2006, 10:13 AM
-
By Sportsdude11751 in forum Java
Replies: 1
Last Post: 04-17-2005, 04:04 PM
-
By David Lambert in forum Web
Replies: 0
Last Post: 11-15-2000, 05:07 PM
-
By David Lambert in forum Web
Replies: 0
Last Post: 11-15-2000, 05:03 PM
-
By David Lambert in forum Web
Replies: 0
Last Post: 11-15-2000, 05:01 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks