# Move two shapes at a time with the keyboard

**URL:** https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203
**Category:** Coding Questions
**Created:** [April 27, 2020, 11:10am UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203 "2020-04-27T11:10:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ear1yT](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ear1yt/32/8996_2.png) [@Ear1yT](https://discourse.processing.org/u/Ear1yT)
#### Post date: [April 27, 2020, 11:10am UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/1 "2020-04-27T11:10:51Z")

</div>

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:

> **[PongV2.zip](https://www.dropbox.com/s/42rypjg58xef45r/PongV2.zip?dl=0)**
>
> Shared with Dropbox

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

Thank you very much for your help  
Ear1yT

---

<div class="post-metadata">

### Author: ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)
#### Post date: [April 27, 2020, 5:39pm UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/2 "2020-04-27T17:39:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ear1yT](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ear1yt/32/8996_2.png) [@Ear1yT](https://discourse.processing.org/u/Ear1yT)
#### Post date: [April 28, 2020, 9:27am UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/3 "2020-04-28T09:27:32Z")

</div>

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

> **[PongV2.zip](https://www.dropbox.com/s/w007gk7ayoq69t1/PongV2.zip?dl=0)**
>
> Shared with Dropbox

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.

---

<div class="post-metadata">

### Author: ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)
#### Post date: [April 28, 2020, 2:46pm UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/4 "2020-04-28T14:46:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ear1yT](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ear1yt/32/8996_2.png) [@Ear1yT](https://discourse.processing.org/u/Ear1yT)
#### Post date: [April 29, 2020, 3:00pm UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/5 "2020-04-29T15:00:22Z")

</div>

Well, then. Here is the code:

```auto
//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;
  }
}

```

```auto
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;
  }
}

```

```auto
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;
  }
}

```

```auto
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);
  }
}

```

```auto
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

---

<div class="post-metadata">

### Author: ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)
#### Post date: [April 30, 2020, 12:05am UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/6 "2020-04-30T00:05:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![KevinWho](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@KevinWho](https://discourse.processing.org/u/KevinWho)
#### Post date: [May 1, 2020, 1:22am UTC](https://discourse.processing.org/t/move-two-shapes-at-a-time-with-the-keyboard/20203/7 "2020-05-01T01:22:51Z")

</div>

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/22644/two-keys-pressed-three-keys-pressed-simultaneously)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

You could also do something like this:

```auto
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
