Score won't stay

I’m making a stupid little game for fun. The square follows you, and however long it follows you, you get score. While the ‘play’ is true, you get score. And when it is ‘gameOver’ it displays a rounded amount. Yet when I go to display it, it resets to 0.

float squareX;
float squareY;
float score = 0;
float scoreDis;
float circleX;
float circleY;

float pulX;
float pulY;
float purX;
float purY;
float pdlX;
float pdlY;
float pdrX;
float pdrY;

boolean play;
boolean gameOver;

boolean keyDwn = new boolean[4];

final int _keyR = 39;
final int _keyL = 37;
final int _keyU = 38;
final int _keyD = 40;

void setup() {
size(600,600);
background(0, 255, 0);
surface.setTitle(“run”);
scoreDis = round(score);
squareX = width/2;
squareY = height/2;
play = true;
gameOver = false;
keyDwn[0] = false;
keyDwn[1] = false;
keyDwn[2] = false;
keyDwn[3] = false;
circleX = width/2;
circleY = 10;

pulX = squareX-15;
pulY = squareY-15;
purX = squareX+15;
purY = squareY-15;
pdlX = squareX-15;
pdlY = squareY+15;
pdrX = squareX+15;
pdrY = squareY+15;

}
void draw() {
if (play = true) {
background(0, 255, 0);
rectMode(CENTER);
stroke(0);
strokeWeight(1);
square(squareX, squareY, 30);
stroke(255);
strokeWeight(1);
point(pulX, pulY);
point(purX, purY);
point(pdlX, pdlY);
point(pdrX, pdrY);
score = score+1;
if (keyDwn[0] == true) {
squareY = squareY-5;
}
if (keyDwn[1] == true) {
squareX = squareX+5;
} if (keyDwn[2] == true) {
squareY = squareY+5;
}
if (keyDwn[3] == true) {
squareX = squareX-5;
}

if (squareX >= 585) {
  squareX = 585;
}
if (squareX <= 15) {
  squareX = 15;
}
if (squareY <= 15) {
  squareY = 15;
}
if (squareY >= 585) {
  squareY = 585;
}

strokeWeight(1);
stroke(0);
fill(255, 0, 0);
square(circleX, circleY, 30);

pulX = squareX-15;
pulY = squareY-15;
purX = squareX+15;
purY = squareY-15;
pdlX = squareX-15;
pdlY = squareY+15;
pdrX = squareX+15;
pdrY = squareY+15;

if(circleX > squareX) {
  circleX = circleX-4;
}
if(circleX < squareX) {
  circleX = circleX+4;
}
if(circleY > squareY) {
  circleY = circleY-4;
}
if(circleY < squareY) {
  circleY = circleY+4;
}




if (pulX > circleX-15 && pulX < circleX + 15 && pulY > circleY-15 && pulY < circleY + 15) {
  play = false;
  gameOver = true;
}
if (purX > circleX-15 && purX < circleX + 15 && purY > circleY-15 && purY < circleY + 15) {
  play = false;
  gameOver = true;
}
if (pdlX > circleX-15 && pdlX < circleX + 15 && pdlY > circleY-15 && pdlY < circleY + 15) {
  play = false;
  gameOver = true;
}
if (pdrX > circleX-15 && pdrX < circleX + 15 && pdrY > circleY-15 && pdrY < circleY + 15) {
  play = false;
  gameOver = true;
}

}
if (gameOver == true) {
background(0);
square(squareX, squareY, 30);
text(scoreDis, width/2, height/2);

}

}

void keyPressed() {
if (keyCode == _keyU) {
keyDwn[0] = true;
}
if (keyCode == _keyR) {
keyDwn[1] = true;
}
if (keyCode == _keyD) {
keyDwn[2] = true;
}
if (keyCode == _keyL) {
keyDwn[3] = true;
}
}
void keyReleased() {
if (keyCode == _keyU) {
keyDwn[0] = false;
}
if (keyCode == _keyR) {
keyDwn[1] = false;
}
if (keyCode == _keyD) {
keyDwn[2] = false;
}
if (keyCode == _keyL) {
keyDwn[3] = false;
}
}

This is not correct

Should be ==

Two ==

