Here's how you should change the act() method in AvoiderWorld: The thinking is that you get some points for every enemy created because you will ultimately have to avoid it. One place we could do this is where we create the enemies in AvoiderWorld. We need to call addScore() on our scoreboard to increase our score over time. (compile, run, test) I'm the worst! My score is always 0! The prepare() method contains code you have seen before and should make perfect sense to you. Last, you need to add this function to your AvoiderWorld class: Add "private ScoreBoard scoreboard " above the constructor function. Next, we need to create a variable at the top of our class (outside any method) to hold our ScoreBoard. The function call you need to add is "prepare() ". First, in your AvoiderWorld() constructor function, you need to add a function call at the end of that function. I'm going to describe how to place Actors in your world manually. In tutorial 4 on the Greenfoot site, you were introduced to "Saving the World" to have the World class automatically place Actors in your world. We want the score to appear immediately in the game. To learn more about this Color class, look at the official documentation: If you don't add this to the code above you'll get compile errors. Make sure you can see all the uses! To get this Color class into your code you need to have an import statement at the top (this is chopped off in my picture). Java provides a class that manages most of the complexity for us: Color. Luckily, we don't have to know much about the theory. If you are curious, you can read more about it here: There are several different ways to represent color on computers (and basically anything with a screen) and we are going to use an RGBA color model. In the constructor function above, Scoreboard(), we do some operations that involve color. Again, read the documentation for GreenfootImage to understand the methods I used.ĭid you really get this far without compiling and running? Shame on you!! You should be doing this every time you write 3-5 lines of code! Working with Color This method creates a score by creating a new background image and drawing an image that contains the score text into it. So, we also use the class GreenfootImage to create text. In Greenfoot, text is viewed as an "image". Notice the use of this method in the other methods in the class. update() This function does the real work of displaying our score. Read the documentation to fully understand the GreenfootImage methods I used. I use the GreenfootImage class to construct and draw the background box. While the score will constantly change, the background box only needs to be created once (and that's why it is in the constructor function). First, we will draw a box to hold the score in and, second, we will draw the score text on top of this box. Our score is going to consist of two parts. Remember my big long explanation of constructor methods from Part II? This is where we put all of our initialization code. ScoreBoard() This is our constructor method. addScore() Whenever we detect we have avoided an enemy, we will call this function to add points to our score. This method allows us to access the current score stored in the ScoreBoard. The levels will change based on the current score. getScore() Later in this tutorial, we will create levels for the game. Changes in score are done via the addScore() method, thus, this method is empty. act() Our scoreboard does not have any independent action that it must perform. OK, let's tackle the ScoreBoard code one method at a time. Edit the code for ScoreBoard so that is looks like this: Do not associate an image with this class. Create a new subclass of Actor and name it "ScoreBoard". Obviously, a "scoreboard" is not a World, therefore, it must be an Actor. We are going to add a scoreboard to display the score to the player. We are going to add a scoring system to the game that rewards players for the number of enemies they avoid. levels completed, time, progression) but the most common method is to assign the player a "score". There are many ways to judge game performance (e.g. Our game is becoming more interesting, however, we need a way to judge how well we are doing in the game. ― Max Brooks, The Zombie Survival Guide Part III: Enhancing Playability We need a score! “Often, a school is your best bet-perhaps not for education but certainly for protection from an undead attack.” “Study without desire spoils the memory, and it retains nothing that it takes in.”
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |