# Score won't stay

**URL:** https://discourse.processing.org/t/score-wont-stay/43739
**Category:** Beginners
**Created:** [January 23, 2024, 1:13pm UTC](https://discourse.processing.org/t/score-wont-stay/43739 "2024-01-23T13:13:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)
#### Post date: [January 23, 2024, 1:13pm UTC](https://discourse.processing.org/t/score-wont-stay/43739/1 "2024-01-23T13:13:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 23, 2024, 1:46pm UTC](https://discourse.processing.org/t/score-wont-stay/43739/2 "2024-01-23T13:46:06Z")

</div>

> [@WillyGrz](#):
>
> if (play = true) {

This is not correct

Should be ==

Two ==

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 23, 2024, 1:58pm UTC](https://discourse.processing.org/t/score-wont-stay/43739/3 "2024-01-23T13:58:33Z")

</div>

- 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) {`

---

<div class="post-metadata">

### Author: ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)
#### Post date: [January 23, 2024, 3:27pm UTC](https://discourse.processing.org/t/score-wont-stay/43739/4 "2024-01-23T15:27:17Z")

</div>

Thank you both! This really helped.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 23, 2024, 8:17pm UTC](https://discourse.processing.org/t/score-wont-stay/43739/5 "2024-01-23T20:17:21Z")

</div>

I formatted your code and made a play() function

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

```
