Move two shapes at a time with the keyboard

Hi

I’m trying to write a little Pong Game with a multi and a singleplayer. Almost everything works, I just can’t get the control of the two paddles in multiplayer right. I honestly don’t know what’s the problem, but as soon as I try to move one, the other one stops right away. The whole project with all classes is here:

I hope you can open and view it, I’m not that good with Dropbox :slight_smile:
Code style advices are welcome, too. :wink:

Thank you very much for your help
Ear1yT

2 Likes

Are you doing it on android or phone, and or libraries. I couldn’t open it with Dropbox.

I do it on my computer. It’s a MacBook Pro.
Maybe this link works:

If that does not work, I will copy-paste the code right in here, so you can read it without the Dropbox link. It’s just a bit long, that was why tried to share it with Dropbox originally.

I am still unable to access the code try copy pasting it on here. Are you using libraries?

Well, then. Here is the code:

//variable for the gamemode switch: 0 for menu, 1 for singleplayer, 2 for multiplayer
int gamemode = 0;

//repetition of the width and height variables to be able to use them in constructors
final int frameWidth = 1000;
final int frameHeight = 750;

void setup() {
  size(1000, 750);
  surface.setTitle("Pong");
}

PongGame pong = new PongGame();

void draw() {
  background(#0B0C10);
  switch(gamemode) {
  case 0:
    pong.displayMenu();
    break;
  case 1:
    pong.singleplayer();
    break;
  case 2:
    pong.multiplayer();
    break;
  }
}

void mousePressed() {
  if (mouseX < frameWidth/2) {
    gamemode = 1;
  } else if (mouseX > frameWidth/2) {
    gamemode = 2;
  }
}

void keyPressed() {
  if (keyCode == RETURN || keyCode == ENTER) {
    gamemode = 0;
  }
}
public class Ball {
  private PVector position;
  private PVector velocity; 
  private color colour;
  private int extent;
  private int radius;

  public Ball(PVector initialPosition, PVector initialVelocity, int extent) {
    this.position = initialPosition;
    this.velocity = initialVelocity;
    this.extent = extent;
    this.radius = extent/2;
  }

  public void update() {
    position.add(velocity);
    fill(colour);
    noStroke();
    circle(position.x, position.y, extent);
  }

  public void bounceOff(float xBorderL, float xBorderR, float yBorderU, float yBorderD) {
    if (position.x < (xBorderR + radius) && position.x > (xBorderL - radius) && position.y < (yBorderD + radius) && position.y > (yBorderU - radius)) {
        velocity.x *= -1;
    }
    if (position.x < (xBorderR + radius) && position.x > (xBorderL - radius) && (position.y == yBorderD + radius || position.y == yBorderU - radius)) {
      velocity.y *= -1;
    }
  }

  public void bounce(int xMin, int xMax, int yMin, int yMax) {
    if ((position.x < (xMin + radius) && velocity.x < 0) || (position.x > (xMax - radius) && velocity.x > 0)) {
      velocity.x *= -1;
    }
    if ((position.y < (yMin + radius) && velocity.y < 0) || (position.y > (yMax - radius) && velocity.y > 0)) {
      velocity.y *= -1;
    }
  } 
  
  public PVector getVelocity() {
    return velocity;
  }
  
  public void setVelocity(PVector velocity) {
    this.velocity = velocity;
  }

  public void setColor(color colour) {
    this.colour = colour;
  }
  
  public PVector getPosition() {
    return position;
  }
  
  public void setPosition(PVector position) {
    this.position = position;
  }
  
  public int getRadius() {
    return radius;
  }
}
public class Paddle {
  private PVector position;
  private color colour;
  private float[] size = new float[2];
  
  public Paddle(PVector position, float[] size) {
    this.size = size;
    this.position = position;
  }
  
  public void update(PVector velocity, char upKey, char downKey) {
    if (keyPressed && (key == upKey || key == Character.toLowerCase(upKey))) {
      position.y += -velocity.y;
    }
    if (keyPressed && (key == downKey || key == Character.toLowerCase(downKey))) {
      position.y += velocity.y;
    }
    fill(colour);
    noStroke();
    rect(position.x, position.y, size[0], size[1]);
  }
  
  public void update(PVector velocity, int upKey,int downKey) {
    if (keyPressed && (keyCode == upKey)) {
      position.y += -velocity.y;
    }
    if (keyPressed && (keyCode == downKey)) {
      position.y += velocity.y;
    }
    fill(colour);
    noStroke();
    rect(position.x, position.y, size[0], size[1]);
  }
  
  public void setColor(color colour) {
    this.colour = colour;
  }
  
  public float[] getSize() {
    return size;
  }
  
  public PVector getPosition() {
    return position;
  }
}
public class PongGame {
  private Ball ball = new Ball(new PVector(frameWidth/2, frameHeight/2), new PVector(3, 4), 25);
  private Paddle paddle1 = new Paddle(new PVector(50, frameHeight/2), new float[]{20, 100});
  private Paddle paddle2 = new Paddle(new PVector(frameWidth-50, frameHeight/2), new float[]{20, 100});
  public Scoreboard scoreboardLeft = new Scoreboard(new PVector(50, 50), #C5C6C7);
  public Scoreboard scoreboardRight = new Scoreboard(new PVector(frameWidth-50, 50), #C5C6C7);

  public void singleplayer() {
    scoreboardLeft.display();
    ball.setColor(#66FCF1);
    paddle1.setColor(#45A29E);
    paddle1.update(new PVector(0, 3), 'W', 'S');
    ball.bounceOff(paddle1.getPosition().x, paddle1.getPosition().x + paddle1.getSize()[0], paddle1.getPosition().y, paddle1.getPosition().y + paddle1.getSize()[1]);
    ball.bounce(-25, frameWidth, 0, frameHeight);
    if (ball.getPosition().x <= 0) {
      ball.setPosition(new PVector(frameWidth/2, frameHeight/2));
      ball.setVelocity(new PVector(ball.getVelocity().x * -1, ball.getVelocity().y));
      scoreboardLeft.points -= 1;
    }
    ball.update();
  }

  public void multiplayer() {
    scoreboardLeft.display();
    scoreboardRight.display();
    ball.setColor(#66FCF1);
    paddle1.setColor(#45A29E);
    paddle1.update(new PVector(0, 3), 'W', 'S');
    paddle2.setColor(#45A29E);
    paddle2.update(new PVector(0, 3), UP, DOWN);
    ball.bounceOff(paddle1.getPosition().x, paddle1.getPosition().x + paddle1.getSize()[0], paddle1.getPosition().y, paddle1.getPosition().y + paddle1.getSize()[1]);
    ball.bounceOff(paddle2.getPosition().x, paddle2.getPosition().x + paddle2.getSize()[0], paddle2.getPosition().y, paddle2.getPosition().y + paddle2.getSize()[1]);
    ball.bounce(-25, frameWidth+25, 0, frameHeight);
    if (ball.getPosition().x <= 0 || ball.getPosition().x >= frameWidth) {
      ball.setPosition(new PVector(frameWidth/2, frameHeight/2));
      ball.setVelocity(new PVector(ball.getVelocity().x * -1, ball.getVelocity().y));
      if (ball.getPosition().x <= 0) {
        scoreboardLeft.points -= 1;
      } else if (ball.getPosition().x >= frameWidth) {
        scoreboardRight.points -= 1;
      }
    }
    ball.update();
  }

  public void displayMenu() {
    fill(255);
    stroke(255);
    line(width/2, height/3, width/2, height);
    textAlign(CENTER, CENTER);
    textSize(50);
    text("PONG", width/2, height/6);
    textSize(20);
    text("Start Singleplayer", width/4, height/1.6);
    text("Start Multiplayer", width/1.3, height/1.6);
  }
}
public class Scoreboard {
  public int points = 0;
  private PVector position;
  private color colour;
  
  public Scoreboard(PVector position, color colour) {
    this.position = position;
    this.colour = colour;
  }
  
  public void display() {
    textAlign(TOP, LEFT);
    fill(colour);
    text("Points:", position.x, position.y);
    text(points, position.x+75, position.y);
  }
}

And, as I’ve mentioned before, style improvement suggestions are always welcome, too.

Thanks again
Ear1yT

I think you should make two if statements (one of each paddle) that say, that when the mouse is pressed on of the paddles, it will lock on the mouse’s y-axis and move according to it. And when it is clicked again it will unlock.
I have been experimenting with that for some time, by using simpler programs, maybe you have some luck with it.

You could also do something like this:

boolean[] KEYS; 
public void setup() {
  size(500,500);
  fill(0);
  textSize(20);
  KEYS = new boolean[255];
}

public void draw() {
  background(255);
  text ("Left:" + KEYS[65] + "   Right:" + KEYS[68] + "   Up:" + KEYS[87] + "   Down:" + KEYS[83], 0 ,(height>>1)-20);
  text ("Left + Up:" + (KEYS[LEFT] && KEYS[UP]) , 0 ,(height>>1)+20);
}

public void keyPressed() {
  if (keyCode < 255) {
    KEYS[keyCode] = true;
    println(keyCode);
  }
}

public void keyReleased() {
  if (keyCode < 255) {
    KEYS[keyCode] = false;
  }
}

Just look inside the KEYS var at the ascii value of the key you want to see if its pressed