Pong multiple keys

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:


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


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

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);
  }
}
1 Like

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

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

1 Like