# Adding Score Mechanism

**URL:** <https://discourse.processing.org/t/adding-score-mechanism/30690>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [June 13, 2021, 11:50pm UTC](https://discourse.processing.org/t/adding-score-mechanism/30690 "2021-06-13T23:50:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@azul](https://discourse.processing.org/u/azul)\
**Post date:** [June 13, 2021, 11:50pm UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/1 "2021-06-13T23:50:08Z")

</div>

Hi, I made a simple game, yet I couldn’t add a scoring mechanism and an opening scene. When the spaceship hits one of the balls, it loses score, when it explodes the balls using mouse function, it gains scores. It would be so appreciated if you would help!

Here’s the code:

```auto
Ball b;
Ball [] balls;

int start;
float x = 50;
float y = 325;
float spaceShipX = 0;
float xspeed = 3;
float yspeed = 3;
PImage img;

float [] xstars = new float [100] ;
float []ystars = new float [100];
float[] speedS = new float [100] ;

import processing.sound.*;
SoundFile file;

void setup() {
  size(700, 700);
  noStroke();
  fill(255, 100, 100);

  img = loadImage("spaceShip.png");

  start = millis();

  b=new Ball();
  balls = new Ball[20];
  for (int i = 0; i < balls.length; i++) {
    balls[i] = new Ball();
  }

  stroke(255);
  strokeWeight(2);

  int i = 0;
  while (i <100) {
    xstars [i] = random(0, width);
    ystars [i]= random(0, height);
    speedS [i]= random(1, 5);
    i = i + 1;
  }

  file = new SoundFile(this, "game-intro-space.wav");
  file.play();
}

void draw() {
  background(0);

  int timer = millis()-start;
  textSize(25);
  text("Time:", 25, 35);
  text(timer, 100, 35);

  text("Score:", 425, 35);
  text(timer, 100, 35);

  if (timer > 20000) {
    fill(255);
    stroke(255);

    println("GAME OVER ");

    textMode(CENTER);
    strokeWeight(3);
    textSize(25);

    text("TIME IS UP!", 300, height/2);
    text("GAME OVER!", 285, height/2+200);

    noFill();
    stroke(#466F45);

    rectMode(CENTER);
    rect(width/2, 450, 300, 75);
    fill(255);
    stroke(255);
    textMode(CENTER);
    text("PLAY AGAIN", 280, 455);
  }

  b.update();
  b.display();
  for (int i = 0; i <balls.length; i++) {
    balls[i].update();
    balls[i].display();
  }

  PImage spaceShip = loadImage("spaceShip.png");

  spaceShip.resize(0, 50);
  image(spaceShip, x, y);

  int i = 0;
  while (i < 100) {

    point(xstars[i], ystars[i]);
    xstars[i] = xstars[i] - speedS[i];
    if (xstars[i] < 0) { 
      xstars[i] = width;
    }
    i = i + 1;
  }

  //SPACESHIP MOVES//

  if (x >width) { 
    x = 0;
  }
  if (x <0) {
    x = width;
  }
  if (y >height) {
    y = 0;
  }
  if (y <0) {
    y = height;
  }
}

//SPACESHIP MOVES//

//SPACESHIP MOVES OPTION 2//

/* if (x>650) {
 x=x-25;
 xspeed=xspeed*-1;
 }
 if (x<0) {
 x=x+25;
 xspeed=xspeed*-1;
 }
 if (y>650) {
 y=y-25;
 yspeed=yspeed*-1;
 }
 if (y<0) {
 y=y+25;
 yspeed=yspeed*-1;
 } */

//SPACESHIP MOVES OPTION 2//

// LEFT HAND CONTROL//

void keyPressed() {
  if (key == 'a') {
    x -=20;
  }
  if (key == 'd') {
    x +=20;
  }
  if (key == 'w') {
    y -=20;
  }
  if (key == 's') {
    y +=20;
  }

  // RIGHT HAND CONTROL//

  if (key == CODED) {
    if (keyCode == UP) {
      y -=20;
    } else if (keyCode == DOWN) {
      y +=20;
    } else if (keyCode == LEFT) {
      x -=20;
    } else if (keyCode == RIGHT) {
      x +=20;
    }
  }
}

void mousePressed () {
  int i = 0;
  while (i < 500) {
    ellipse(x+100+i, y+25, 5, 5);
    i = i + 50;
  }
}

```

