Tino Intro To Java

Last modified: April 02, 2024

Lode Runner HW 7 HUD, Gold, Score

HUD (Heads Up Display)

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.

Creating Text

Create a new subclass of Actor named Text. Then, declare five private instance variables in the Text class:

  1. A String representing the actual text it contains
  2. An int representing font size
  3. A Color representing foreground (text color)
  4. A Color representing background color
  5. A Color representing outline color

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:

  1. Define a constructor for your Text class that takes a single String parameter.
  2. Set your text instance variable to the value of the String parameter and set the size, foreground, background, and outline to define reasonable default values.
  3. Make sure to call updateImage() after initializing the instance variables.
  4. Define a getter and setter for each instance variable you declared (i.e. getText(), setText(String text), getSize(), setSize(int size)...etc).
  5. Each setter should also updateImage() so the text shown always matches the values of the instance variables. To enforce the use of getters and setters by users of this class, all your instance variables should be private.

Keeping track of score

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.

Accessing Score from other classes

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

Creating Gold and Score

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.

Submission

Zip your entire project folder and submit it below.

  • Name your project folder PX_LastName_FirstName_LodeRunner.
  • Right click on your project folder and Send To -> Compressed (zipped) folder. On a mac compressing is similar, using a right click, but has a slightly different option.
    • You should end up with a zip file named 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
  • Make sure your Google Doc has Editing permissions for "anyone with the link". This is for your teacher and TAs.
  • Paste the link to your Google Doc in the Submission Comments section on this submission form (which becomes visible after you enter a file to submit)

You must Sign In to submit to this assignment

Dark Mode

Outline