Chapter 14. Building Playable Levels and Collision Detection

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:

  • Exploring how to design levels in a text file
  • Building a 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 limit
  • Updating the game engine to use LevelManager
  • Coding a polymorphic function to handle the collision detection for both Bob and Thomas

Designing some levels

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:

Designing some levels

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:

  • Tile 0 is completely transparent and will be used to fill in the gaps where there aren't any other tiles
  • Tile 1 is for the platforms that Thomas and Bob will walk on
  • Tile 2 is for fire tiles and tile 3 is for water tiles
  • Tile 4 you might need to look quite closely to see. It has a white, square outline. This is the goal of the level where Thomas and Bob must get to together.

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:

Designing some levels

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:

Designing some levels

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.

Tip

Just make sure you use a monospace font so that all the numbers are the same size. This makes designing the levels much easier.

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:

Designing some levels

It is then quite simple to get to the goal and move on to the next level.

Tip

I strongly encourage you to complete this chapter and then spend some time designing your own levels.

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:

Designing some levels

This is level layout that the previous code will produce:

Designing some levels

This level is the "leap of faith" level I referred to in Chapter 12, Abstraction and Code Management - Making Better Use of OOP:

Designing some levels

I have highlighted the platforms, as they are not very clear in the zoomed-out screenshot:

Designing some levels

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.180.113