Games that don't keep score and never end aren't much fun. We'll take these ideas one at a time. FIrst we'll need a new variable to keep track of score. First declare your new variable at the beginning of the program. Then modify your mousePressed function:
By adding speed to score the faster targets are worth more points. Keeping score is no fun if no one knows what it is. The text() function allows you to print text in your sketch.
In this example text() has three parameters. The first is what is to be printed, the second and third are the x and y coordinates. Add this line below the line for drawing your ball and run the sketch.
It would be more satisfying if it was more than just a number so replace the line with:
The text in quotes is a String. This line will print the string followed immediately by the number stored in the variable score.
We're keeping score now, but there's still no way to lose the game. Create a variable to keep track of your lives. You'll need to declare it with the other variables. We'll also need to decrease it when you miss.
Then add to your mousePressed function:
We added else to our if(). else runs if the if() is false. So if you hit the circle the score increases and if you miss you lives decreases. You should add a text() function to print lives remaining so the players know how they're doing. But, what will happen when lives gets to zero? At this point, nothing. So we need to add a way to end the game and restart again.
Add the following code in to the end of the draw().
noLoop() makes the program stop looping draw(), effectively ending the program. We need to add a few more concepts if we want to restart the game. First we'll introduce a new variable type, boolean. Booleans have two states, true or false. In the variable declaration add this line:
Then add lost=true; above noLoop() in our if(). This will allow us to get things start up again. We're going to add more to our mousePressed() function so we can restart.
The loop() function starts draw() looping again. Now if we click on the screen after we've lost the game will reset. The only other thing to add would be a message to the player that they should click to restart. So we'll add a bit in our draw().
|
Processing for Android > MACUL 2012 >