Last modified: April 05, 2025
In this lab, you will add enemies to your Lode Runner game who chase the closest player available, whether it's Player or MousePlayer.
The enemies will walk, climb up ladders, and bars just like how your players move around. To avoid writing repeated code, you can take advantage of Java inheritance.
In the previous lab, you made two player classes, where one class inherited the movement code from the other.
In this lab, you will be creating an Enemy class that will follow the player just like the mouse player followed the mouse. Since you need to get a reference to the player, it is important to get a list of Player objects in the world.
However, if you make Enemy a subclass of Player, then calling getWorld().getObjects(Player.class)
will return a list of Players and Enemies since Enemy extends the Player class and is a kind of Player.
In order to get a list of Player-only objects in the world, we can create a general Person class that represents all objects that move around like a person in the world.
The Person class should have all the movement code and the Player and Enemy classes should both extend Person.
The Player class should override the getCommand()
method to return the direction indicated by the keyboard input
and the Enemy class should override the getCommand()
method to return the direction that is toward the location of the player,
just like the mouse player moved toward the location of the mouse.
Now, you can create the actual enemies.
Create an enemy class that follows the closest player available, whether it's Player or MousePlayer.
Give it an appropriate image, size, and movement animation. See the "images" folder.
The person class should have animation code that works for both player and enemy. You have a couple of options for this:
String fall;
. In the constructors of the player and enemy classes, initialize these variables to the appropriate image name. Note that all the enemy images start with "enemy" and the player images start with "player", so you could take advantage of that naming scheme.It would be nice if your enemies were smarter and did not get caught on walls as often. Here is a more advanced algorithm that will more effectively find the player:
For now, you do not need to keep track of player's lives nor reduce life when an enemy hits a player.
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