Tino Intro To Java

Last modified: April 15, 2023

Lode Runner HW 9 Lives and Levels

Setup

First, download the Level Loader Starter Code.

  1. Unzip and open the project to confirm it is working. You should see a level loaded.
  2. The starter code has a LevelWorld class that is an abstract class that can load levels from files in the levels folder.
  3. There is also a subclass of LevelWorld called MyLevelWorld that you will customize to use your classes to load levels.
  4. The only class you will modify is the MyLevelWorld class.
  5. The LevelLoader class is the class that actually reads the level files and adds actors to the world. It keeps track of which characters in the level file correspond to which classes.
  6. The LodeRunnerLevelLoader class is a subclass of LevelLoader that contains helper methods to make it easier to specify which classes represent which types of objects and also is able to load levels by level number. The text (.txt) files in the levels folder each represent a level, where the first level in the list alphabetically is level 1 and the second level alphabetically is level 2...etc.

Next, download the Level Editor. The link was updated on 4/15/2023 so you will have access to a more flexible level editor that has built in support for custom types.

  1. Unzip and open the project.
  2. Press run and adjust the width and height of the level as desired, then click on New Level to create a new level.
  3. You can click and drag to draw with the current tile (it starts out as wall). At the bottom you can see directions for which keys will change the tile to which types of tile. You should only add tiles that represent classes you actually have in your game.
  4. Make a level and press s to save it. You should save it in the levels folder.

Level Loader and Level World

  1. Close Greenfoot. You will be adding some files and folders to the project and Greenfoot will not recognize them until you close and reopen it.
  2. Copy the LevelWorld.java, MyLevelWorld.java, LevelLoader.java and LodeRunnerLevelLoader.java files to your lode runner project folder.
  3. Copy the levels folder to your lode runner project folder. You can also copy the custom level you made with the editor to the levels folder of your project.
  4. Open your project in Greenfoot and examine the LevelWorld class. You may see some errors which you can fix in the next sections. If there are no errors, you can just right click on MyLevelWorld and make a new MyLevelWorld(). It should load the first level in your levels folder. You may encounter an error when you do that. See the next sections for how to fix errors.

Debugging Tips

The most common error you will run into in MyLevelWorld, is in the code the defines which classes should represent which objects in the world. You should see code like this:

// define which classes represent walls, ladders, bars, players, and enemies
// TODO: REPLACE WITH YOUR CLASSES
getLoader().setWallClass(Wall.class);
getLoader().setPlayerClass(Player.class);
getLoader().setLadderClass(Ladder.class);
getLoader().setBarClass(Bar.class);
getLoader().setEnemyClass(Enemy.class);
getLoader().setGoldClass(Gold.class);

As indicated in the comment, you need to replace the classes with YOUR classes. For example, if the class representing your walls is called Platform, then you would change Wall.class to Platform.class. If there are objects that you don't even have in your game, you can either make a new class to represent them or just delete that line of code. For example, if you only have a Brick class and a Player class in your game and you don't want other objects in your levels yet, then the code above would be reduced to:

getLoader().setWallClass(Brick.class);
getLoader().setPlayerClass(Player.class);

Note that if those are the only classes you have, then you won't be able to load any levels that have other objects in them.

Transferring Other Features

Once you have fixed the compiler errors, you should consider if there are any other instance variables or methods that your old world class has that you will need to keep. You won't need any methods that simply lay out your old level, but you might want other methods.

If in doubt, you can transfer all the helper methods, since they shouldn't cause any harm even if you don't use them, but it is nice if you keep the code fairly clean of extraneous code.

You should still have a copy of your old world class, so you can always add methods again later if you find you need them.

The one thing you definitely should NOT transfer is constructors. Your old constructors were most likely filled with code that lays out your old level by placing platforms, ladders and bars. You don't need any of that anymore because that will all be loaded from a file.

If there is other initialization code in your world class that isn't related to drawing levels, you can simply copy it to the MyLevelWorld(int level) constructor.

Cleaning Up

Before you create a new MyLevelWorld, you should make sure the levels that will be loaded are levels that only contain objects represented by classes in your project.

If you haven't already, you should use the Level Editor to create a level that meets that requirement.

Copy that level into the levels folder in your project. Delete any levels in the levels folder that contain objects that are not in your game.

If you aren't sure, you can just delete all the levels except the custom one(s) you made yourself. You can always get them back later if you want.

Note that the latest starter code comes with two levels that only have walls and player objects (a_wall_player_level1.txt and a_wall_player_level2.txt) so those should work fine even if you only have wall and player classes.

Creating a new MyLevelWorld

You are now ready to load a level!

In Greenfoot, right click on the MyLevelWorld and select the first option: new MyLevelWorld()

  • If you set everything up correctly in the previous sections, it should load one of your custom levels (the first one alphabetically in the levels folder)
  • If you get an error message, you will need to investigate the error.
    • If you get an out of bounds error, it is most likely because you don't actually have any levels in the levels folder in your lode runner project.
    • if you don't even have a levels folder, you will get a NullPointerException

If everything worked, you are ready to change your start button so it sets the world to an instance of your MyLevelWorld instead of your old world. See the next sections.

Connecting Your Menu to MyLevelWorld

If everything is working up to now, this part should be fairly painless.

Replace the code in your start button that sets the world to an instance of your old world with code that sets the world to a new MyLevelWorld().

Lives and Score

In the following sections, you will create a system for transferring lives and score between levels. If you have an alternate idea that you think will work, you are welcome to do it differently.

  1. Make instance variables for lives and score at the top of your MyLevelWorld class.
  2. Add lives and levels parameters to your MyLevelWorld(int level) constructor. You will need to update your default constructor to pass default values for lives and score.
  3. Initialize the lives and score variables to the values passed in and update the HUD as needed to display the values.

Lose Life

When an enemy touches the player, you should set the world to a new MyLevelWorld passing the same level and score, but one less life.

To do this, you will need to add an act() method to you Player class. Since you still want the player to perform all the actions in your top level Person class, you can call super.act() to run the Person class version of act(). Then, you can add code after that to detect if an enemy touched the player and respond accordingly.

If the player is out of lives, game over should be shown. It could be text that displays in the middle of the game world or it could be a separate game over world similar to the menu world. In either case, there should be a way for the player to go back to the main menu.

Next Level

When the player has collected all the gold in the world, set the world to a new MyLevelWorld with level and lives increased by 1 and score the same.

  • To find out if all the gold has been found, you can ask the world for a list of all the Gold objects in the world. If there are no objects in the list, then the player has collected all the gold.
  • You decide which class will check if all gold has been found. It could be done in the act() method of the Player class, the World class or even the Gold class, if the Gold objects are responsible for collecting themselves. Just note that if you do it in the Gold class, the last gold collected will need to do whatever is necessary before the act() method finishes, since after that there will be no more gold left so act() will not be called on a Gold object again.
  • To get the current level, you can call getLevel() from your MyLevelWorld class (that method is inherited from LevelWorld)

If the level passed is the last level, you can decide exactly how to handle it. Here are some options:

  • Show a Win screen (could be a subclass of world like the Menu or it could just be a Text Label in the middle of the world). In either case, you should include a way for them go back to the main menu.
  • Loop back to level 1 and let them keep playing to get a higher score.

To Find out how many levels there are, call this method from the LodeRunnerLevelLoader class:

static int numLevels(): returns the number of levels in the levels folder

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