```auto
class Ball {

  float a, b, w, h;
  float speedX, speedY;
  color c;

  Ball() { 

    a = random(250, width-100);
    b = random(250, height-100);
    w = random(20, 60);
    h = w;
    c = color(random(255), random(255), random(255), random(100));
    speedX = random(-5, 5);
    speedY = random(-5, 5);
  }

  void update() {

    checkBounds();
    a+=speedX;
    b+=speedY;
  }

  void display() {

    fill(c);
    ellipse(a, b, w, h);
  }

  void checkBounds() {

    if (a<0+w/2 || a>width-w/2) {
      speedX*=-1;
    }

    if (b<0+h/2 || y>height-h/2) {
      speedY*=-1;
    }
  }
}

```

---

<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:** [June 14, 2021, 8:08pm UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/2 "2021-06-14T20:08:21Z")

</div>

I implemented a start screen for your

**Score**

for the score : Do you mean that the circles (the shooting) destroying a ball gives score increase?

- Then in mousePressed() in the while loop compare the circle pos (for all balls, use a for-loop) to the ball pos (using `dist()` command): `if dist < ball radius say score++;`

[EDITED]

**Pseudo Code**

```auto
    boolean hit=false; 
    while (i < 500) {

      // check this ellipse on all balls (loop over all balls)
      for(int j= 0; j < balls.length; j++) {
           if(dist(x+100+i, y+25, balls[j].x, balls[j].y ) < 33) { // or radius instead of 33
                  // the ellipse / circle / bullet hit the ball / meteorite
                  score++; 
                  hit=true; 
                  // kill ball
                  break;
           }//if
      }//for

      if(!hit)
          ellipse(x+100+i, y+25, 5, 5);

    }//while 

```

* * *

**My Sketch**

with start Screen but without score

