We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have created a game in processing and although I have done the fundamentals of the game I am now looking to add a score feature to it. I understand that where I used the remove function for my enemies I now need to add something which means that whenever an enemy is removed it contributes to the score of the game but I dont know how to implement that into my game using code. Below I have included the bits of code which I think need to be used for this.
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = (Enemy) enemies.get(i);
if (enemy.outside() == true) {
direction *= (-1);
incy = true;
break;
}
}
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = (Enemy) enemies.get(i);
if (!enemy.alive()) {
enemies.remove(i);
} else {
enemy.draw();
}
}
incy = false;
}
}
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;
}
Answers
Something is weird with the code you posted. Where is the SpaceShip class? What is @Override (line 120)?
It's easy to add a score. Just have a score variable, and increase it each time an enemy ship is removed.
Thanks for pointing that out and all that override does It means that the particular method is overriding a method from the parent class.
What sort of a variable will you need to keep track of a score?
Is your score going to be a floating point number? Or a String? No. It's going to be an integer.
Now you need to give that variable a name. It's going to keep track of the current score. You want a name that will remind you what the value is stores is. Since it is the score... A good name for the variable is "score".
Now what value should your score start at? 100? -6? No. 0.
That's a complete assignment statement. Add a semicolon.
Now you will want to display the score in your sketch window. You will want to draw it as some text. There is a function that lets you draw text in the sketch window: text().
What parameters does text() take? There are three of them. The first is the text you want to display. What we want to display is the score.
The next two parameters are WHERE we want the text to be displayed. We want a position that is inside the sketch window. A good position is (20,60).
Great. Now let's try your sketch with those lines added.
Where do you add those lines? The score variable needs to be global. So you would add it at the highest scope. That means you would put that line outside of any function.
The text() call needs to happen ever frame while playing the game. It should go inside the draw() function. It might be a good idea to put it inside the conditional statement that draws the level, so it doesn't show up on the start screen.
How do you increment the score? You would use the increment operator. What's that? It's ++. To increment the score, you'd just do:
When should you do this? You should do it when an enemy is removed. Where is the line of code that removes an enemy? Do it after that line.
You now have the exact three lines of code you need.
You've been told where to put them.
Try putting them in your code now.
Post your code again with where you think they should go.
Wow, that was a really good explanation. Thanks for really breaking it down and helping me out. I am struggling to figure out where exactly I should include the score code in my game.
https://forum.Processing.org/two/discussion/15987/how-to-add-score-and-end-game-state
GoToLoop I have already checked that link out.
You have an ArrayList of enemies.
This keeps track of your enemy objects.
At some point an enemy is hit by a bullet and causes that enemy's alive() function to return false instead of true.
When that happens, the enemy is removed from your ArrayList.
Find where that is happening and increment the score there.
It is incredibly troubling that you are having this much trouble modifying your own code.
It works now thanks for the help.
I like this forum. Reminds me a bunch of the Youtube channel Extra Credits. Everybody is really nice to each other there.
That's true, and thanks for saying that