Game Development Reference
In-Depth Information
3.4.3 Whitespace and Indentation
As you can see in the BasicGame example, we use whitespace quite liberally. There
is an empty line between each method, as well as spaces between an equals sign
and the expressions either side of it. Spacing can help to clarify the code for
the programmer. For the compiler, they have no meaning. The only place where
a space is really important is between separate words. It is not allowed to write
protected override void Update as protectedoverridevoidUpdate . And similarly, it is not
allowed to write an extra space in the middle of a word. In text that is interpreted
literally, spaces are also taken literally. There is a difference between:
Content.RootDirectory = "Content";
and
Content.RootDirectory = "C o n t e n t";
But apart from this, extra spaces are allowed everywhere. Good places to put extra
whitespace are:
behind every comma and semicolon (but not before),
left and right of the equals sign ( = ), you can see an example of this in the instruc-
tion Content.RootDirectory = "Content"; ,
at the beginning of lines, so that the bodies of methods and classes are indented
(usually four positions) with respect to the braces enclosing the body.
The Visual Studio editor helps you a little bit by automatically performing the in-
dentation for you. Also, the editor automatically places whitespace at certain spots
in your code to increase readability.
3.5 What You Have Learned
In this chapter, you have learned:
what the skeleton of a game is, consisting of the game loop and the game world
that the loop acts upon;
how to structure a game program, consisting of a Game subclass, setting up the
graphics device and implementing the Draw method to change the background
color;
the basic layout rules of a program, including how to place comments in your
code and where to put extra whitespace to improve readability of your code.
Search WWH ::




Custom Search