```auto

Ball b;
Ball [] balls;

boolean startScreen=true; 

int start;
float x = 50;
float y = 325;
float spaceShipX = 0;
float xspeed = 3;
float yspeed = 3;
PImage img;

float [] xstars = new float [100] ;
float []ystars = new float [100];
float[] speedS = new float [100] ;

int score=0; 

PImage spaceShip ; 

import processing.sound.*;
SoundFile file;

void setup() {
  size(700, 700);
  noStroke();
  fill(255, 100, 100);

  // img = loadImage("spaceShip.png");
  spaceShip = loadImage("spaceShip.png");
  // spaceShip.resize(0, 50);

  start = millis();

  b=new Ball();
  balls = new Ball[20];
  for (int i = 0; i < balls.length; i++) {
    balls[i] = new Ball();
  }

  stroke(255);
  strokeWeight(2);

  int i = 0;
  while (i <100) {
    xstars [i] = random(0, width);
    ystars [i]= random(0, height);
    speedS [i]= random(1, 5);
    i = i + 1;
  }

  file = new SoundFile(this, "game-intro-space.wav");
  // file.play();//??????
}

void draw() {
  if (startScreen) {
    background(110);
    fill(0); 
    text("Game Start\n\n\nPress mouse to start ", 
      133, 133);
  } else {
    drawForGame() ;
  }
}

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

void drawForGame() {
  background(0);

  int timer = millis()-start;
  textSize(25);
  text("Time:", 25, 35);
  text(timer, 100, 35);

  text("Score:", 425, 35);
  text(score, 100, 35);

  if (timer > 20000) {
    fill(255);
    stroke(255);

    // println("GAME OVER ");

    textMode(CENTER);
    strokeWeight(3);
    textSize(25);

    text("TIME IS UP!", 300, height/2);
    text("GAME OVER!", 285, height/2+200);

    noFill();
    stroke(#466F45);

    rectMode(CENTER);
    rect(width/2, 450, 300, 75);
    fill(255);
    stroke(255);
    textMode(CENTER);
    text("PLAY AGAIN", 280, 455);
  }

  b.update();
  b.display();
  for (int i = 0; i <balls.length; i++) {
    balls[i].update();
    balls[i].display();
  }

  // image(spaceShip, x, y);
  text("spaceShip", x, y); // ????

  int i = 0;
  while (i < 100) {

    point(xstars[i], ystars[i]);
    xstars[i] = xstars[i] - speedS[i];
    if (xstars[i] < 0) { 
      xstars[i] = width;
    }
    i = i + 1;
  }

  //SPACESHIP MOVES//

  if (x >width) { 
    x = 0;
  }
  if (x <0) {
    x = width;
  }
  if (y >height) {
    y = 0;
  }
  if (y <0) {
    y = height;
  }
}

//SPACESHIP MOVES//

//SPACESHIP MOVES OPTION 2//

/* if (x>650) {
 x=x-25;
 xspeed=xspeed*-1;
 }
 if (x<0) {
 x=x+25;
 xspeed=xspeed*-1;
 }
 if (y>650) {
 y=y-25;
 yspeed=yspeed*-1;
 }
 if (y<0) {
 y=y+25;
 yspeed=yspeed*-1;
 } */

//SPACESHIP MOVES OPTION 2//

// LEFT HAND CONTROL//

void keyPressed() {
  if (key == 'a') {
    x -=20;
  }
  if (key == 'd') {
    x +=20;
  }
  if (key == 'w') {
    y -=20;
  }
  if (key == 's') {
    y +=20;
  }

  // RIGHT HAND CONTROL//

  if (key == CODED) {
    if (keyCode == UP) {
      y -=20;
    } else if (keyCode == DOWN) {
      y +=20;
    } else if (keyCode == LEFT) {
      x -=20;
    } else if (keyCode == RIGHT) {
      x +=20;
    }
  }
}

void mousePressed () {

  if (startScreen) {
    startScreen=false;
  } else {
    // drawForGame() ;

    int i = 0;
    while (i < 500) {
      ellipse(x+100+i, y+25, 5, 5);
      i = i + 50;
    }
  }
}

//====================================================================

class Ball {

  float a, b, w, h;
  float speedX, speedY;
  color c;

  Ball() { 

    a = random(250, width-100);
    b = random(250, height-100);
    w = random(20, 60);
    h = w;
    c = color(random(255), random(255), random(255), random(100));
    speedX = random(-5, 5);
    speedY = random(-5, 5);
  }

  void update() {

    checkBounds();
    a+=speedX;
    b+=speedY;
  }

  void display() {

    fill(c);
    ellipse(a, b, w, h);
  }

  void checkBounds() {

    if (a<0+w/2 || a>width-w/2) {
      speedX*=-1;
    }

    if (b<0+h/2 || y>height-h/2) {
      speedY*=-1;
    }
  }
}

```

---

<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:** [June 14, 2021, 8:10pm UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/3 "2021-06-14T20:10:40Z")

</div>