1 Like
  • Actually we should drop == true or == false altogether!
  • Instead of if (play == true) { just write if (play) {.
  • For negation like if (play == false) {, use operator !: if (!play) {
1 Like

Thank you both! This really helped.

1 Like

I formatted your code and made a play() function

float squareX;
float squareY;
float score = 0;
float scoreDis;
float circleX;
float circleY;

float pulX;
float pulY;
float purX;
float purY;
float pdlX;
float pdlY;
float pdrX;
float pdrY;

boolean play;
boolean gameOver;

boolean[] keyDwn = new boolean[4];

final int _keyR = 39;
final int _keyL = 37;
final int _keyU = 38;
final int _keyD = 40;

// ---------------------------------------------------------------------------------

void setup() {
  size(600, 600);
  background(0, 255, 0);

  surface.setTitle("run");

  scoreDis = round(score);
  squareX = width/2;
  squareY = height/2;

  play = true;
  gameOver = false;

  keyDwn[0] = false;
  keyDwn[1] = false;
  keyDwn[2] = false;
  keyDwn[3] = false;

  circleX = width/2;
  circleY = 10;

  pulX = squareX-15;
  pulY = squareY-15;
  purX = squareX+15;
  purY = squareY-15;
  pdlX = squareX-15;
  pdlY = squareY+15;
  pdrX = squareX+15;
  pdrY = squareY+15;
}

void draw() {
  if (play) {
    play();
  }
  if (gameOver) {
    background(0); // black
    square(squareX, squareY, 30);
    text(scoreDis, width/2, height/2);
    text("Game Over", 22, 22);
  }
}

// ---------------------------------------------------------------------------------

void play() {
  background(0, 255, 0); // green

  rectMode(CENTER);
  stroke(0);
  strokeWeight(1);
  square(squareX, squareY, 30);

  stroke(255);
  strokeWeight(1);
  point(pulX, pulY);
  point(purX, purY);
  point(pdlX, pdlY);
  point(pdrX, pdrY);
  score = score+1;
  if (keyDwn[0]) {
    squareY = squareY-5;
  }
  if (keyDwn[1]) {
    squareX = squareX+5;
  }
  if (keyDwn[2]) {
    squareY = squareY+5;
  }
  if (keyDwn[3]) {
    squareX = squareX-5;
  }

  if (squareX >= 585) {
    squareX = 585;
  }
  if (squareX <= 15) {
    squareX = 15;
  }
  if (squareY <= 15) {
    squareY = 15;
  }
  if (squareY >= 585) {
    squareY = 585;
  }

  strokeWeight(1);
  stroke(0);
  fill(255, 0, 0);
  square(circleX, circleY, 30);

  pulX = squareX-15;
  pulY = squareY-15;
  purX = squareX+15;
  purY = squareY-15;
  pdlX = squareX-15;
  pdlY = squareY+15;
  pdrX = squareX+15;
  pdrY = squareY+15;

  if (circleX > squareX) {
    circleX = circleX-4;
  }
  if (circleX < squareX) {
    circleX = circleX+4;
  }
  if (circleY > squareY) {
    circleY = circleY-4;
  }
  if (circleY < squareY) {
    circleY = circleY+4;
  }

  if (pulX > circleX-15 &&
    pulX < circleX + 15 &&
    pulY > circleY-15 &&
    pulY < circleY + 15) {
    play = false;
    gameOver = true;
  }
  if (purX > circleX-15 &&
    purX < circleX + 15 &&
    purY > circleY-15 &&
    purY < circleY + 15) {
    play = false;
    gameOver = true;
  }
  if (pdlX > circleX-15 &&
    pdlX < circleX + 15 &&
    pdlY > circleY-15 &&
    pdlY < circleY + 15) {
    play = false;
    gameOver = true;
  }
  if (pdrX > circleX-15 &&
    pdrX < circleX + 15 &&
    pdrY > circleY-15 &&
    pdrY < circleY + 15) {
    play = false;
    gameOver = true;
  }
}

void keyPressed() {
  if (keyCode == _keyU) {
    keyDwn[0] = true;
  }
  if (keyCode == _keyR) {
    keyDwn[1] = true;
  }
  if (keyCode == _keyD) {
    keyDwn[2] = true;
  }
  if (keyCode == _keyL) {
    keyDwn[3] = true;
  }
}

void keyReleased() {
  if (keyCode == _keyU) {
    keyDwn[0] = false;
  }
  if (keyCode == _keyR) {
    keyDwn[1] = false;
  }
  if (keyCode == _keyD) {
    keyDwn[2] = false;
  }
  if (keyCode == _keyL) {
    keyDwn[3] = false;
  }
}