Bfrank
Bfrank

Reputation: 33

Need help on KeyEvents

simple question. I'm trying to re-create the game Pong. I've got the graphics that I want on the screen with the two paddles moving with KeyEvents. My only problem is that when I release one key the other paddle stops as well (if both are moving at the same time) I know exactly why this is happening but can't figure out the fix. It's happening because when the keyReleased method is called, both paddles are being set to velocity 0. How can I code it differently where I can move/stop both paddles separately with the keyReleased method?

Code:

package pong.main;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PongGame extends JPanel implements ActionListener,KeyListener {

private final int WIDTH = 750;
private final int HEIGHT = 750;
private int DELAY = 20;
private int IMAGE_SIZE = 35;

private ImageIcon paddle_one;
private ImageIcon paddle_two;
private ImageIcon pong_logo;
private ImageIcon pong_ball;
private Timer timer = new Timer(DELAY, this);

private int p1_x = 10;
private int p1_y = 325;

private int p2_x = 710;
private int p2_y = 325;

private int p1_moveX;
private int p1_moveY;

private int p2_moveX;
private int p2_moveY;

public PongGame(){

    timer.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

    paddle_one = new ImageIcon("C:\\Users\\Bryan\\Pictures\\paddle.gif");
    paddle_two = new ImageIcon("C:\\Users\\Bryan\\Pictures\\paddle.gif");
    pong_logo = new ImageIcon("C:\\Users\\Bryan\\Pictures\\pong_logo.gif");
    pong_ball = new ImageIcon("C:\\Users\\Bryan\\Pictures\\pong_ball.gif");

    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setBackground(Color.black);

}

public void paintComponent(Graphics g){

    super.paintComponent(g);

    paddle_one.paintIcon(this, g, p1_x, p1_y);
    paddle_two.paintIcon(this, g, p2_x, p2_y);
    pong_ball.paintIcon(this, g, 350, 350);
    pong_logo.paintIcon(this, g, 220, -100);

}

public void actionPerformed(ActionEvent e){

    repaint();

    p1_x += p1_moveX;
    p1_y += p1_moveY;

    p2_x += p2_moveX;
    p2_y += p2_moveY;
}

public void paddle_one_up(){
    p1_moveX = 0;
    p1_moveY = -5;
}

public void paddle_one_down(){
    p1_moveX = 0;
    p1_moveY = 5;
}

public void paddle_two_up(){
    p2_moveX = 0;
    p2_moveY = -5;
}

public void paddle_two_down(){
    p2_moveX = 0;
    p2_moveY = 5;
}

public void keyPressed(KeyEvent e){
    int code = e.getKeyCode();

    if(code == KeyEvent.VK_W){
        paddle_one_up();

    }
    if(code == KeyEvent.VK_S){
        paddle_one_down();

    }

    if(code == KeyEvent.VK_UP){
        paddle_two_up();

    }
    if(code == KeyEvent.VK_DOWN){
        paddle_two_down();

    }
}

public void keyReleased(KeyEvent e){

    int code = e.getKeyCode();

    if(code != KeyEvent.VK_W){
        p1_moveX = 0;
        p1_moveY = 0;
    }

    if(code != KeyEvent.VK_S){
        p1_moveX = 0;
        p1_moveY = 0;
    }

    if(code != KeyEvent.VK_UP){
        p2_moveX = 0;
        p2_moveY = 0;
    }
    if(code != KeyEvent.VK_DOWN){
        p2_moveX = 0;
        p2_moveY = 0;
    }
public void keyTyped(KeyEvent e){}

}

Upvotes: 0

Views: 44

Answers (2)

aaroncarsonart
aaroncarsonart

Reputation: 1134

Using the structure of your keyPressed() method's conditional logic, you need a similar check in keyReleased() to determine which key was released before you set either paddle's velocity. As it is, you can release ANY key and keyReleased() will assign the p1 and p2's values to zero.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

  1. Look at the difference between your keyPressed method and your keyReleased method.
  2. Do a similar thing in keyReleased -- check the KeyEvent object's key code and act accordingly. The differences between these two methods and your inability to solve this suggests that your keyPressed code is borrowed code. It's OK to borrow code as long as you understand it first. Better to borrow ideas, and then write your own code.
  3. Even better -- use Key Bindings which is a safer and better solution than using KeyListeners.

Upvotes: 1

Related Questions