> [@azul](#):
>
> ```auto
> text("Time:", 25, 35);
> text(timer, 100, 35);
> 
> text("Score:", 425, 35);
> text(timer, 100, 35);
> 
> ```

remark

the 4th text must be `text(score,....`

**Remark**

> [@azul](#):
>
> ```auto
> PImage spaceShip = loadImage("spaceShip.png");
> 
> spaceShip.resize(0, 50);
> 
> ```

not good

better:

before setup say `PImage spaceShip ;`

in setup (without PImage!!!)

```auto
  spaceShip = loadImage("spaceShip.png");
  spaceShip.resize(0, 50);

```

---

<div class="post-metadata">

**Author:** ![azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@azul](https://discourse.processing.org/u/azul)\
**Post date:** [June 15, 2021, 12:25am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/4 "2021-06-15T00:25:21Z")

</div>

Oh, thanks a lot for your contribution and feedback, now I see how to add a start screen. It really helped, yet I couldn’t understand how to increase the score every time the spaceship shoots the balls since it seems too complicated for me. Really couldn’t understand how to use dist() command. Can you help with that too? Thank you in advance! It’s exactly what you said: the circles (the shooting) destroys a ball gives score increase.

---

<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:** [June 15, 2021, 6:24am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/5 "2021-06-15T06:24:15Z")

</div>

I described what you have to do in great detail.

Please check the reference for dist() and then show your attempt please

---

<div class="post-metadata">

**Author:** ![azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@azul](https://discourse.processing.org/u/azul)\
**Post date:** [June 15, 2021, 7:51am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/6 "2021-06-15T07:51:58Z")

</div>

Oh, okay. I tried it and with your contribution, I came up with:

```auto
while (i < 500) {

      // check this ellipse on all balls
      for( int i=0; i<500; i=i+1){
           if(dist(x+100+i, y+25, balls[i].a, balls[i].b ) < 33) { // or radius instead of 33
                  score ++; 
                  hit=true; 
                  // kill ball
                  break;
           }//if
      }//for

      if(!hit)
          ellipse(x+100+i, y+25, 5, 5);
    }

```

however, couldn’t run the sketch properly. I changed the j in your code with i (couldn’t understand what j symbolizes here), and also in the `balls[j].x` part, it didn’t run since I couldn’t figure out in my code what is the ball position. (I initiliazed them as a and b). Anyway, thanks a lot for your comments.

---

<div class="post-metadata">

**Author:** ![azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@azul](https://discourse.processing.org/u/azul)\
**Post date:** [June 15, 2021, 8:09am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/7 "2021-06-15T08:09:59Z")

</div>

Now, I changed it like this:

```auto
boolean hit=false;
int j=0;
while (i < 500) {

      // check this ellipse on all balls
      for (int i = 0; i < balls.length; i++) { 
           if(dist(x+100+i, y+25, random(250, width-100), random(250, height-100) ) < 33) { // or radius instead of 33
                  score ++; 
                  hit=true; 
                  // kill ball
                  break;
           }//if
      }//for

      if(!hit)
          ellipse(x+100+i, y+25, 5, 5);
  }
boolean hit=false;
int j=0;
while (i < 500) {

      // check this ellipse on all balls
      for (int i = 0; i < balls.length; i++) { 
           if(dist(x+100+i, y+25, random(250, width-100), random(250, height-100) ) < 33) { // or radius instead of 33
                  score ++; 
                  hit=true; 
                  // kill ball
                  break;
           }//if
      }//for

      if(!hit)
          ellipse(x+100+i, y+25, 5, 5);
  }

```

I thought that I could add the a and b values, yet whenever I click it stucks. It seems that I need to figure out what to use for the ball pos. Desperately looking for clues:(

---

<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:** [June 15, 2021, 10:08am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/8 "2021-06-15T10:08:17Z")

</div>

You really need i in the outer loop and another variable ( j) in the inner for loop

The names don’t matter but you want to compare each circle/bullet with each ball

Therefore 2 different variables as the indexes

---

<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:** [June 15, 2021, 10:09am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/9 "2021-06-15T10:09:20Z")

</div>

And don’t use random

You want to check your circles against the balls position (not against random)

---

<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:** [June 15, 2021, 10:50am UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/10 "2021-06-15T10:50:35Z")

</div>

> [@Chrisir](#):
>
> ```auto
> boolean hit=false; 
> while (i < 500) {
> 
> // check this ellipse on all balls
> for( j.... over all balls 
> 
> ```

with `for(int j....` over all balls to be more precise

I changed my pseudo code above, it’s not so pseudo anymore.  
Please note that it is policy of this forum not to do homework. That’s why I don’t do the full Sketch…

---

<div class="post-metadata">

**Author:** ![azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@azul](https://discourse.processing.org/u/azul)\
**Post date:** [June 15, 2021, 5:16pm UTC](https://discourse.processing.org/t/adding-score-mechanism/30690/11 "2021-06-15T17:16:43Z")

</div>

I totally understand, thanks anyway. Even though the deadline is over, I am gonna figure it out, I am a beginner and eager to learn, I guess asking too many questions is part of that, too 🙂
