Game Development Reference
In-Depth Information
Chapter 22
Loading Levels from Files
22.1 Introduction
Many games consist of different levels. Especially in casual games such as puzzles
and maze games, the game may have several hundreds of levels. With the program-
ming tools that we have seen thus far, we could add different levels to our game by,
for example, writing a generic Level class, and then defining hundreds of subclasses,
where each class defines what the particular level looks like. This approach has a
few disadvantages. The most important disadvantage is that we are mixing the game
logic (gameplay, win condition, and so on), with the game content . This means that
every time we wanted to add another level to the game, we would need to write a
new class, which would lead to a lot of classes that need to be compiled. A much
better approach would be to store the different levels in a text file, and then read the
information from this text file to create the levels. This way, we can define a Level
class that loads its content and settings from a file. Then, if we want to add another
level to our game, we do not have to change the source code but we can edit the text
file. This has the advantage that developers do not need to be involved with creating
the levels anymore. We can simply give the program to an artist who can develop
the levels by editing the text file, while we can focus on developing the game itself.
In this chapter, we will see how we can load levels from a text file using the C# file
I/O classes.
22.2 Structure of a Level
Let us first look at what kind of things can be inside a level. First, there will be some
kind of background image. Let us assume that this background is fixed when we
load the level, so there is no need to store any information about that in the text file.
Inside the level, there will be penguins, seals, sharks, icebergs, background
blocks that penguins can move on, and a few more things. We want to store all
of this information in a text file. One possibility would be to store for every object
 
Search WWH ::




Custom Search