Last modified: January 26, 2023
In this lab, you will create a simple game where a moving pedestrian is being showered by fruits.
You can download a demo of this lab here.
First, create a Pedestrian actor. Upon creation:
Then, make a private instance variable to keep track of what color the Pedestrian should be (hint: this could be an integer)
Make a setter for the color variable that sets the value AND changes the image of the Pedestrian accordingly. If an invalid color value is passed in, set the color to blue and the value to the value that maps to blue.
A Pedestrian behaves according to the following rules:
Give pedestrians a private health attribute initialized to 5
Test your class by selecting the Pedestrian class and holding down shift while clicking on the world. A Pedestrian should appear in the world. When you press run, it should behave as described in the directions.
Create the Fruit actor.
In the default constructor for fruit, set the image of the fruit to a random image chosen from plum, apple, or orange.
Hint: you can use a system similar to what you did with Pedestrian. If needed you can even change the image names.
Every frame, if a fruit is not touching the bottom of the canvas, it should move down by 4.
The following behaviors may be written in the Fruit class or the Pedestrian class depending on how you would like to implement them.
If a Pedestrian is touching a fruit that is not the same color as the Pedestrian, reduce the pedestrian's health by 1, reverse the direction of the pedestrian and remove the fruit. If the pedestrian is touching fruit of the same color, the fruit is just removed.
A pedestrian should always have text written on its shirt that says how much health it has left
You can use a GreenfootImage constructor that creates an image that displays a String, and use the drawImage method to draw that image on the pedestrian image.
Hint: Make an updateImage method that sets the image and draws the string showing the health, then call that method in the setters for health and color.
if a pedestrian has 0 health, the pedestrian stops moving and show text that says Game Over in the middle of the world.
Test your code by adding a pedestrian to the world using shift click and then adding several fruits also using shift clicking.
With the Pedestrian and Fruits ready, finally, create MyWorld.
In the constructor, create a Pedestrian and place it in the bottom left corner, completely in the world.
Override the act() method such that each frame there is a 0.7% chance that a fruit will spawn fully in the world, somewhere random along the top edge.
If the game is over, no more fruit should spawn, fruit should not move and the pedestrian should not move.
Hint: make an instance variable to keep track of whether the game is over
If you make a public getter method isGameOver() that returns true if game is over and false otherwise, you can call that on the world like this:
// typecast the result of getWorld() to be a MyWorld.
// typecasting is promising the compiler that the object you are referencing is a specific type
// This is needed because only MyWorld has an isGameOver() method. The World class does not.
MyWorld world = (MyWorld)getWorld();
boolean gameIsOver = world.isGameOver();
Zip your entire project folder and submit it below.
PX_LastName_FirstName_Pedestrian
.PX_LastName_FirstName_Pedestrian.zip
. For example if you were in 3rd period and named Michael Wang, then you would name the file P3_Wang_Michael_Pedestrian.zip
You must Sign In to submit to this assignment