Adding Score Mechanism

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:

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

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

    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



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

remark

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

Remark

not good

better:

before setup say PImage spaceShip ;

in setup (without PImage!!!)

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

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.

1 Like

I described what you have to do in great detail.

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

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

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.

Now, I changed it like this:

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:(

1 Like

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

And don’t use random

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

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…

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 :slight_smile:

1 Like