# Pong multiple keys

**URL:** https://discourse.processing.org/t/pong-multiple-keys/19915
**Category:** Libraries
**Created:** [April 19, 2020, 12:22am UTC](https://discourse.processing.org/t/pong-multiple-keys/19915 "2020-04-19T00:22:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![alphakay](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@alphakay](https://discourse.processing.org/u/alphakay)
#### Post date: [April 19, 2020, 12:22am UTC](https://discourse.processing.org/t/pong-multiple-keys/19915/1 "2020-04-19T00:22:25Z")

</div>

Hey guys, im programming a pong game and I want to use multiples keys. I want for the left paddle ‘‘w’’ and ‘’ s’’ to move and for the right side up and down. it is based on coding trains pong game but i want to make it a little better for me. i cant move the right and the left paddle at the same time.

here’s the code:

```auto

`import processing.sound.*;

//based on coding Train pong;

Puck puck;

SoundFile ding;
SoundFile pingpong;

Paddle left;
Paddle right;

int leftscore = 0;
int rightscore = 0;

void setup() {
  fullScreen();
  ding = new SoundFile(this, "ding.mp3");
  pingpong = new SoundFile(this, "pingpong.mp3");
  puck = new Puck();
  left = new Paddle(true);
  right = new Paddle(false);
}

void draw() {
  background(0);

  //puck.checkPaddle(left);
  puck.checkPaddleRight(right);
  puck.checkPaddleLeft(left);

  left.show();
  right.show();
  left.update();
  right.update();

  puck.update();
  puck.edges();
  puck.show();

  fill(255);
  textSize(32);
  text(leftscore, 32, 40);
  text(rightscore, width-64, 40);
}

void keyReleased() {
  left.move(0);
  right.move(0);
}

void keyPressed() {
  if (key == 'w') {
    left.move(-5);
  } else if (key == 's') {
    left.move (5);
  }
  if (keyCode == UP) {
    right.move(-5);
  } else if (keyCode == DOWN) {
    right.move (5);
  }
}

```

```auto

class Paddle {
  float x;
  float y = height/2;
  float w = 20;
  float h = 100;
  
  float ychange = 0;
  
  Paddle(boolean left) {
    if (left) {
      x = w;
    } else {
      x = width - w;
    }
  }
  
  void update() {
   y += ychange;
   y = constrain(y, h/2, height-h/2);
  }
  
  void move(float steps) {
    ychange = steps;
      
  }
  
  void show() {
    
    rectMode(CENTER);
    rect(x, y, w, h);
  }
}

```

```auto

class Puck {
  float x = width/5;
  float y =height/5;
  float xspeed;
  float yspeed;
  float r = 12;

  Puck() {
    reset();
  }

  void checkPaddleLeft(Paddle p) {
    if (y < p.y + p.h/2 && y > p.y - p.h/2 && x - r < p.x + p.w/2) {
      if (x > p.x) {
        float diff = y - (p.y - p.h/2);
        float rad = radians(45);
        float angle = map(diff, 0, p.h, -rad, rad);        
        xspeed = 5 * cos(angle);
        yspeed = 5 * sin(angle);
        x = p.x + p.w/2 + r;

        //xspeed *= -1;
        pingpong.play();
      }
    }
  } 
  void checkPaddleRight(Paddle p) {
    if (y < p.y + p.h/2 && y > p.y - p.h/2 && x + r > p.x - p.w/2) {
      if (x < p.x) {
        float diff = y - (p.y - p.h/2);
        float rad = radians(135);
        float angle = map(diff, 0, p.h, -rad, rad);
        xspeed = 5 * cos(angle);
        yspeed = 5 * sin(angle);
        x = p.x - p.w/2 - r;

        //xspeed *= -1;
        pingpong.play();
      }
    }
  } 

  void update() {
    x = x + xspeed;
    y = y + yspeed;
  }

  void reset() {
    x = width/2;
    y = height/2;
    float angle = random(-PI/4, PI/4);
    xspeed = 5 * cos(angle);
    yspeed = 5 * sin(angle);

    if (random(1) < 0.5) {
    }
  }

  void edges() {
    if (y < 0 || y > height) {
      yspeed *= -1;
      pingpong.play();
    }
    if (x - r > width) {
      ding.play();
      leftscore++;
      reset();
    }
    if (x + r < 0) {
      ding.play();
      rightscore++;
      reset();
    }
  }

  void show() {
    fill(255);
    ellipse(x, y, 40, 40);
  }
}

```

---

<div class="post-metadata">

### Author: ![CodingIsLife](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@CodingIsLife](https://discourse.processing.org/u/CodingIsLife)
#### Post date: [April 19, 2020, 4:35pm UTC](https://discourse.processing.org/t/pong-multiple-keys/19915/2 "2020-04-19T16:35:40Z")

</div>

Hey, there! Seems like when you release your keys for one paddle the other also stops. Is that what you want?  
If not, then you should see which key you released with conditionals just like you did in the keypressed function

---

<div class="post-metadata">

### Author: ![alphakay](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@alphakay](https://discourse.processing.org/u/alphakay)
#### Post date: [April 19, 2020, 9:12pm UTC](https://discourse.processing.org/t/pong-multiple-keys/19915/3 "2020-04-19T21:12:51Z")

</div>

no I mean they are connected. If I first move the left one and then I move the right one the left stops.

nvm I could solve the problem.

but thx
