Last modified: April 02, 2024
Add a section at the bottom of your lode runner world that displays the score, lives and level. For now, you can just hardcode the score to 0, lives to 5 and level to 1. It should look something like this:
To conveniently display these text objects and potentially update them, you will create a Text class.
Create a new subclass of Actor named Text. Then, declare five private instance variables in the Text class:
Next, declare a default constructor for Text initializing the instance variables to default values.
Then, declare an instance method named updateImage()
that does not return anything and takes no parameters.
UpdateImage() creates a new GreenfootImage passing the values of the instance variables to the appropriate GreenfootImage constructor, and sets the image of the Text object to the new image.
Make the Text constructor call updateImage() after initializing the instance variables.
Finally, try adding a Text to your World to see if it works.
Be sure your Text works as expected.
Now, add the final touches to your Text class:
To keep track of the score, you should create private instance variables in the World class. You will need one instance variable that stores the actual score as an integer and another instance variable to store a reference to the Text object that displays the score.
You should create a public getter and setter for the integer score, but don't make a getter or setter for the score Text because that should simply update automatically when the score changes. In the setter for the integer score, update the score Text to display the new score each time score changes.
To access and change the score from other classes, you will need to get a reference to the world and typecast it to your subclass of world. You can then call public methods on that world. For example, if your subclass of world is named LodeWorld, then you would write something like this:
LodeWorld lw = (LodeWorld)getWorld(); // get a reference to the world and typecast it to a LodeWorld
int currentScore = lw.getScore(); // get the current score
lw.setScore(currentScore + 100); // increase the score by 100
Add a class to represent gold.
When the player touches gold, increase the score by 250.
Make sure the text in the HUD is also updated to display the new score. This should happen automatically if you wrote the setScore method correctly.
Zip your entire project folder and submit it below.
PX_LastName_FirstName_LodeRunner
.PX_LastName_FirstName_LodeRunner.zip
. For example if you were in 3rd period and named Michael Wang, then you would name the file P3_Wang_Michael_LodeRunner.zip
You must Sign In to submit to this assignment