Tino Intro To Java

Last modified: April 05, 2025

Lode Runner HW 6 Create Enemies

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.

Creating the Person Class

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.

Creating the actual Enemy

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.

Class Hierarchy

Images and Animations

The person class should have animation code that works for both player and enemy. You have a couple of options for this:

  • Option 1: Make instance variables representing each image name. Example: 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.
  • Option 2: Make helper methods that return the appropriate image. You can then override these methods in the player and enemy classes to return the appropriate image name. The animations with multiple frames could have helper methods that take a parameter representing which frame number the animation is on.

(Optional) Optimizations

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:

  1. If the enemy is at the same y as the player, then go toward the player (left or right)
  2. If the enemy is below the player, then if they can go up, then go up, otherwise go in the direction of the nearest position where they can go up (i.e. a ladder going up)
  3. If the enemy is above the player, then if they can go down, then go down, otherwise go in the direction of the nearest position where they can go down (i.e. a ladder going down or a position where they would fall down)

For now, you do not need to keep track of player's lives nor reduce life when an enemy hits a player.

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