mo.h
mo.h

Reputation: 15

How to add Score to a game

I am trying to add a score feature to my game. I know that where I have a remove function I now need to add code which means that whenever an enemy is removed it contributes to the score of the game but I don't know how to implement that into my game using code. Below I have included relevant code.

      boolean alive() {
        for (int i = 0; i < bullets.size(); i++) {
          Bullet bullet = (Bullet) bullets.get(i);
          if (bullet.x > x && bullet.x < x + pixelsize * 7 && bullet.y > y && bullet.y < y + 5 * pixelsize) {
            bullets.remove(i);
            bullet.alive = false;
            return false;
          }
        }
        for (int i = 0; i < bullets.size(); i++) {
          Bullet bullet = (Bullet) bullets.get(i);
          if (bullet.alive == false) {
            bullets.remove(i);
          }
        }
        return true;
      }

Upvotes: 1

Views: 11404

Answers (2)

Enias Cailliau
Enias Cailliau

Reputation: 486

You didn't mention if you'd like the score to be permanent or not. If so, you could write the scores to a simple csv file to get a leaderbord. Eaven a normal texfile would do.

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42176

Step 1: Create an int score variable at the sketch level.

int score = 0;

Step 2: Increment that variable whenever you want to increase the score.

score += 100;

Step 3: Display that score in the draw() function whenever and wherever you want the score to be displayed.

text("Score: " + score, 20, 20); 

Also, I noticed that you're comparing String values with the == operator. Don't do that. Instead, use the equals() function:

String x = "test";
if(x.equals("blah")){
   //whatever
}

Upvotes: 1

Related Questions