0

Me and my partner are attempting to create the game Pong for our computer science final project. We created a reference code where 2 cubes can be controlled upwards and downwards and it works fine. The problem occurs when attempting to control both cubes at the same time (only 1 cube will move at a time). We want to make both cubes move at the same time.

WE want to say that:

yPos - is the y position of the black cube 
xPos - is the x position of the black cube 
xPos2 - is the x position of the blue cube
YPos2 - is the y position of the blue cube

Keys:

A - Go up for black cube
Z - Go down for black cube
K - Go up for blue cube
M - go down for blue cube

We have tried using a more complicated version which used j-label animation. How ever we want to make our pong game through the graphics function. But we do not understand:

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

public class PongHelpNeed extends JFrame implements KeyListener
{
  // booleans to tell which key is pressed
  boolean upKey;
  boolean downKey;
  boolean upKey2;
  boolean downKey2;

  // the position variables
  int yPos;           
  int xPos; 
  int xPos2;
  int yPos2;

  public PongHelpNeed ()
  {
    //create window
    super ("Controller");
    setSize (660, 700);

    // set keys to false and original positions
    upKey = false;
    downKey = false;

    upKey2 = false;
    downKey2 = false;

    xPos = 100;
    yPos = 350;

    xPos2 = 500;
    yPos2 = 350;

    // add the frame as a listener to your keys
    addKeyListener (this);  
    // Show the frame
    setVisible(true);       
  }

  //needs to be here because the class implements KeyListener
  public void keyTyped (KeyEvent e)       
  {
    System.out.println (e.getKeyCode () + " Typed");
  }

  //needs to be here because the class implements KeyListener
  public void keyPressed (KeyEvent e) {
    //check if keys a,z or k,m are pressed

    if  (e.getKeyCode () == KeyEvent.VK_A) 
    {
      upKey = true;
    }
    else if (e.getKeyCode () == KeyEvent.VK_Z) 
    {
      downKey = true;
    }
    else if (e.getKeyCode () == KeyEvent.VK_K) 
    {
      upKey2 = true;
    }
    else if (e.getKeyCode () == KeyEvent.VK_M) 
    {
      downKey2 = true;
    }
    //repaint the window everytime you press a key
    repaint ();   
  } 

  //needs to be here because the class implements KeyListener
  public void keyReleased (KeyEvent e) 
  {
    System.out.println (e.getKeyCode () + " Released");
  }

  //paints the pictures 
  public void paint (Graphics g)   
  {
    //set background
    g.setColor(Color.WHITE);  
    g.fillRect(0, 0, 660, 700);

    //cube 1
    g.setColor(Color.BLACK);      
    g.fillRect(xPos,yPos,50, 50);

    //draw cube 2
    g.setColor(Color.BLUE);    
    g.fillRect(xPos2,yPos2, 50, 50);

    //if keys are pressed move the cubes accordingly up or down
    if (upKey == true)
    {
      yPos = yPos - 15;
      upKey = false;
    }

    else if (downKey == true)
    {
      yPos = yPos + 15;
      downKey = false;
    }
    else if (downKey2 == true){
      yPos2 = yPos2 + 15;
      downKey2 = false;
    }
    else if (upKey2 == true)  {
      yPos2 = yPos2 - 15;
      upKey2 = false;
    }   
  }

  public static void main (String[] args) 
  {
    new PongHelpNeed (); 
  }
} 

Our expected results are we are trying to move both cube at the same time. So when we press the A key and the K key the black square should move and the blue cube should move.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vish
  • 29
  • 4
  • have a look at this thread: https://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time – MD Ruhul Amin Jun 12 '19 at 02:37

1 Answers1

0

Calling repaint() does not trigger a call to the paint immediately, so it's possible that the keyPressed is triggered twice (or more) before paint.

In your paint method you are checking the keys in if-else, which means that if one of the flags is true, the rest are not checked. You also have a race condition where the keyPressed is fighting with paint over the flags. Also, if you press a key quickly multiple times, you'll lose all the extra key presses between the first handled event and the next repaint.

Instead of doing the move within paint, you should do it within the keyPressed handler. Don't set a flag to e.g. upKey = true;, but instead do the action directly: yPos = yPos - 15;. The paint method will then just refresh the view to reflect the current (updated) state.

vlumi
  • 1,321
  • 1
  • 9
  • 18