Game Development Reference
In-Depth Information
obliged to group them in methods. Every instruction in a program belongs to a
method. Grouping instructions is done with braces ( { and } ). Such a block of instruc-
tions grouped together is called the body of a method. Above the body, we write the
header of the method. An example of a method header is:
static void Main()
The header contains, among other things, the name of the method (in this case Main ).
As a programmer, you may choose any name for a method. However, every program
must have exactly one method named Main .The Main method tells the compiler
which instructions should be executed when the program starts. We have seen that
the game loop consists of two parts: Update and Draw . In programming terms, these
parts are modeled as methods , as you can see in the example program. Inside these
methods, we then place the instructions that we want to execute in order to update
or draw the game world.
3.3.4 A Class: A Group of Methods with a Name
Because C# is also an object-oriented language, methods, in turn, are grouped in
classes . If you look more carefully at the BasicGame program, you see that there are
five methods grouped in the class called BasicGame . Just like instructions, methods
cannot be arbitrarily placed in a program. Instructions need to be part of the body
of a method, and methods must be part of a class. Grouping methods is also done
by using braces. This means that around our methods we place braces (this is called
the body of the class) and above it we see the header of the class:
class BasicGame : Game
The class header consists of the word class and after that, the name of the class. As
a programmer, you are allowed to choose any name for your class, which in this
case is BasicGame . Sometimes, a class is a specialized version of another class. This
is the case with our BasicGame class: we indicate behind the class name that we are
building a game , and not a console application or a web application. We do this by
using a colon and the name of another class ( Game ). We will come back to this later
on.
3.3.5 Syntax Diagrams
Programming in a language such as C# can be quite difficult if you do not know the
rules of the language. In this topic, we will use so-called syntax diagrams to explain
how the language is structured. The syntax of a programming language refers to the
 
Search WWH ::




Custom Search