I need help to keep score

Hi, I just need help adding the score to my game. Its doing random things. I need the score to be added separately when the ball touches each individual paddle.
heres my code;

//Screem
int screenX = 600;
int screenY = 450;

//Paddle 1
int pad1_sizeX = 10;
int pad1_sizeY = 50;
int pad1X = screenX-30;
int pad1Y = screenY/2-25;

//Paddle General Settings
color pad_color = #FFFFFF;
int pad_vel = 5;

//Paddle 2
int pad2_sizeX = 10;
int pad2_sizeY = 50;
int pad2X = screenX-580;
int pad2Y = screenY/2-25;
color pad2_color = #FFFFFF;
//score
int score1=0;
int score2=0;
//Pellets
float ball_posX = screenX/2;
float ball_posY = screenY/2;
float ball_velX = 3;
float ball_velY = 2;
int ball_size = 20;
color ball_color = #FFFFFF;

//key configuration
boolean w_pressed = false;
boolean s_pressed = false;
boolean space_pressed = false;
boolean up_pressed = false;
boolean down_pressed = false;
boolean lose = false;

color text_color = #FFFFFF;
PFont ds_digital;

//pellet configuration
void setup() {
  size(600, 450);
}

void draw() {
  //KeyStroke
  if (w_pressed == true) {
    pad2Y -= pad_vel;
  }
  if (s_pressed == true) {
    pad2Y += pad_vel;
  }
  if (up_pressed == true) {
    pad1Y -= pad_vel;
  }
  if (down_pressed == true) {
    pad1Y += pad_vel;
  }
  if (pad1Y < 0) {
    pad1Y = 0;
  }
  if (pad1Y > screenY - pad2_sizeY) {
    pad1Y = screenY - pad2_sizeY;
  }
  if (pad2Y < 0) {
    pad2Y = 0;
  }
  if (pad2Y > screenY - pad2_sizeY) {
    pad2Y = screenY - pad2_sizeY;
  }
  //scoring
  if (ball_posX>=pad1X&&ball_posY>=pad1Y) {
    score1=score1+10;
    score2=score2+10;
  }

  //Ball Push
  if (key == ENTER ) {
    lose = false;
  }

  //pellet reset
  if (lose == false) {
    ball_posX = ball_posX+ball_velX;
    ball_posY = ball_posY+ball_velY;
  }

  //Bounce Paddle Right
  if ((ball_posX > pad1X - ball_size/2) && (ball_posX < pad1X + pad1_sizeX + ball_size/2) && (ball_posY > pad1Y - ball_size/2) && (ball_posY < pad1Y + pad1_sizeY + ball_size/2)) {
    if (( ball_velX <= 6)&&(ball_velX >= -6)) {
      ball_velX = -(ball_velX + ball_velX * 0.1);
    } else {
      ball_velX = -ball_velX;
    }
  }

  //Bounce paddle left
  if ((ball_posX < pad2X + pad2_sizeX + ball_size/2) && (ball_posX > pad2X - ball_size/2) && (ball_posY > pad2Y - ball_size/2) && (ball_posY < pad2Y + pad2_sizeY + ball_size/2)) {
    if (( ball_velX <= 6)&&(ball_velX >= -6)) {
      ball_velX = -(ball_velX + ball_velX * 0.1);
    } else {
      ball_velX = -ball_velX;
    }
  }

  //Bounce with floor and ceiling
  if ((ball_posY > 440)||(ball_posY < 10)) {
    ball_velY = -ball_velY;
  }

  //Point marking limits
  if ((ball_posX < 0)||(ball_posX > screenX-ball_size/2)) {
    ball_posX = screenX/2;
    ball_posY = screenY/2;
    ball_velX = 3;
    ball_velY = 2;
    lose = true;
  }


  //Visual
  background(0);
  fill(pad_color);
  noStroke();
  rect(pad1X, pad1Y, pad1_sizeX, pad1_sizeY);
  fill(pad2_color);
  rect(pad2X, pad2Y, pad2_sizeX, pad2_sizeY);
  fill(ball_color);
  ellipse(ball_posX, ball_posY, ball_size, ball_size);
  fill(255, 255, 0);
  textSize(20);
  text(score1, 230, 40);
  text(score2, 550, 40);
  textSize(20);
  text("Score Left Player:", 50, 40);
  text("Score Right Player:", 350, 40);
  //Use this if oyu know  how to make working counter
  //textSize(60);
  //fill(text_color);
  //text("0:0",255,100);
}

//Key Settings
void keyPressed() {
  if (key == 'w') {
    w_pressed = true;
  }
  if (key == 's') {
    s_pressed = true;
  }
  if (keyCode == UP) {
    up_pressed = true;
  }
  if (keyCode == DOWN) {
    down_pressed = true;
  }
}

//Key settings
void keyReleased() {
  if (key == 'w') {
    w_pressed = false;
  }
  if (key == 's') {
    s_pressed = false;
  }
  if (keyCode == UP) {
    up_pressed = false;
  }
  if (keyCode == DOWN) {
    down_pressed = false;
  }
}

Hi,

Welcome to the forum! :slight_smile:

Can you please format your code by first pressing Ctrl+T in the Processing IDE (auto format) and then use the </> button in the message editor to insert your code? Because for now it’s unreadable and it’s difficult to copy it :wink:

Hi @mrporcessing,

as @josephh said, please take time to format your code when you post it ; it is more readable for people who will help you.

Concerning your issue, you have to check if the ball position in X axe exceeds the position of one of your pad :

    score1 += 10;
  }
  if (ball_posX >= pad1X){ // for right pad
    score2 += 10;
  }

But it seems that doing this, you won’t add only 10 points by winning once, but 70 because, loop goes 7 times before restart.
Your actual conditon restart the game when the middle of the ball exceeds the position of the right pad, whereas the condition of giving point is validated when the corner of the ball exceeds, so the draws loops many times before the middle of ball exceeds it also.
So you can change a little the condition to validate only once the scoring, according to your condition of restart :

  if (ball_posX <= 1) { // because you restart the game at ball_posX < 0, so add score the frame just before
    score1 += 10;
  }
  if (ball_posX >= width - ball_size - 1) {// because you restart the game at ball_posX > width - ball_size, so add score the frame just before
    score2 += 10;
  }

if (ball_posX < 0)||(ball_posX > screenX - ball_size){
// restart game
}
2 Likes