This chapter will probably be one of the most satisfying of this project. The reason for this is that by the end of it, we will have a playable game. Although there will still be features to implement (sound, particle effects, HUD, and shader effects), Bob and Thomas will be able to run, jump, and explore the world. Furthermore, you will be able to create your very own level designs of almost any size or complexity by simply making platforms and obstacles in a text file.
We will achieve all this by covering following topics in this chapter:
LevelManager
class that will load levels from a text file, convert them into data our game can use, and keep track of the level details, such as spawn position, current level, and allowed time limitLevelManager
Remember our sprite sheet that we introduced in Chapter 12, Abstraction and Code Management - Making Better Use of OOP. Here it is again, annotated with numbers that represent each tile that we will build our levels from:
I placed the screenshot on a gray background so you could see clearly the different details of the sprite sheet. The checkered background represents the level of transparency. So, all of the tiles except for number 1 will reveal at least a little of the background behind them:
Keep this screenshot in mind as we discuss designing the levels.
We will enter combinations of these tile numbers into text files to design the layouts. An example will help:
0000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000 1111111111000111111222222221111133111111111411 0000000000000000001222222221000133100000001110 0000000000000000001222222221000133100000000000 0000000000000000001222222221000133100000000000 0000000000000000001111111111000111100000000000
The previous code translates to the following level layout:
Note that to get the view shown in the previous screenshot, I had to zoom out the View
. Also, the screenshot is cropped. The actual start of the level would look like the following screenshot:
The point of showing you these screenshots is two fold. Firstly, you can see how you can quickly construct level designs using a simple and free text editor.
Secondly, the screenshots demonstrate the gameplay aspects of the design. From left to right in the level, Thomas and Bob first need to jump a small hole or they will fall to their deaths (re-spawn). Then they have a large expanse of fire to traverse. It is actually impossible for Bob to jump that many tiles. The players will need to work together for the solution. The only way that Bob will clear the fire tiles is by standing on Thomas's head and jumping from there, as shown in the following screenshot:
It is then quite simple to get to the goal and move on to the next level.
I have included a few level designs to get you started. They are in the levels
folder that we added to the project back in Chapter 12, Abstraction and Code Management - Making Better Use of OOP.
What follows are some zoomed out views of the game, along with a screenshot of the code of the level design. The screenshot of the code is probably more useful than reproducing the actual textual content. If you do want to see the code, just open up the files in the levels
folder.
This is what the code looks like:
This is level layout that the previous code will produce:
This level is the "leap of faith" level I referred to in Chapter 12, Abstraction and Code Management - Making Better Use of OOP:
I have highlighted the platforms, as they are not very clear in the zoomed-out screenshot:
The provided designs are simple. The game engine will be able to handle very large designs, however. You have the freedom to use your imagination and build some really big and hard-to-complete levels.
Of course, these designs won't actually do anything until we learn how to load them and convert the text into a playable level. Additionally, it won't be possible to stand on any platforms until we have implemented the collision detection.
First, let's handle loading the level designs.
3.139.105.159