Last modified: April 15, 2023
First, download the Level Loader Starter Code.
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.
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.
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.
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.
You are now ready to load a level!
In Greenfoot, right click on the MyLevelWorld and select the first option: new MyLevelWorld()
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.
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().
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.
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.
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.
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:
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
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