Game Development Reference
In-Depth Information
So, a complete level in the text file looks something like this:
Splash!
Don't let the penguins fall in the water!
1
99
422
#.......#
#...r...#
#.......#
#. .#
#. .#
#. .#
#.......#
#...r...#
#.......#
For every level, we add such a definition to the text file. The example provided with
the topic contains 12 levels. When we read the text file, the first line in the file is the
number 12, so that we know how many levels we have to read. The question is: how
do we get the information that is in the file into our program?
22.5 File Reading in C#
22.5.1 Reading a Text File
Just like other game assets such as sprites or sounds, text files are also placed in
the content project. In this example, you can see that the level files are placed in a
sub folder called Levels . You can drag and drop other text files in this folder if you
wish to create more levels. However, Visual Studio does not know what a text file is
supposed to be. The default behavior of Visual Studio is to try to compile this text
file. This is not what we want. After all: the text file does not contain any C# code.
You can change these settings by right-clicking on the text file that you added and
select 'Properties'. A property panel should appear (see Fig. 22.1 ). Make sure that
the option 'Build Action' is set to 'None'. If you select any other build action, the
compiler will try to execute that action, which will most probably fail, resulting in
an error.
C# knows a number of standard classes for dealing with input and output (also ab-
breviated as I/O). A number of commonly used I/O-classes together with subclasses
are given in Fig. 22.2 . Most of these classes are part of the System.IO namespace,
but some of them are in separate namespaces for networking, XML, cryptography
or compression.
Search WWH ::




Custom Search