Tuesday, March 22, 2011

Learn C sharp Gaming Chapter 3


Your First C# Program
There is an ancient tradition (okay it’s not that old) in computer programming that says
that your first program in any language should be a “Hello World” program, a program
that simply prints out a welcome message on your computer.
On the CD for this book you will find a demo entitled “HelloCSharp.”You can find it in the
/Demos/Chapter02/01-HelloCSharp/ directory. The HelloCSharp.cs file in that directory
contains the code for the program; you can open it up in any text editor or Visual Studio
and view it. The code should look like this:
class HelloCSharp
{
static void Main( string[] args )
{
System.Console.WriteLine( “Hello, C#!!” );
}
}
At first glance, you can see that this is about four or five lines longer than you could write
it in C or C++; that’s because C# is a more complicated language.

Classes
C# is an object-oriented programming language, which may not mean anything to you at
this point. “A Brief
Introduction to Classes,” but for now, all you need to know is that C# represents its programs
as objects.
The idea is to separate your programs into nouns and verbs, where every noun can be represented
as an object. For example, if you make a game that has spaceships flying around,
you can think of the spaceships as objects.
A class in a C# program describes a noun; it tells the computer what kind of data your
objects will have and what kind of actions can be done on them. A spaceship class might
tell the computer about how many people are in it, how much fuel it has left, and how fast
it is going.
The Entry Point
Every program has an entry point, the place in the code where the computer will start execution.
In older languages like C and C++, the entry point was typically a global function
called main, but in C# it’s a little different. C# doesn’t allow you to have global functions,
but rather it forces you to put your functions into classes, so you obviously cannot use the
same method for a C# entry point. C# is like Java in this respect; the entry point for every
C# program is a static function called Main inside a class.Every C# program must have a class that has a static Main function; if it doesn’t, then the
computer won’t know where to start running the program. Furthermore, you can only
have one Main function defined in your program; if you have more than one, then the computer
won’t know which one to start with.
note
Technically, you can have more than one Main function in your program, but that just makes things
messy. If you include more than one Main, then you need to tell your C# compiler which class contains
the entry point—that’s really a lot of trouble you can live without.
Hello, C#!!
The part of the program that performs the printing is this line:
System.Console.WriteLine( “Hello, C#!!” );
This line gets the System.Console class—which is built into the .NET framework—and tells
it to print out “Hello, C#!!” using its WriteLine function.
Compiling and Running
There are a few ways you can compile this program and run it. The easiest way would be
to open up a console window, find your way to the demo directory, and use the commandline
C# compiler to compile the file, like this:
csc HelloCSharp.cs
Now, when you run the program, you should get a simple output on your screen:
Hello, C#!!
The Basics
Almost every programming language has common properties. For one thing, programming
languages generally know how to store data. They must also operate on that data by
moving it around and performing calculations on it.
Basic Data Types
Like most programming languages, C# has a large number of built-in data types, mostly
representing numbers of various formats. These are shown in Table 2.1.
note
C# is an extendible language, which means that you can create your own data types later on if you
want.
The integer-based types (byte, short, int, long, and so on) can only store whole numbers,
such as 0, 1, 2, and so on; they cannot hold decimal numbers, such as 1.5 or 3.14159.
C# Built-in Data types
Type Size (bytes) Values
Ø  bool 1 true or false
Ø  byte 1 0 to 255
Ø  sbyte 1 -128 to 127
Ø  char 2 Alphanumeric characters (in Unicode)
Ø  short 2 -32,768 to 32,767
Ø  ushort 2 0 to 65,535
Ø  int 4 -2,147,483,648 to 2,147,483,647
Ø  uint 4 0 to 4,294,967,295
Ø  *float 4 -3.402823x1038 to 3.402823x1038
Ø  long 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Ø  ulong 8 0 to 18,446,744,073,709,551,615
Ø  *double 8 -1.79769313486232x10308 to 1.79769313486232x10308
Ø  **decimal 16 -79,228,162,514,264,337,593,543,950,335 to
Ø  79,228,162,514,264,337,593,543,950,335
* - These are floating-point formats, which can represent inexact decimal values
* - This is a fixed-point format, which represents exact decimal values with up to 28 digits
In order to hold decimal numbers, you need to switch to either a floating-point or a fixedpoint
format. The exact details on how these kinds of numbers are stored is beyond the
scope of this book, but there is a subtle difference that will affect scientists and mathematicians
(but probably not game programmers).
note
Basically, floating-point numbers cannot hold precise numbers; they can only approximate decimal
numbers within a certain amount of error. For example, using floats, you can represent the numbers
1.0 and 1.00000012, but you can’t represent any number in between. So, if you set a float to be
equal to 1.00000007, then the computer will automatically round that up to 1.00000012. Doubles
are the same way, but have more precision (up to 15 digits). Decimals are encoded in a different
way, and even though the .NET documentation calls them fixed-point numbers, they are still technically
floating-point numbers, and they have a precision of up to 28 digits.
Operators
Operators are symbols that appear in a computer language; they tell the computer to perform
certain calculations on data. Operators are commonly used in math equations, so
I’m sure this concept will be very familiar to you.
The C# language has a number of built-in operators in the language, and if you’ve ever
used C++ or Java, then you probably already know most of them.
Mathematical Operators
C# has five basic mathematical operations built into the language, as shown in Table 2.2.
The first four operators are no-brainers, or at least they ought to be. The fifth operator may
be new to you if you haven’t done a lot of programming before. Modulus is sometimes
Basic Mathematical Operators in C#
Operator Symbol Example Result
Ø  Addition + 5 + 6 11
Ø  Subtraction - 6 - 5 1
Ø  Multiplication * 6 * 7 42
Ø  Division / 8 / 4 2
Ø  Modulus % 9 % 3 0
Ø  Increment ++ 10++ 11
Ø  Decrement — 10— 9
%known as “the remainder operator” or “the clock operator.” Basically, the result from a
modulus operation is the same as the remainder if you took the first number and divided
it by the second. In the example given in Table 2.2, 3 divides into 9 evenly, so the remainder
is 0. If you took 10 % 3, the result would be 1, as the remainder of 10/3 is 1.
note
Modulus is often called the clock operator because you can easily calculate the result using a clock.
For example, take the calculation 13 % 12. Imagine you have the hand of a clock starting at 12, and
you move it forward one hour every time you count up by 1. So when you count to 1, the hand will
be at 1, and when you count to 2, the hand will be at 2, and so on. Eventually, when you get to 12,
the hand will be at 12 again, and when you count to 13, the hand moves back to 1. So the result
of 13 % 12 is 1.
note
The increment and decrement operators actually each have two different versions: the post- and
pre- versions. For example, ++x is the pre-increment version, and x++ is the post-increment version.
The difference is when the operators actually perform their calculations. For example, if x is 10 and
you write y = x++, then the computer first puts the value of x into y and then increments x, leaving
y equal to 10 and x equal to 11 when the code is done. On the other hand, y = ++x performs the
increment first and performs the assignment later, leaving both x and y equal to 11. This is another
holdover from C, and can make it ugly and difficult to read, so I don’t really recommend using these
operators too much.
You should note that all mathematical operators have alternate versions that allow you to
directly modify a variable (see more about variables later on in this chapter). For example,
if you wanted to add 10 to x, you could do this:
x = x + 10;
But that’s somewhat clunky and redundant. Instead, you can write this:
x += 10;
All of the other math operators have similar versions:
x *= 10; // multiply by 10
x /= 10; // divide by 10
x -= 10; // subtract 10
x %= 10; // modulus by 10
x >>= 2; // shift down by 2
x <<= 2; // shift up by 2
Bitwise Math Operators
In addition to the standard math operators, there are also bitwise math operators, which
perform binary math operations on numbers. The basic bitwise operators in C# are listed
in Table 2.3.
Bitwise math operators have alternate versions as well:
x &= 10; // and by 10
x |= 10; // or by 10
x ^= 10; // xor by 10
Shifting Operators
There are two shifting operators, << and >>. These operators shift the bits in a number up
or down, resulting in the following equations:
_ - x << y is the same as x * 2y
_ - x >> y is the same as x / 2y
So 5 << 3 is the same as 5 * 8, or 40, and 40 >> 3 is the same as 40 / 8, or 5.
note
Bitshifting is a lot faster than straight multiplication or division, but it’s rarely used anymore. The
speed savings just aren’t that spectacular, and it makes your programs harder to read, anyway.
Logical Operators
There are a few common logical operators that perform comparisons on things and
return the Boolean values true or false, depending on the outcome. Table 2.4 lists the logical
operators.
The Basics 19
Basic Bitwise Operators in C#
Ø  Operator Symbol Example Result
Ø  Binary And & 6 & 10 2
Ø  Binary Or | 6 | 10 14
Ø  Binary Xor ^ 6 ^ 10 12
Ø  Binary Not ~ ~7* 248
* - this example is performed on a byte

Variables
In C#, as in almost any other language, you can create instances of the basic data types,
called variables, and perform mathematical operations on them.
Declaring a piece of data in your program is an easy thing to do. All you need to do is put
in the name of the type of data, then the name of the variable you want to create after that,
and then (optionally) initialize the data with a value. Here’s an example:
int x = 10;
float y = 3.14159;
decimal z;
caution
Note that if you try using a variable before initializing it (if you try using z from the previous code
sample, for example), then you will get a compiler error in C#. Older languages, such as C and C++,
would allow you to use a variable without giving it a value, which could cause a lot of errors
because you never know what was in the variable if you never set it!
Here’s an example using variables with the mathematical functions:
int x = 10 + 5; // 15
int y = 20 * x; // 300
int z = x / 8; // 1
float a = (float)x / 8.0; // 1.875
x = (int)a; // 1
Logical Operators in C#
Operator Symbol Example Result
Ø  Equals == 1 == 2 false
Ø  Des Not Equal != 1 != 2 true
Ø  Less Than < 1 < 2 true
Ø  Greater Than > 1 > 2 false
Ø  Less Than or Equal To <= 1 <= 2 true
Ø  Greater Than or Equal To >= 1 >= 2 false
Ø  Logical And && true && false false
Ø  Logical Or || true || false true
Logical Not ! !true false
* - This example is performed on a byte
Pay particular attention to the last two lines. These lines show you how to use typecasts in
your program. An explanation of typecasts is coming soon.
Constants
You can declare constants, pseudo-variables that cannot be changed, in your code. This is
just another safety feature that’s been around in computer languages for years now. For
example:
const float pi = 3.14159;
Now you can use pi in your calculations, but you can’t change its value (because changing
the value of pi to 3.0 makes absolutely no sense!). This will cause a compiler error:
pi = 3.0; // ERROR!
t i p
Constants improve the readability of your programs by eliminating magic numbers. Magic numbers
are numbers in your program that have no immediate meaning to whomever is reading it. For example,
you can write x = 103; somewhere, but no one really knows what 103 means. It could mean the
number of bullets in an ammo clip, or something else completely. Instead, you can use constants
to show exactly what you mean, by defining a constant, called const int BulletsInClip = 103;,
earlier in your program and then later using the constant x = BulletsInClip;

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...