Sie sind auf Seite 1von 108

DigiPen Institute of Technology

Presents

Overview of the Game Development Process


DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session One:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Game Web Cast Project: Introduction

Introduction:
In partnership with Microsoft, DigiPen Institute of Technology is pleased to present a series of eight one-hour webcasts to introduce participants on how video games are produced. The main game development concepts will be demonstrated via the creation of a top-down shooter using C# 2005 Express Edition, Microsofts new lightweight integrated development environment designed for beginning programmers. Participants are able to download C# 2005 Express Edition along with webcast tutorial materials to experience building the game themselves. It should be noted that the intent of this webcast series is to provide an overview of the game development process and is not designed to train participants in all aspects of C#. Webcast Sessions: Session 1: Overview of Game Development Process Session 2: Basic Programming Concepts and Introduction to C# Session 3: Overview of Game Elements Session 4: Introduction to Sprites and Animation Session 5: Transformation and Collision of Sprites Session 6: Player Control of Sprites Session 7: Game Music and Sound Effects Session 8: Creating Sprite Behavior The DigiPen Game Development Webcast Series continues through May 2005. Go to http://www.microsoft.com/events/series/msdnvideodev.mspx to register or to access archives.

About DigiPen Institute of Technology:


Based in Redmond, Washington, a major center of game development in the United States, DigiPen Institute of Technology is acknowledged as a leader in interactive entertainment technology education. Since 1994, DigiPen has been successfully educating students for careers in the video game industryas programmers, production artists and computer engineers. DigiPen offers the following degrees: Master of Science in Computer Science Bachelor of Science in Real-time Interactive Entertainment Bachelor of Science in Computer Engineering Associate of Applied Arts in 3D Computer Animation Bachelor of Fine Arts in Production Animation. For more details about DigiPens degree programs, visit http://www.digipen.edu. In addition to this webcast series, DigiPen Institute of Technology also offers a number of outreach efforts to explore a career in video game development: DigiPen Summer Workshops: Series of introductory classes in game programming, 3D animation production and robotics. Information about the workshops is available at http://workshops.digipen.edu. DigiPen ProjectFUN Distance Learning Initiative: DigiPen has launched a new online program taught live by DigiPen instructors. For details, go to http://projectfun.digipen.edu.

DigiPen Game Development C# Webcast Series - Session One, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Introduction

1 Introduction to Game Web Cast Project


Introduce the web cast topic and outline. The preparation for this section should start after the hours from 2 to 8 are prepared. o Introduction: covers what the first hour is about o C# overview: covers what the second hour is about o Game components: covers what the third hour is about o Building the demo: covers what the rest of the hours are about Second section of the first hour starts here; it is basically used to explain what a real-time interactive concurrent events application is. Play the demo game o Please note that the game is implemented with simplicity in mind in order to show clearly the game architecture and components o Try to show that the events of the game happen concurrently o Try to show that the objects are moved at real time using the keyboard interaction What we just saw is a real-time interactive concurrent events application simulating a starship fighting enemies in space. Why concurrent? o The starship, bullets, enemies, sound effect, music, and text exist and move at the same time as a series of coincident events o The list of coincident events: Testing the keyboard for up, down, left, and right key strokes Scrolling the background New ship position calculation Testing if the fire key is hit Creating a bullet Calculating the new bullet position Testing for collision between each bullet with each enemy Calculating the new enemy position Playing a sound effect when an enemy intersects with a bullet Playing the explosion animation when an enemy intersects with a bullet Updating the texts Playing the music Etc. Why interactive? o The player decides when and where to move the ship o The player decides when and where to fire a bullet o The game AI decides when to attack o The number of bullets increases each time a bullet is fired o The score updates each time an enemy is hit o The enemy explodes when hit by a bullet o Etc. Why real-time? o When the ship moves, its new position is calculated at run time o The collision between the bullet and the enemy is detected at run time o The enemys new position happens at run time o The text updates at run time o Etc. How do we write an application with concurrent events? o We need to be able to execute several instruction at the same time o It would be nice if we have a CPU dedicated for each event o Usually, we only have one CPU o But we need to execute in parallel several instructions DigiPen Game Development C# Webcast Series - Session One, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Introduction o o o o o

If we divide the second into 60 pieces Each piece would be 1/60 or 0.016 a second (this is also 16.66 milliseconds) If during each 16.66 ms we update sequentially all game component The game components would be updated 60 times a second The player will get the illusion that the events are coincident or parallel this means that the events are happening at the same time. o In reality the events are not parallel; they are pseudo-parallel o The events do not happen at the same time, they happen sequentially o Because the events are updated at a fixed interval of 60 times a second, the illusion of concurrent events is achieved o Each 16.66 ms duration is one game iteration o Since the iteration repeats as long as the game is playing, the concurrent events are controlled by repeating the game iteration through a game loop o The game loop also makes the series of events update as a motion picture or pictures in motion Game loop o The game loop iteration duration greatly affects the illusion of concurrent events o If the duration of the game iteration is long, lets say 0.1 s, then the simulation will feel slow o Why? Because the reaction to the events happens only 10 times in one second o Show the game at 10 fps, 30 fps, and 60fps o On the other hand if the duration of the game iteration is short, like 0.016 s, then the simulation feel smooth o Why? Because the reaction to the events happens 60 times in one second o Consequently, the duration of the game iteration is called the frame o Therefore, the game speed is measured by frames per second o For example, when we say a game speed is 60 frames per second or 60 f/s or 60 fps, then the game iteration duration is 16.66 ms How do we add interaction at real time with concurrent events? o During the game iteration, we: Detect and register the user input Execute the behavior of each object; usually the object behavior depends on: Input from the keyboard Input from other objects Collision status AI Etc. Once all the objects are updated, their position and status at the current game loop is determined We render the objects What we just mentioned means that the objects are rendered as many times a second as the game speed For example in a 60 fps game, the objects are rendered 60 times a second During the beginning of the rendering during the game loop, a blank frame is prepared Then all the objects are rendered sequentially When an object o moves, its position at game loop n is slightly different than its previous position at game loop n-1; also, its position at game loop n+1 would be different than the position at game loop n Show example of the moving bullet at a low frame rate

DigiPen Game Development C# Webcast Series - Session One, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Introduction

Frame n

Frame n +1

Frame n +2

DigiPen Game Development C# Webcast Series - Session One, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Introduction

Being drawn several times consecutively, the sequence of different pictures and different positions provide the illusion of a motion picture As a matter of fact, movies at the movie theater play at 25 pictures per second Game components o Background: static object o Sprites: dynamic objects o Text o Sound Sound effects Music o Object behavior: specifies its interaction. Overview of the game flow

Game Loop
Initialize game Initialize starting time Read player Input

Handling Computing the new position for every object based on: Behavior Collision Physics Artificial Intelligence

No

Drawing rendering the image one object at a time to a buffer

Copy the buffer content to the Video Memory in order to display the frame

Check if elapsed time is less than 16.66 ms

Yes

Do nothing

DigiPen Game Development C# Webcast Series - Session One, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Overview of C# Programming
DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session Two:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
2 C# Types
2.1 Value Types 2.1.1 bool 2.1.2 byte 2.1.3 char 2.1.4 decimal 2.1.5 double 2.1.6 enum 2.1.7 float 2.1.8 int 2.1.9 long 2.1.10 sbyte 2.1.11 short 2.1.12 struct 2.1.13 uint 2.1.14 ulong 2.1.15 ushort 2.2 Reference Types 2.3 void 14 14 15 15 15 16 16 17 17 17 18 18 18 19 19 19 19 20

3 Variables
3.1 Introduction 3.2 Categories of Variables in C# 3.2.1 Static Variables 3.2.2 Instance Variables 3.2.3 Array Elements 3.2.4 Value Parameters 3.2.5 Reference Parameters 3.2.6 Output Parameters 3.2.7 Local Variables 3.3 Default Values 21 21 21 21 21 21 21 22 22 22

4 Expressions, Statements & Operators


4.1 Classification of Expressions 4.2 Operators 4.3 Function Members 4.4 Unary Operators 4.5 Arithmetic Operators 4.6 Shift Operators 4.7 Relational & Type Testing Operators 4.8 Logical Operators 23 23 24 25 26 28 28 29

5 Functions
5.1 Definition 5.2 Scope 5.3 Calling a Function 5.4 return Statement Example 5.5 Call by Value Function Arguments 5.6 Call by Reference Function Arguments 31 31 31 31 32 33

5.7 Function Returning Value Example

34

6 Flow Control
6.1 The while Statement 6.2 The do-while Statement 6.3 The for Loop 6.4 The foreach, in Statement 6.5 The jump Statement 6.5.1 break 6.5.2 continue 6.5.3 goto 6.5.4 return 6.6 The switch Statement 6.7 if-else 35 35 36 36 37 37 37 38 38 39 39

7 Classes
7.1 Definition of a Class 7.2 Class Declaration 7.3 Members of a Class 7.4 Constants 7.5 Fields 7.6 Methods 7.7 Properties 7.8 Events 7.9 Indexers 7.10 Operators 7.11 Instance Constructors 7.12 Static Constructors 7.13 Destructors 41 41 41 43 43 44 44 45 45 45 46 46 46

8 Arrays
8.1 Introduction 8.2 Array Types 8.3 Array Creation 8.4 Array Element Access 8.5 Array Members 8.6 Array Initializers 47 47 47 47 47 47

9 Structures
9.1 Definition of a Struct 9.2 Struct Declaration 9.3 Members of a Struct 9.4 Class and Struct Differences 49 49 49 49

10 Miscellaneous
10.1 C# 2.0 10.1.1 The System.Diagnostics Namespace 10.1.2 The System.Drawing Namespace 10.1.3 The System.Windows.Forms Namespace 10.1.4 The System.Collections.Generic Namespace 51 51 51 51 51

10.2 Generics 10.3 The Exception Class 10.4 DirectX 10.4.1 Direct3D 10.4.2 DirectInput 10.4.3 DirectSound 10.4.4 DirectX.AudioVideoPlayback 10.4.5 The Vector2 Class 10.4.6 The Matrix Class

52 52 52 52 53 53 53 53 54

Game Web Cast Project: C# Programming Overview

1 C# Programming Overview
1.1 Introduction
C# is a new, simple programming language based on the older programming language called C++. The similarities between C# and C++ are easily notified. This chapter is an overview of the C# programming language. In this chapter we might use some instructions or expressions without explaining their meaning. These materials will be covered in subsequent chapters.

1.2 The Smallest C# Program


static void Main( ) { } static void Main( ) { }

OR

The main function is the entry point of a C# program. All standard C# programs start by executing the content of the main function. However, this program does not do anything because the main function does not have instructions. Facts: An open and close parenthesis is placed after the function name. Parentheses are used to hold the function arguments. The type of the value returned by the function is specified before the function name. Void is a C# type specifying a typeless type. The body of the function is written within the function block specified by the open and close curly braces. The C# programming language is a free format language.

1.3 Displaying a Message


The canonical hello, world program can be written as follows: using System; class Hello { static void Main() { Console.WriteLine(hello, world); } } The source code for a C# program is typically stored in one or more text files with a file extension of .cs, as in hello.cs. Using the command-line compiler, such a program can be compiled with the command line directive csc hello.cs which produces an application named hello.exe. The output produced by this application when it is run is: hello, world Close examination of this program is illuminating: The using System; directive references a namespace called System. This namespace contains the Console class referred to in the Main method. Namespaces provide a hierarchical means of organizing the elements of one or more programs. A using directive enables unqualified use of the types that are DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

members of the namespace. The hello, world program uses Console.WriteLine as shorthand for System.Console.WriteLine. (For the sake of brevity, most examples in this specification omit the using System; directive.) The Main method is a member of the class Hello. The entry point for an applicationthe method that is called to begin executionis always a static method named Main. The hello, world output is produced using a class library. The language does not itself provide a class library. Instead, it uses a class library that is also used by other programming languages.

1.4 Comments
Two forms of comments are supported: single-line comments and delimited comments. Single-line comments start with the characters // and extend to the end of the source line. Delimited comments start with the characters /* and end with the characters */. Delimited comments may span multiple lines. Comments do not nest. The character sequences /* and */ have no special meaning within a // comment, and the character sequences // and /* have no special meaning within a delimited comment. The example below includes a delimited comment. /* Hello, world program This program writes hello, world to the console */ class Hello { static void Main() { Console.WriteLine(hello, world); } } The following example shows several single-line comments. // Hello, world program // This program writes hello, world to the console // class Hello // any name will do for this class { static void Main() { // this method must be named Main Console.WriteLine(hello, world); } }

1.5 Multiple Instructions Program


A function is a block of code written to perform a specific task (function) and packaged in a unit so that it can be executed at desired points in a program. Dividing a program into separate functions makes the program more manageable and easier to understand.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

class hello { static void Main() { DisplayHello();DisplayWorld(); DisplayHelloWorld(); } /* Function definition */ static void DisplayHello() { Console.WriteLine("Hello"); } static void DisplayWorld() { Console.WriteLine("World"); } static void DisplayHelloWorld() { Console.WriteLine("Hello World"); } }

Output

Hello World Hello World

Facts: The program contains three user functions. Functions need to be defined before being used or executed. The main function contains three function calls or three statements. User functions are declared outside the main function. Functions can be called many times. The program starts by executing the first instruction in Main, which is the DisplayHello function. When the DisplayHello function is called, the execution flow changes to the first instruction within the function definition of DisplayHello. When the last instruction (which is also the first instruction) of the function DisplayHello is executed, the execution returns to the instruction right after the function call, which is DisplayWorld.

1.6 Functions with Argument Returning a Value


A function contains the parameters and the return type. The parameters provide data needed by the function to do its job. For each parameter specify its name and data type; if no parameters are needed, specify the keyword void inside the parentheses. The return type specifies the data type of the data that is returned by the function. Some functions do not return any data; in this case specify the keyword void as the return type. Example: void foo(); void foo(int i); int foo(); int foo(int i);

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

class hello { static void Main( ) { Console.WriteLine("{0}",Add(3,5)); } /* Function definition */ static int Add(int x, int y) { return x+y; } }
Output

Facts: int is a C# type specifying whole or integral numbers. int means integer. The function prototype specifies that the function takes two integer arguments and returns an integer. When a function has more than one argument, a comma is used to separate the arguments. When a function with arguments is called, the arguments are received as parameters by the function where the functions instructions are specified. When arguments are passed to the parameters, the order of the arguments is respected. In our case, x would be equal to 3 and y would be equal to 5. The last statement of the function definition returns the result of the arithmetic expression x + y. x and y are called variables. A variable is a name assigned to a data storage location. The variables x and y are defined in the parameter list of the function: (int x, int y). In C#, a variable must be defined before it can be used. A variable definition specifies its name and type. The compiler uses the type in order to know how much memory to allocate.

1.7 Variables

class add { static void Main( ) { int i,j; i=3; j=5; Console.WriteLine("{0}", Add(i,j)); } static int Add(int x, int y) { return x+y; } }
Output

Facts: Two integer variables i and j are declared and defined. By declaration, we mean that the rest of the function main knows about the presence and type of i and j. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

class test { static void Main( ) { int x1=2,y1=1,x2=7,y2=3; Console.WriteLine("{0}", Distance(x1,y1,x2,y2)); } static double Distance(int x1, int y1, int x2, int y2) { int deltaX, deltaY; deltaX=x2-x1; deltaY=y2-y1; return Math.Sqrt(deltaX*deltaX + deltaY*deltaY); } }

In other words, the scope and type of i and j is within the body of function main. Then, an assignment operator is used in order to assign the value 3 and 5 to i and j respectively. The function Add is used by having two variables as arguments, while in the previous example the arguments were constants.

1.8 User Input


An address is a number that is assigned to each byte in a computers memory. It is used to track where data and instructions are stored in memory. Each byte is assigned an address whether or not it is being used to store data. Facts: Instead of using the assignment operator to assign value to the variables, the input function ReadLine is used. This is an arithmetic expression.
class test { static void Main() { int i,j; Console.Write("Enter i: "); i = Int32.Parse(Console.ReadLine()); Console.Write("Enter j: "); j = Int32.Parse(Console.ReadLine()); Console.WriteLine(Add(i,j)); } static int Add(int x, int y) { return x+y; } }

Output

5.3851648071345

Facts: Four variables of type integer are declared, defined, and assigned a value. int x1=2,y1=1,x2=7,y2=3; In C#, you can assign an initial value to a variable during the declaration. The function Distance takes four integer arguments and returns a value of type double. The C# double type allows the variable to have a value with fraction. A value with fraction is made from an integral part followed by a decimal point followed by the precision DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview (5.3851648071345). The function Distance requires a square root calculation. The square root function Sqrt is defined in the namespace called Math. The function Distance declares and defines six variables: x1,y1,x2,y2,deltaX, and deltaY. Remember that variables declared inside a function are only available while executing the function. deltaX and deltaY are used to hold the difference between the first and the second point. The statement deltaX=x2-x1; subtract x1 from x2 then assign the result to deltaX. In C# arithmetic, the multiplication * operator is evaluated from left to right. The multiplication operator has a higher order of precedence than the addition operator. This is why deltaX*deltaX is evaluated first to 25. Next deltaY*deltaY is evaluated to 4. Then 25 and 4 are added evaluating to 29. Then the square root of 29 is evaluated to 5.3851648071345.

10

1.9 Conditional Expression


class test { static void Main() { int i; string s; Console.WriteLine("Enter a number then press return:"); i = Int32.Parse(Console.ReadLine()); if(i>100) Console.WriteLine("{0} is larger than 100", i); else Console.WriteLine("{0} is less or equal than 100",i); } }
Output

Enter a number then press return: 60 60 is less or equal than 100

Facts: i>100 is the Boolean expression that evaluates to true or false. If the expression is true, then the statement (or the block of statements enclosed between an opening and a closing curly braces) following the condition is executed. If the expression is false then the statement following the condition is skipped. The statement following the else is executed only when the conditional expression is false. In other words, only one of the WriteLine statements will be executed. The conditional statement starts with the keyword if followed by an opening parenthesis, followed by a Boolean expression, followed by a closing parenthesis, followed by an instruction: if(Boolean expression) instruction; Notice that there is no semicolon after the closing parenthesis of the Boolean expression because the conditional statement has not ended yet.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

11

1.10 Loops

class test { static void Main() { int i; for(i=0;i<10;i=i+1) Console.Write("{0} ",i); Console.Write("\n"); i=0; while(i<10) { Console.Write("{0} ",i); i=i+1; } } }

Output

0123456789 0123456789

Facts: The for loop form is: f o r ( e x p r e s s i o n 1 ; e x p r e s s i o n 2 ; e x p r e s s i o n 3 ) statement The loop is initialized through expression1 i=0; Expression2 specifies the test made before each iteration i<10; If expression2 is true, the statement Console.Write({0} ,i); is executed, then expression 3 i=i+1 is executed. The loop iterates until expression2 is false. If expression2 is false, the for loop will exit, and the control is transferred to the statement following the statement. Expression3 is evaluated after each iteration. Any or all of the three for expressions may be omitted, but the semicolon must remain. The while loop form is: while (expression) statement If expression i<10 is true, the statement is executed until expression becomes false. In our case the statement is made from a block enclosed between curly braces: { Console.WriteLine({0} ,i); i=i+1; } If expression is false, the execution resumes at the following statement. In our case, the following statement is the end of the program. The expression is evaluated before the statement is executed. When the expression is false from the first time, the statement will never be executed.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

12

1.11 One-Dimensional Array


class test { static void Main() { int i; int[] A = new int[10]; for(i=0;i<10;i=i+1) A[i]=i; for(i=0;i<10;i=i+1) Console.Write("{0} ",A[i]); } }

Output

0123456789

Facts: It is a collection of variables of the same type that are referred to by the same name. In our case int[] A = new int[10]; reserved 10 integers. Array elements are accessed through an index; in our case the index is i. The first element is accessed by index 0 A[0]. The highest address corresponds to the last element and it is accessed by index (total number of elements 1); in our case it is A[9]. The amount of storage required to hold an array depends on the type and the total number of elements; in our case it is 10 * 4=40, since each integer is 4 bytes. The C# compiler does not perform index range checking. The array element is accessed by indexing the array name. It is done by writing the index enclosed between brackets placed after the array name. arrayName[index] A[i]=i;

1.12 Structure
class test { struct point { public int x; public int y; }; static void Main() { point p1, p2; p1.x=2; p1.y=1; p2.x=7; p2.y=3; Console.WriteLine(Distance(p1,p2));/* will print 5.3851648071345 */ } static double Distance(point p1,point p2) { int deltaX, deltaY; deltaX=p2.x - p1.x; deltaY=p2.y - p1.y; return Math.Sqrt(deltaX*deltaX + deltaY*deltaY); } }

Facts: A structure is an object consisting of a sequence of named members of various types. It is a collection of variables referenced under one name. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Chapter One: Overview Programming The collection of variables is logically related. It provides a convenient way in order to keep related information together.

13

A structure is declared by typing the keyword struct, followed by the structure name, followed by the structure members enclosed between curly braces; for example: struct point { public int x, y; } Once a structure is declared, variables having the structure type could be declared by typing the structure name followed by the variable name; for example: int deltaX; The dot . operator is used to access the structure member. First write the structure variable name, followed by a dot, followed by a member; for example: deltaX=p2.x - p1.x;

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

14

2 C# Types
2.1 Value Types
In C#, a value type can be either a struct or an enumeration. C# contains a set of predefined struct types called the simple types. These simple types are identified through reserved words. All value types implicitly inherit form a class called object. Also, no type can derive from a value type. It is not possible for a value type to be null (null means nothing or no value). Assigning a variable of a value type creates a copy of the value. This is null different from assigning a variable of a reference type, which copies the reference and not the object identified by the reference.

2.1.1 bool
The bool type represents boolean quantities. There can be two possible values of type bool: true and false. There is no standard conversion between bool and other types. Such conversions are accomplished by comparing an integral value to zero or comparing an object to null. A boolean value can be assigned to a bool variable, for example: bool MyBool = true; You can also assign an expression that evaluates to a bool variable, for example: bool b = (i > 66); Conversions No conversion exists between the bool type and other types. For example, the following if statement: int i = 101; if (i) { } is not allowed in C#. To test an int type, you have to explicitly compare it to a value, as follows: int i = 13; if (i == 13) { // do something } Example: In this example, you enter a character from the keyboard and the program checks if the input character is a letter. public class test { static void Main() { Console.Write(Enter a character: ); char ch = (char) Console.Read(); if (Char.IsLetter(ch)) Console.WriteLine(It is an alphabetic character.); DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview else Console.WriteLine(It is not an alphabetic character.);

15

Output Enter a character: A It is an alphabetic character.

2.1.2 byte
The byte keyword denotes an integral type that stores values ranging from 0 to 255. Its size is 8-bit. A byte can be declared and initialized as follows: byte b = 117; In the preceding declaration, 117 is implicitly converted from int to byte. If the integer literal exceeds the range of byte, a compilation error will occur, as with the following assignment statement: byte c = a + b; // Error: conversion from int to byte To fix this problem, use an explicit cast: byte c = (byte)(a + b); // OK

2.1.3 char
char is used to declare a Unicode character in the range 0 to 65535. Unicode characters are 16-bit characters used to represent most of the written languages throughout the world. The following statement declares a char variable and initializes it with the character D: char c = D;

2.1.4 decimal
decimal denotes a 128-bit data type. The decimal type has a greater precision and a smaller range than the floating-point type, which makes it suitable for financial and monetary calculations. To make sure that a numeric real number is treated as a decimal, use the suffix m or M: decimal dec = 710.88m; Without the suffix m, the number is treated as a double, and the expression generates a compilation error. Example: public class test { static void Main () { decimal dec = 12.4m; int i = 33; Console.WriteLine(dec * i); } }

// Here the result is converted to decimal

Formatting Decimal Output You can format the results by using the String.Format method, or through the Console.Write method, which calls String.Format(). The currency format is specified using the standard currency format string C or c. Example: In this example, the output is formatted using the currency format string. public class test DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview { {

16

static void Main () decimal dec1 = 0.987m; decimal dec2 = 5454335566m; Console.WriteLine({0:C}, dec1); Console.WriteLine({0:C}, dec2);

Output: $1.00 $5454335566.00

2.1.5 double
double denotes a simple type that stores 64-bit floating-point values. By default, a real numeric literal on the right-hand side of the assignment operator is treated as a double. However, if you want an integer number to be treated as a double, use the suffix d or D. double x = 44D; Numeric integral types and floating-point types can be mixed in an expression. In this case, the integral types are converted to floating-point types. Example: class test { static void Main() { float f = 8.66f; int i = 123; double d = 22.1E+2; Console.Write({0}, f + i + d); } }

2.1.6 enum
enum is used to declare an enumeration, which is distinct type consisting of a set of constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example: enum WeekDays {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; In this enumeration, Sun is 0, Mon is 1, and so forth. Enumerators can have initializers overriding the default values, as the following example shows: enum WeekDays {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat}; In this enumeration, the sequence starts from 1. An enum En has a default value, which is the value produced by the expression (En)0. The enumeration type specifies how much storage is allocated. However, an explicit cast is needed to convert from enum type to an integral type. Example: In this example, an enumeration, Days, is declared. Two enumerators are explicitly converted to int and assigned to int variables. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview public class test { enum WeekDays {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat}; static void Main() { int i1 = (int) WeekDays.Tue; int i2 = (int) WeekDays.Thu; Console.WriteLine(Tuesday is day {0}, i1); Console.Write(Thursday is day {0}, i2); } } Output: Tuesday is day 3 Thursday is day 5

17

2.1.7 float
float denotes a type that can store 32-bit floating-point values. A real numeric literal on the right-hand side of the assignment operator is treated by default as a double. Therefore, to initialize a float, use the suffix f or F, for example: float f = 68.77F; You will get a compilation error if you do not use the suffix because you are attempting to store a double value into a floating point variable. You can mix numeric integral types and floating-point types in an expression. In this case, the integral types are converted to floating-point types. Example: class test { static void Main() { int i = 14; float f = 68.25f; Console.Write({0}, i-f); } }

2.1.8 int
int denotes an integral type that stores 32-bit values. It ranges from -2,147,483,648 to 2,147,483,647. The type int is declared and initialized like this: int i = 441;

2.1.9 long
long denotes an integral type that stores 64-bit values. It ranges from 9,223,372,036,8 54,775,808 to 9,223,372,036,854,775,807. The type long is declared and initialized like this: long myLong = 23940043; When an integer literal has no suffix, its type is the first of the following types in which its value can fit: int, uint, long, ulong. The suffix L can be used with the long type like this: long myLong = 990085665543L; When you use the suffix L or l, the literal integers type is either long or ulong according to its size. A predefined implicit conversion exists from long to float, double, or decimal. In other cases, a cast must be DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview used. For example, the following statement will produce a compilation error without an explicit cast: int i = 21L;

18

There is an implicit conversion from sbyte, byte, short, ushort, int, uint, or char to long. Also, there is no implicit conversion from floating-point types to long. For example, the following statement generates an error: long l = 31.23;

2.1.10 sbyte
sbyte denotes an integral type that stores signed 8-bit integer values, ranging from -128 to 127. An sbyte can be declared and initialized like this: sbyte mySbyte = 100; 100 is implicitly converted from int to sbyte. If the integer literal exceeds the range of sbyte, a compiler error will occur. A predefined implicit conversion exists from sbyte to short, int, long, float, double, or decimal. Also, there is no implicit conversion from floating-point types to sbyte.

2.1.11 short
short denotes an integral data type that stores signed 16-bit integer values, ranging from -32,768 to 32,767. A short is declared and initialized like this: short s = 30201; 30201 is implicitly converted from int to short. If the integer literal does not fit into a short storage location, a compiler error will occur. A predefined implicit conversion exists from short to int, long, float, double, or decimal. You cannot implicitly convert non-literal numeric types of larger storage size to short. Also there is no implicit conversion from floating-point types to short.

2.1.12 struct
A struct is a value type. It can contain constructors, constants, fields, methods, properties, indexers, operators, and nested types. The struct type is suitable for representing objects such as Point, Rectangle, and Color. When creating a struct object using the new operator, it gets created, and the appropriate constructor is called. Structs can be instantiated without using the new operator. If you do not use the new operator, the fields will remain unassigned and the object cannot be used until all of the fields are initialized. You cannot declare a class using the keyword struct. Classes and structs are semantically different. A struct is a value type, while a class is a reference type. Example: public struct Point { public int x, y; } Example: This example creates a Point object without using the new operator. public struct Point { public int x, y; } class test { static void Main() { DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview Point p1;

19

2.1.13 uint
uint denotes an integral type that stores unsigned 32-bit integer values, ranging from 0 to 4,294,967,295. An uint can be declared and initialized like this: uint myUint = 4294967288; The suffix u or U can be used, like this: uint myUint = 112U; If you use the suffix U or u, the literal type is determined to be either uint or ulong according to its size. In this example, it is uint. A predefined implicit conversion exists from uint to long, ulong, float, double, or decimal. For example: float myFloat = 4294967289; Also, there exists a predefined implicit conversion from byte, ushort, or char to uint. Otherwise you must use a cast. There is no implicit conversion from floating-point types to uint.

2.1.14 ulong
The ulong keyword denotes an integral type that stores unsigned 64-bit integer values, ranging from 0 to 18,446,744,073,709,551,615. A ulong is declared and initialized like this: ulong myUlong = 92854775806; When using L or l as a suffix, the type of the literal integer will be either long or ulong according to its size. There is an implicit conversion from ulong to float, double, or decimal, but there is no implicit conversion from ulong to any integral type.

2.1.15 ushort
ushort denotes an integral data type that stores unsigned 16-bit integer values, ranging from 0 to 65,535. ushort can be declared and initialized like this: ushort myUShort = 65535; 65535 is implicitly converted from int to ushort. A compiler error will occur if the integer literal exceeds the range of ushort. A predefined implicit conversion exists from ushort to int, uint, long, ulong, float, double, or decimal. Also, there is a predefined implicit conversion from byte or char to ushort. Otherwise a cast must be used. There is no implicit conversion from floating-point types to ushort.

2.2 Reference Types


A reference type is one of the following: class, interface, array, or delegate. A reference type value is a reference to an instance of the type. null is compatible with all reference types and indicates the absence of an instance. Class Types A class defines a data structure containing data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors, and static constructors), and nested types. Object Types The object class type is the ultimate base class of all other types. Every type in C# directly or indirectly derives from the object class type. String Types DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

20

The string type inherits directly from class object. Interface Types An interface defines a contract. A class implementing an interface must adhere to its contract. Array Types An array is a data structure containing a number of variables, which are accessed through indices. The variables contained in an array are called the elements of the array. They are all of the same type, and this type is called the element type of the array.

2.3 void
When used as the return type for a method, void specifies that the method does not return a value. void is not allowed in a methods parameter list. A method with no parameters and returning no value is declared as follows: void MyMethod();

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

21

3 Variables
3.1 Introduction
In C#, a variable represents a storage location. A variable has a type that determines what values can be stored in this variable. Because C# is a type-safe language, the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable is changed through the assignment operator. The value of a variable is also changed through the use of the ++ and -- operators. A variable must be definitely assigned before its value can be obtained: variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value. An initially unassigned variable has no initial value.

3.2 Categories of Variables in C#


In C#, there are seven categories of variables: static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables. The following sections describe each of these categories.

3.2.1 Static Variables


When declaring a variable with the static keyword, it is called a static variable. The initial value of a static variable is the default value of the variables type. A static variable is initially assigned.

3.2.2 Instance Variables


A variable declared without the static keyword is called an instance variable. An instance variable of a class exists when a new instance of that class is created, and ceases to exist when there are no references to that instance and the instances destructor (if any) has executed. The initial value of an instance variable of a class is the default value of the variables type. An instance variable of a class is initially assigned.

3.2.3 Array Elements


The elements of an array exist when an array instance is created, and cease to exist when there are no references to that array instance. The initial value of each of the elements of an array is the default value of the type of the array elements. An array element is initially assigned.

3.2.4 Value Parameters


A parameter declared without a ref or out modifier is a value parameter. A value parameter is initially assigned.

3.2.5 Reference Parameters


A parameter declared with a ref modifier is a reference parameter. This represents the same storage location as the variable given as the argument in the function member invocation. Therefore, the value of a reference parameter is always the same as the underlying variable. A variable has to be definitely assigned before it can be passed as a reference parameter in a function member invocation. A reference parameter is considered initially assigned within a function member.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

22

3.2.6 Output Parameters


An output parameter is a parameter declared with an out modifier. An output parameter represents the same storage location as the variable given as the argument in the function member invocation. Therefore, the value of an output parameter is always the same as the underlying variable. A variable does not need to be definitely assigned before it can be passed as an output parameter in a function member invocation. Within a function member, an output parameter is unassigned initially.

3.2.7 Local Variables


A local variable is declared within a block, a for-statement, a switch-statement, or a using-statement. The lifetime of a local variable is implementation-dependent. For example, the compiler could generate code that results in the variables storage having a shorter lifetime than its containing block. A local variable is not automatically initialized and has no default value. It also is unassigned initially. A compile-time error results if the local variable is referred a position that precedes its declaration.

3.3 Default Values


Static variables, instance variables of class instances, and array elements are automatically initialized to their default values. The default value of a variable depends on the type of the variable.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

23

4 Expressions, Statements & Operators


What is an expression? It is a sequence of operators and operands. It specifies computation of a value, or It designates a variable or constant.

4.1 Classification of Expressions


An expression is classified as one of the following: A value. A variable. A namespace. A type. A method group. A property access. An event access. An indexer access. Nothing.

The final result of an expression cannot be one of the following: A namespace. A type. A method group. An event access.

These categories are intermediate constructs. They are only permitted in certain contexts. Values of Expressions If the expression denotes a property access, an indexer access, or a variable, the value of the property, indexer, or variable is implicitly substituted: The value of a variable is simply the value currently stored in the storage location identified by the variable. The value of a property access expression is obtained by invoking the get-accessor of the property. The value of an indexer access expression is obtained by invoking the get-accessor of the indexer.

4.2 Operators
Expressions are constructed from operands and operators. Operators of an expression indicate which operations to apply to the operands. We can find three types of operators: 1. Unary Operators: The unary operators take one operand. They use either prefix notation (i) or postfix notation (i++). 2. Binary Operators: The binary operators take two operands. They all use infix notation (i + j). 3. Ternary Operator: There exists only one ternary operator, ?:. The ternary operator takes three operands and uses infix notation (z? x: y). Operands in an expression are evaluated from left to right. Certain operators can be overloaded. This permits user-defined operator implementations to be specified for operations where one or both of the operands are of a DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview user-defined class or struct type.

24

Operator Precedence and Associativity When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the expression a + b * c is evaluated as a + (b * c) because the * operator has higher precedence than the + operator. The precedence of an operator is established by the definition of its associated grammar production. When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed: All binary operators are left-associative, except for the assignment operators, meaning that operations are performed from left to right. Assignment operators and conditional operator (?:) are right-associative, which means that operations are performed from right to left. Precedence and associativity can be controlled using parentheses. Operator Overloading User-defined implementations can be introduced by including operator declarations in classes and structs. User-defined operator implementations always take precedence over predefined operator implementations only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered. The overloadable unary operators are: + ! ~ ++ -true false The overloadable binary operators are: + - * / % & | ^ << >> == != > < >= <=

When a binary operator is overloaded, the corresponding assignment operator (if any) is also implicitly overloaded. For example, an overload of operator + is also an overload of operator +=. The assignment operator (=) cannot be overloaded. An assignment performs a bit-wise copy of a value into a variable. Element access, such as Ar[x], is not an overloadable operator. User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration. User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator. For example, the / operator is always a binary operator, always has the precedence level specified in, and is always left-associative.

4.3 Function Members


Function members are members that contain executable statements. They are always members of types and cannot be members of namespaces. C# defines the following categories of function members: Methods Properties Events Indexers User-defined operators Instance constructors Static constructors Destructors

Statements contained in function members are executed through function member invocations. The argument list of a function member invocation provides actual values or variable references for the parameters of the function member. The new Operator The new operator is used to create new instances of types. There are three forms of new expressions: DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

25

1. Object creation expressions are used to create new instances of class types and value types. 2. Array creation expressions are used to create new instances of array types. 3. Delegate creation expressions are used to create new instances of delegate types. The new operator implies creation of an instance of a type. Instances of value types require no additional memory beyond the variables in which they reside.

4.4 Unary Operators


The unary operators are +, -, !, ~, *, ++, --, and cast operators. The - Operator The predefined plus operators are: int operator +(int x); uint operator +(uint x); long operator +(long x); ulong operator +(ulong x); float operator +(float x); double operator +(double x); decimal operator +(decimal x);

For each of these operators, the result is simply the value of the operand. The - Operator The predefined negation operators are: 1. Integer negation: int operator (int x); long operator (long x); The result is computed by subtracting x from zero. 2. Floating-point negation: float operator (float x); double operator (double x); The result is the value of x with its sign inverted. 3. Decimal negation: decimal operator (decimal x); The result is computed by subtracting x from zero. Decimal negation is equivalent to using the unary minus operator of type Decimal. 4. Logical negation operator: There is only one predefined logical negation operator: bool operator !(bool x); This operator computes the logical negation of the operand: if the operand is true, the result is false. If the operand is false, the result is true. 5. Bitwise complement operator: The bitwise complement operators are: int operator ~(int x); uint operator ~(uint x); long operator ~(long x); ulong operator ~(ulong x); 6. Prefix increment and decrement operators: DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

26

Pre-increment-expression: ++ unary-expression Pre-decrement-expression: -unary-expression The value returned by the operator becomes the result of the operation. The ++ and -- operators also support postfix notation. Cast Expressions A cast-expression is used to explicitly convert an expression to a given type. Example: ( type ) unary-expression

4.5 Arithmetic Operators


The *, /, %, +, and operators are called the arithmetic operators. multiplicative-expression: unary-expression multiplicative-expression * unary-expression multiplicative-expression / unary-expression multiplicative-expression % unary-expression additive-expression: multiplicative-expression additive-expression + multiplicative-expression additive-expression multiplicative-expression Multiplication Operator Integer multiplication: int operator *(int x, int y); uint operator *(uint x, uint y); long operator *(long x, long y); ulong operator *(ulong x, ulong y);

Floating-point multiplication: float operator *(float x, float y); double operator *(double x, double y); Decimal multiplication: decimal operator *(decimal x, decimal y); Division Operator Integer division: int operator /(int x, int y); uint operator /(uint x, uint y); long operator /(long x, long y); ulong operator /(ulong x, ulong y);

Floating-point division: float operator /(float x, float y); double operator /(double x, double y); Decimal division: decimal operator /(decimal x, decimal y); Remainder Operator DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview Integer remainder: int operator %(int x, int y); uint operator %(uint x, uint y); long operator %(long x, long y); ulong operator %(ulong x, ulong y); Floating-point remainder: float operator %(float x, float y); double operator %(double x, double y); Decimal remainder: decimal operator %(decimal x, decimal y); Addition Operator Integer addition: int operator +(int x, int y); uint operator +(uint x, uint y); long operator +(long x, long y); ulong operator +(ulong x, ulong y); Floating-point addition: float operator +(float x, float y); double operator +(double x, double y); Decimal addition: decimal operator +(decimal x, decimal y); Enumeration addition: E operator +(E x, U y); E operator +(U x, E y); String concatenation: string operator +(string x, string y); string operator +(string x, object y); string operator +(object x, string y); Subtraction Operator Integer subtraction: int operator (int x, int y); uint operator (uint x, uint y); long operator (long x, long y); ulong operator (ulong x, ulong y); Floating-point subtraction: float operator (float x, float y); double operator (double x, double y); Decimal subtraction: decimal operator (decimal x, decimal y); Enumeration subtraction: U operator (E x, E y); E operator (E x, U y); DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

27

Game Web Cast Project: C# Programming Overview

28

4.6 Shift Operators


The << and >> operators are used to perform bit shifting operations. shift-expression: additive-expression shift-expression << additive-expression shift-expression >> additive-expression When declaring an overloaded shift operator, the type of the first operand must always be the class or struct containing the operator declaration, and the type of the second operand must always be int. The predefined shift operators are listed below. Shift Left int operator <<(int x, int count); uint operator <<(uint x, int count); long operator <<(long x, int count); ulong operator <<(ulong x, int count); The << operator shifts x left by a number of bits computed as described below. The high-order bits outside the range of the result type of x are discarded, the remaining bits are shifted left, and the low-order empty bit positions are set to zero. Shift Right int operator >>(int x, int count); uint operator >>(uint x, int count); long operator >>(long x, int count); ulong operator >>(ulong x, int count); The >> operator shifts x right by a number of bits computed as described below.

4.7 Relational & Type Testing Operators


The ==, !=, <, >, <=, >=, is and as operators are called the relational and type testing operators. Relational-Expression shift-expression relational-expression < shift-expression relational-expression > shift-expression relational-expression <= shift-expression relational-expression >= shift-expression relational-expression is type relational-expression as type Equality-Expression relational-expression equality-expression == relational-expression equality-expression != relational-expression All comparison operators return a result of type bool, as described in the following table:

Operation Result
x == y x != y x < y true if x is equal to y, false otherwise true if x is not equal to y, false otherwise true if x is less than y, false otherwise DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

29

x > y x <= y x >= y

true if x is greater than y, false otherwise true if x is less than or equal to y, false otherwise true if x is greater than or equal to y, false otherwise

The as Operator This operator is used to explicitly convert a value to a given reference type using a reference conversion or a boxing conversion. The as operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is null.

4.8 Logical Operators


The &, ^, and | operators are called the logical operators. and-expression: equality-expression and-expression & equality-expression exclusive-or-expression: and-expression exclusive-or-expression ^ and-expression inclusive-or-expression: exclusive-or-expression inclusive-or-expression | exclusive-or-expression Conditional Operator The ?: operator is called the conditional operator. It is sometimes called the ternary operator. A conditional expression of the form b? x: y first evaluates the condition b. Then, if b is true, x is evaluated and becomes the result of the operation. Otherwise, y is evaluated and becomes the result of the operation. A conditional expression never evaluates both x and y. The conditional operator is right-associative, meaning that operations are grouped from right to left. The first operand of the ?: operator must be an expression of a type that can be implicitly converted to bool, or an expression of a type that implements operator true. If neither requirement is satisfied, a compile-time error occurs. The second and third operands of the ?: operator control the type of the conditional expression. Assignment Operators Assignment operators assign a new value to a variable, a property, or an indexer element. The left operand of an assignment must be an expression classified as a variable, a property access, or an indexer access. The = operator is called the simple assignment operator. It assigns the value of the right operand to the variable, property, or indexer element given by the left operand. The operators formed by prefixing a binary operator with an = character are called the compound assignment operators. These operators perform the indicated operation on the two operands, and then assign the resulting value to the variable, property, or indexer element given by the left operand. The assignment operators are right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a = b = c is evaluated as a = (b = c). Simple Assignment The = operator is called the simple assignment operator. In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand. The operation assigns the value of the right operand to the variable, property, or indexer element given by the left operand. The result of a simple assignment expression is the value assigned to the left operand. The result has the same type as the left operand and is always classified as a value. If the left operand is a property or indexer access, the property or indexer must have a set accessor. If this is not the case, a compile-time error occurs. Boolean Expressions A boolean-expression is an expression that yields a result of type bool. The controlling conditional expression of an if-statement, while-statement, do-statement, or for-statement is a boolean-expression. The controlling conditional expression of the ?: operator follows the same rules as a boolean-expression, but for reasons of DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

30

operator precedence is classified as a conditional-or-expression. A boolean-expression is required to be of a type that can be implicitly converted to bool or of a type that implements operator true. If neither requirement is satisfied, a compile-time error occurs. When a boolean expression is of a type that cannot be implicitly converted to bool but does implement operator true, then following evaluation of the expression, the operator true implementation provided by that type is invoked to produce a bool value.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

31

5 Functions
5.1 Definition
The definition of a function includes the return type, the function name, the parameters list, and the function body. The function body is enclosed between an opening and a closing brace. Example: int add(int a, int b) { return a + b; } The parameters a and b receive the values of the arguments when the function is called. Example: int n; n = add(4,5);

5.2 Scope
The code found in a function is private to the function and cannot be accessed by any statement from another function. Function code is accessed only through function call. A C# program starts from the function Main. All functions have a file scope. Parameters and variables declared inside the function have function scope. They are created when the function is entered and destroyed when the function ends. Static variables declared inside the function have a function scope, but they retain their values between function calls.

5.3 Calling a Function


A function is called by using its name followed by an opening and closing parenthesis including the arguments, if any. If the function has no arguments, then the argument list is left empty. When a function is called, the function parameters receive the value of the arguments by respecting their order. Parameter1 receives the value of argument1, and ParameterN receives the value of argumentN. When a function is called, the sequential execution flow is interrupted, and the program counter jumps to the first statement of the function called. Once the last statement is executed, or a return instruction is executed, the sequential execution flow is interrupted and the program counter is updated so it points to the next instruction after the function call. The return instruction or statement also returns a value since the function has a returning type. In other words, the function could be used as value specified by its type.

5.4 return Statement Example


class test {

struct box { public float left; public float top; public float right; public float bottom; }; static float Maximum(float v1, float v2) { DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview if(v1>v2) return v1; else return v2;

32

static float Minimum(float v1, float v2) { if(v1<v2) return v1; else return v2; } static bool Intersect(float v1, float v2) { if(v1-v2<=0) return true; else return false; } static void Main() { box b1, b2; Console.WriteLine(Enter the left, top, right, and bottom coordinates of b1:); /* the input from keyboard is stored in the structure members */ b1.left = Int32.Parse(Console.ReadLine()); b1.top = Int32.Parse(Console.ReadLine()); b1.right = Int32.Parse(Console.ReadLine()); b1.bottom = Int32.Parse(Console.ReadLine()); Console.WriteLine(Enter the left, top, right, and bottom coordinates of b2:); b2.left = Int32.Parse(Console.ReadLine()); b2.top = Int32.Parse(Console.ReadLine()); b2.right = Int32.Parse(Console.ReadLine()); b2.bottom = Int32.Parse(Console.ReadLine()); if(Intersect(Maximum(b1.left,b2.left)-Minimum(b1.right,b2.right), Minimum(b1.bottom,b2.bottom)-Maximum(b1.top,b2.top))) Console.WriteLine(b1 and b2 intersect\n); else Console.WriteLine(b1 and b2 do not intersect\n); } }

5.5 Call by Value Function Arguments


Arguments can be passed in two ways: Call by value and call by reference. The call by value method copies the value of the argument into the function parameter. Therefore, changes made to the parameter have no effect on the argument. In other words, the function code cannot modify the arguments used to call the function. Example: class test { static void Main() DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview {

33

function: x={0},x); } static void callByValue(int x) { x=20; } }

int x; x=10; callByValue(x); Console.WriteLine(The argument x was not modified by the

5.6 Call by Reference Function Arguments


To pass a parameter by reference, use the ref or out keyword. A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref (or out) keyword. Example: class test { static void Main() { int x=10, y=20; Console.WriteLine(Before calling swap: x={0}, y={1},x,y); /* call by reference arguments*/ swap(ref x,ref y); Console.WriteLine(After calling swap: x={0}, y={1},x,y); } /* call by reference parameters*/ static void swap(ref int p1,ref int p2) { int tmp; tmp = p1; /*Saving p1 in tmp*/ p1 = p2; /*p1 takes the value of p2*/ p2 = tmp; /*p2 takes the value of tmp*/ } }

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

34

5.7 Function Returning Value Example


class test {

static void Main() { int i; Console.Write(Enter an integer value: ); i = Int32.Parse(Console.ReadLine()); Console.WriteLine(The absolute value of {0} is {1}, i, Absolute(i)); } /* the function receives an integer and returns its absolute value */ static int Absolute(int i) { return i>=0? i:-i; }

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

35

6 Flow Control
6.1 The while Statement
The while statement executes a block of statements repeatedly until a specified expression evaluates to false. It has the form while (expression) statement where expression is an expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators. The expression is used to test the loop-termination criteria. statement is the statement(s) that will be executed. A while loop executes zero or more times because the test of expression takes place before each execution of the loop. A while loop can terminate when a break, goto, return, or throw statement transfers control outside the loop. To pass control to the next iteration without exiting the loop, use the continue statement. Example: using System; class Test { static void Main() { int i = 16; while (n > 0) { Console.WriteLine(Another value of i: {0}, i); i -= 4; } } }

6.2 The do-while Statement


The do statement executes a block of statements repeatedly until a specified expression evaluates to false. It is in the following form: do statement while (expression); where expression is an expression that can be implicitly converted to bool. statement is the statement(s) to be executed. Regardless of the value of expression, the body of the do statement is executed at least once. Example: using System; public class Test { static void Main () { int i = 16; do { Console.WriteLine(Another value of i: {0}, i); i -= 4; } } while(i > 0);

} Example:

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview using System; public class Test { static void Main () { int i = 16; do { Console.WriteLine(Another value of i: {0}, i); i -= 4; } } while(i > 18);

36

In the preceding example, although the condition evaluates initially to false, the loop will be executed once.

6.3 The for Loop


The for loop executes a block of statements repeatedly until a specified expression evaluates to false. It is in the following form: for ([initializers]; [expression]; [iterators]) statement where initializers is a comma separated list of expressions or assignment statements that will initialize the loop, expression is an expression that can be implicitly converted to bool, iterators is an expression statement(s) that increment or decrement the loop counters, and statement is the embedded statement(s) to be executed. The for statement executes the statement repeatedly as follows: First, the initializers are evaluated. Then, while the expression evaluates to true, the statement(s) are executed. When expression evaluates to false, control is transferred outside the loop. A for statement executes zero or more times because the test of expression takes place before the execution of the loop. All of the expressions of the for statement are optional. Example: using System; public class Test { static void Main() { for (int i = 6; i >0; i--) Console.Write(i); } }

6.4 The foreach, in Statement


The foreach statement repeats a group of statements for each element in an array. The foreach statement is used to iterate through the array to get the desired information, but should not be used to change the contents of the array. The statement is in the following form: foreach (type identifier in expression) statement where type is the type of identifier, identifier is the iteration variable that represents the array element, expression is the array expression, and statement is the statement(s) to be executed. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

37

The statements continue to execute for each element in the array. Control is transferred to the next statement following the foreach block after the iteration has been completed for all the elements. When used with an array, the foreach statement repeats the embedded statement(s) for each element in the array. Example: using System; class test { static void Main() { int[] ar = new int [] {0,-1,2,-3,4,-5,6,-7,8,-9}; foreach (int i in ar) { if (i < 0) Console.WriteLine(This is a negative number); else Console.WriteLine(This is a positive number); } } }

6.5 The jump Statement


The jump statement is used for branching, which causes a transfer of the program control. The following keywords are used in jump statements: break, continue, goto, return.

6.5.1 break
The break statement terminates the closest enclosing loop or conditional statement in which it appears. Control is passed to the statement that follows the terminated statement, if any. The break statement takes the form: break; Example: using System; class Test { static void Main() { for (int i = 15; i >= 0; i--) { if (i == 10) break; Console.Write(i); } } }

6.5.2 continue
The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears. It takes the form: continue; Example: using System; class Test { static void Main() { DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview for (int i = 13; i > 1; i--) { if (i > 2) continue; Console.WriteLine(The current value is : {0}, i); }

38

6.5.3 goto
The goto statement transfers control directly to a labeled statement. It can be one of the following forms: goto identifier; goto case constant-expression; goto default; where identifier is a label and constant-expression is switch-case label. identifier indicates a label located in the current body, the same scope, or an enclosing scope of the goto statement. goto is commonly used to transfer control to a switch-case label. goto is useful to get out of nested loops. A warning message can be issued if the label has never been referenced in the program. Example: using System; class test { static void Main() { int i = 0; do { if (i == 8) goto Label1; else i++; } while (true); } Label1: Console.Write(i);

6.5.4 return
The return statement terminates execution of the method in which it appears and returns control to the calling method. If the method is of the type void, the return statement can be omitted. The return statement has the form: return [expression]; where expression is the value returned by a method. expression is not used with methods of the type void. Example: class Test { static int AddInt(int i1, int i2) DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview { }

39

int j = i1 + i2; return j; static void Main() n1 = 120; n2 = 125; sum = AddInt(n1,n2);

public { int int int }

6.6 The switch Statement


The switch statement selects for execution a statement from a list. The type of a switch statement is specified by the switch expression. The switch statement can have at most one default label. A switch statement is executed as follows: The switch expression is evaluated and converted to the specified type. If one of the constants specified in a case label is equal to the value of the switch expression, control is transferred to the statement list following the matched case label. If none of the constants specified in case labels is equal to the value of the switch expression, control is transferred to the statement list following the default label, if a default label exists. If no default label is present, control is transferred outside the switch statement. A switch section is not permitted to fall through to the next switch section. When execution of a switch section is to be followed by execution of another switch section, a goto case or goto default statement must be used. Multiple labels are permitted in a switch section. The statement list of a switch section typically ends in a break, goto case, or goto default statement. The governing type of a switch statement may be the type string. If this is the case, null is permitted as a case label constant. The statement list is reachable if the switch statement is reachable and at least one of the following is true: 1. The switch expression is a non-constant value. 2. The switch expression is a constant value that matches a case label in the switch section. 3. The switch expression is a constant value that does not match any case label, and the switch section contains the default label. 4. A switch label of the switch section is referenced by a reachable goto case or goto default statement. The end point is reachable if at least one of the following is true: 1. The switch statement contains a reachable break statement that exits the switch statement. 2. The switch statement is reachable, the switch expression is a non-constant value, and no default label is present. 3. The switch statement is reachable, the switch expression is a constant value that does not match any case label, and no default label is present.

6.7 if-else
The if statement is a control statement that executes a block of code if an expression evaluates to true. It has the form: if (expression) statement1 DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview [else statement2]

40

where expression is an expression that can be converted to bool, statement1 is the statement(s) to be executed if expression is true, and statement2 is the statement(s) to be executed if expression is false. If expression is true, statement1 is executed. If the optional else clause exists and expression evaluates to false, statement2 is executed. After executing the if statement, control is transferred to the next statement. If any of the two results of the if statement (true or false) results in executing more than one statement, multiple statements can be conditionally executed by including them into blocks. The statement(s) to be executed upon testing the condition can be of any kind, including another if statement nested into the original if statement. In nested if statements, the else clause belongs to the last if that does not have a corresponding else. ill be displayed if the condition (x > 10) evaluates to false. x Example: using System; public class Test { static void Main() { Console.Write(Enter something from the keyboard ); char ch = (char) Console.Read(); if (Char.IsLetter(ch)) if (Char.IsLower(ch)) Console.WriteLine(What you entered is a lowercase character.); else Console.WriteLine(What you entered is an uppercase character.); else Console.WriteLine(What you entered is not a character.); } } Example: using System; public class Test { static void Main() { Console.Write(Press a key: ); char ch = (char) Console.Read(); if (Char.IsUpper(ch)) Console.WriteLine(You pressed an uppercase character.); else if (Char.IsLower(c)) Console.WriteLine(You pressed a lowercase character.); else if (Char.IsDigit(c)) Console.WriteLine(You pressed a digit.); else Console.WriteLine(What You pressed is not alphanumeric.);

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

41

7 Classes
7.1 Definition of a Class
A class is a data structure. It may contain data, functions, and nested types. Data members include constants and fields. Function members include methods, operators, events, properties, indexers, instance constructors, destructors and static constructors. A class support inheritance, which is a mechanism that allows a derived class to extend and specialize a base class.

7.2 Class Declaration


A class-declaration is a type-declaration that declares a new class. It consists of an optional set of attributes, followed by an optional set of class-modifiers, followed by the keyword class and an identifier that names the class, followed by an optional class-base specification, followed by a class-body, and optionally followed by a semicolon. Class Modifiers A class-declaration may optionally include a sequence of class modifiers: new, public, protected, internal, private, abstract, and sealed. In a class declaration, if the same modifier appears multiple times, it results in a compilertime error. The new modifier is permitted on nested classes. It specifies that the class hides an inherited member by the same name. If the new modifier appears on a class declaration that is not a nested class declaration, it results in a compile-time error. The public, protected, internal, and private modifiers control the accessibility of the class. Abstract Classes The abstract modifier indicates that a class is incomplete and that it is intended only as a base class. An abstract class cannot be instantiated directly. Using the new operator on an abstract class is a compile-time error. An abstract class can contain abstract members and it cannot be sealed. Sealed Classes The sealed modifier prevents derivation from a class. If a sealed class is specified as the base class of another class, it generates a compile-time error. A sealed class cannot be an abstract class. Base Classes When a class-type is included in the class-base, it specifies the direct base class of the class being declared. If a class declaration has no class-base, the direct base class is assumed to be object. A class inherits members from its direct base class. Example: class B {} class D: B {} Class B is said to be the direct base class of D, and D is said to be derived from B. Class B implicitly derives from object. The direct base class of a class type must be at least as accessible as the class type itself. For example, it is a compile-time error for a public class to derive from a private or internal class. The base classes of a class are the direct base class and its base classes. Except for class object, every class has exactly one direct base class. The object class has no direct base class and is the ultimate base class of all other classes.

7.3 Members of a Class


Class members consist of the members introduced by its class-member-declarations and the members inherited DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview from the direct base class. Class members are divided into the following categories: 1. Fields are the class variables. 2. Constants represent constant values associated with the class. 3. Methods implement the computations and actions that can be performed by the class. 4. Properties define characteristics associated with reading and writing those characteristics. 5. Indexers permit instances of the class to be indexed like arrays. 6. Events define notifications that can be generated by the class. 7. Instance constructors implement class initialization. 8. Operators define the expression operators that would be applied to instances of the class. 9. Static constructors implement the actions required to initialize the class itself. 10. Destructors implement the actions to be performed before instances of the class are deleted. 11. Types represent local types of the class.

42

Members that can contain executable code are known as the function members of the class. The function members of a class are the events, operators, methods, properties, indexers, instance constructors, static constructors, and destructors of that class. A class-declaration creates a new declaration space, and the class-member-declarations immediately contained by the class-declaration introduce new members into this declaration space. Rules that apply to class-memberdeclarations are: 1. Instance constructors, destructors and static constructors should have the same name as the immediately enclosing class. 2. The name of a constant, property, type, field, or event should differ from the names of all other members declared in the same class. 3. The name of a method should differ from the names of all other non-methods declared in the same class. 4. The signature of a method should differ from the signatures of all other methods declared in the same class. 5. The signature of an instance constructor should differ from the signatures of all other instance constructors declared in the same class. 6. The signature of an indexer should differ from the signatures of all other indexers declared in the same class. 7. The signature of an operator should differ from the signatures of all other operators declared in the same class. 8. The inherited members of a class are not part of the declaration space of a class. Therefore, a derived class is allowed to declare a member with the same name or signature as an inherited member. Inheritance A class inherits the members of its direct base class. It implicitly contains all members of its direct base class, except for the instance constructors, destructors and static constructors of the base class. Inheritance is transitive. If C is derived from B, and B is derived from A, then C inherits the members declared in B as well as the members declared in A. A derived class extends its direct base class. It can add new members to those it inherits, but it cannot remove the definition of an inherited member. Instance constructors, destructors, and static constructors are not inherited. A derived class can hide inherited members by declaring new members with the same name or signature. A class can declare virtual methods, properties, and indexers, and derived classes can override the implementation of these function members. This enables classes to exhibit polymorphic behavior wherein the actions performed by a function member invocation vary depending on the run-time type of the instance through which the function member is invoked. Access Modifiers It is a compile-time error to specify more than one access modifier, except for the protected internal combination. When a class-member-declaration does not include any access modifiers, private is assumed. A class-memberdeclaration can have any one of the five possible kinds of declared accessibility: public, protected internal, protected, internal, or private. Static and Instance Members DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

43

Members of a class are either static members or instance members. Static members belong to classes, and instance members belong to objects (instances of classes). When a method, event, field, property, operator, or constructor declaration includes a static modifier, it declares a static member. Additionally, a constant or type declaration implicitly declares a static member. When a method, event, field, property, indexer, constructor, or destructor declaration does not include a static modifier, it declares an instance member. Nested Types A type declared within a class or struct is called a nested type. A type that is declared within a compilation unit or namespace is called a non-nested type. Remark: this within a nested type cannot be used to refer to instance members of the containing type. Access to Private and Protected Members of the Containing Type A nested type has access to all of the members that are accessible to its containing type, including members of the containing type that have private and protected declared accessibility. Reserved Member Names For each member declaration that is a property, event, or indexer, the implementation must reserve two method signatures based on the kind of the member declaration, its name, and its type. It is a compile-time error for a program to declare a member whose signature matches one of these reserved signatures. The reserved names do not introduce declarations, thus they do not participate in member lookup. Destructor declaration causes a signature to be reserved. For a property P of type T, the following signatures are reserved: T T get_P(); void set_P(T value); Both signatures are reserved, even if the property is read-only or write-only. For an event E of delegate type T, the following signatures are reserved: T void add_E(T handler); void remove_E(T handler); For an indexer of type T with parameter-list L, the following signatures are reserved: T get_Item(L); void set_Item(L, T value); Both signatures are reserved, even if the indexer is read-only or write-only. For a class containing a destructor, the following signature is reserved: void Finalize();

7.4 Constants
A constant is a class member that represents a constant value that can be computed at compile-time. A constant declaration that declares multiple constants is equivalent to multiple declarations of single constants with the same attributes, modifiers, and type. Constants are permitted to depend on other constants within the same program as long as the dependencies are not of a circular nature.

7.5 Fields
A field represents a variable associated with an object or class. A field-declaration introduces one or more fields of a given type. It declares that multiple fields are the same as multiple declarations of single fields with the same attributes, modifiers, and type. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

44

Static and Instance Fields When a field declaration includes a static modifier, the fields introduced are static fields. When no static modifier is present, the fields introduced are instance fields. A static field is not part of a specific instance. There is only one copy of a static field for the associated application domain. An instance field belongs to an instance. Every instance of a class contains a separate set of all instance fields of the class. Readonly Fields When a field-declaration includes a readonly modifier, the fields are readonly fields. Direct assignments to readonly fields can only occur as part of the declaration or in an instance constructor (for readonly non-static fields) or static constructor (for readonly static fields) in the same class. Attempting to assign to a readonly field or passing it as an out or ref parameter in any other context results in a compile-time error.

7.6 Methods
A method is a member that implements a computation or action that can be performed by an object or class. Methods are declared using method-declarations. The return-type of a method declaration specifies the type of the value computed and returned by the method. The return-type is void if the method does not return a value. The member-name specifies the name of the method. Method Parameters The methods formal-parameter-list declares the parameters of a method, if any exist. Value Parameters A parameter declared with no modifiers is a value parameter. It corresponds to a local variable that gets its initial value from the corresponding argument supplied in the method invocation. When a formal parameter is a value parameter, the corresponding argument in a method invocation must be an expression of a type that is implicitly convertible to the formal parameter type. A method is permitted to assign new values to a value parameter. Reference Parameters A parameter declared with a ref modifier is a reference parameter. It does not create a new storage location. A reference parameter represents the same storage location as the variable given as the argument in the method invocation. A variable must be definitely assigned before it can be passed as a reference parameter. Within a method, a reference parameter is always considered definitely assigned. Output Parameters A parameter declared with an out modifier is an output parameter. It does not create a new storage location. An output parameter represents the same storage location as the variable given as the argument in the method invocation. A variable need not be definitely assigned before it can be passed as an output parameter, but following an invocation where a variable was passed as an output parameter, the variable is considered definitely assigned. Within a method an output parameter is considered initially unassigned and must be definitely assigned before its value is used. Every output parameter of a method must be definitely assigned before the method returns. Static and Instance Methods When a method declaration includes a static modifier, the method is said to be a static method. When no static modifier is present, the method is said to be an instance method. A static method does not operate on a specific instance, and it is a compile-time error to refer to this in a static method. On the other hand, an instance method operates on a given instance of a class, and this instance can be accessed as this.

7.7 Properties
A property is a member that provides access to a characteristic of an object or a class. The length of a string, the caption of a window, the name of a customer, and the size of a font are all examples of a property. Properties are a natural extension of fields and do not denote storage locations. The type of a property declaration specifies the type of the property introduced by the declaration, and the member-name specifies the name of the property. The type of a property must be at least as accessible as the property itself. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

45

Static and Instance Properties When a property declaration includes a static modifier, the property is said to be a static property. When no static modifier is present, the property is said to be an instance property. A static property is not associated with a specific instance. On the other hand, an instance property is associated with a given instance of a class, and this instance can be accessed as this in the accessors of the property. Virtual, Sealed, Override, and Abstract Accessors A virtual property declaration specifies that the accessors of the property are virtual. The virtual modifier applies to both accessors of a read-write property. It is not possible for only one accessor of a read-write property to be virtual. An abstract property declaration specifies that the accessors of the property are virtual, but it does not provide an actual implementation of the accessors. Non-abstract derived classes are required to provide their own implementation for the accessors by overriding the property. A property declaration that includes both the abstract and override modifiers specifies that the property is abstract and overrides a base property. Abstract property declarations are only permitted in abstract classes. The accessors of an inherited virtual property can be overridden in a derived class by including a property declaration that specifies an override directive. An overriding property declaration may include the sealed modifier. The accessors of a sealed property are also sealed. Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessors behave exactly like virtual, sealed, override and abstract methods.

7.8 Events
An event is a member that enables an object or class to provide notifications. Clients can attach executable code for events by supplying event handlers. An event can be used as the left hand operand of the += and -= operators. Static and Instance Events When an event declaration includes a static modifier, the event is said to be a static event. When no static modifier is present, the event is said to be an instance event. A static event is not associated with a specific instance. On the other hand, an instance event is associated with a given instance of a class, and this instance can be accessed as this in the accessors of the event.

7.9 Indexers
An indexer is a member that enables an object to be indexed in the same way as an array. An indexer element is not classified as a variable; therefore, it is not possible to pass an indexer element as a ref or out argument. The formal parameter list of an indexer defines the signature of the indexer, which consists of the number and types of its formal parameters. The element type and names of the formal parameters are not part of an indexers signature. The signature of an indexer must differ from the signatures of all other indexers declared in the same class. Indexers and properties are conceptually similar, but they differ in many ways. When an indexer declaration includes an extern modifier, the indexer is said to be an external indexer.

7.10 Operators
An operator is a member that defines the meaning of an expression operator that can be applied to instances of the class. There are three categories of overloadable operators: 1. Unary operators. 2. Binary operators. 3. Conversion operators. An operator declaration must include both a public and a static modifier. When an operator declaration includes an extern modifier, the operator is said to be an external operator. For all non-external operators, the operatorbody consists of a block which specifies the statements to execute when the operator is invoked. The parameter(s) of an operator must be value parameters. The signature of an operator must differ from the signatures of all other operators declared in the same class. All types referenced in an operator declaration DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

46

must be at least as accessible as the operator itself. When the same modifier appears multiple times in an operator declaration, it results in a compile-time error. Each operator category imposes additional restrictions, as described in the following sections. Like other members, operators declared in a base class are inherited by derived classes.

7.11 Instance Constructors


An instance constructor is a member that implements the actions required to initialize an instance of a class. Constructor-declarations declare instance constructors. A constructor-declaration may include a set of attributes, a valid combination of the four access modifiers, and an extern modifier. A constructor declaration is not permitted to include the same modifier multiple times. The identifier of a constructor-declarator must name the class in which the constructor is declared. Specifying any other name results in a compile-time error. The formal parameter list defines the signature of an instance constructor and governs the process whereby overload resolution selects a particular instance constructor in an invocation. Each of the types referenced in the formal-parameter-list of an instance constructor must be at least as accessible as the constructor itself. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this instance constructor. When a constructor declaration includes an extern modifier, the constructor is said to be an external constructor. Because an external constructor declaration provides no actual implementation, its constructorbody consists of a semicolon. For all other constructors, the constructor-body consists of a block, which specifies the statements to initialize a new instance of the class. This corresponds exactly to the block of an instance method with a void return type. Instance constructors are not inherited. Thus, a class has no instance constructors other than those actually declared in the class. If a class contains no instance constructor declarations, a default instance constructor is automatically provided. Instance constructors are invoked by object-creation-expressions and through constructor-initializer constructor-initializers. Default Constructors If a class contains no instance constructor declarations, a default instance constructor is automatically provided. The default constructor simply invokes the parameterless constructor of the direct base class. If the direct base class does not have an accessible parameterless instance constructor, a compile-time error occurs. If the class is abstract, then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. A default constructor is provided because the class contains no instance constructor declarations.

7.12 Static Constructors


A static constructor is a member that implements the actions required to initialize a class. Static constructors are not inherited, and cannot be called directly. The exact timing of static constructor execution is implementation-dependent.

7.13 Destructors
A destructor is a member that implements the actions required to destruct an instance of a class. Destructors are not inherited. Thus, a class has no destructors other than the one that may be declared in it. Since a destructor is required to have no parameters, it cannot be overloaded. Thus, a class can have, at most, one destructor. Destructors are invoked automatically, and cannot be invoked explicitly. An instance becomes eligible for destruction when it is no longer possible for any code to use the instance. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction. When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived. Destructors are implemented by overriding the virtual method Finalize on System.Object. Programs are not permitted to override this method or call it (or overrides of it) directly. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

47

8 Arrays
8.1 Introduction
An array is a data structure. It contains a number of variables, which are accessed through computed indices. Also called the elements of the array, the variables contained in an array are all of the same type, which is called the element type of the array. An array has a rank that determines the number of indices associated with each array element. The rank of an array is also referred to as the dimensions of the array. An array with a rank of one is called a single-dimensional array, while an array with a rank greater than one is called a multi-dimensional array. Multi-dimensional arrays of specific sizes are often referred to by size, as two-dimensional arrays, three-dimensional arrays, and so on. Each dimension of an array has an associated length that is an integral number greater than or equal to zero. The dimension lengths are not part of the type of the array; instead, they are established when an instance of the array type is created at run-time. The length of a dimension determines the valid range of indices for that dimension. For example, for a dimension of length N, indices can range from 0 to N 1 inclusive. The total number of elements in an array is the product of the lengths of each dimension in the array. If one or more of the dimensions of an array have a length of zero, the array is said to be empty. The element type of an array can be any type, including an array type.

8.2 Array Types


The System.Array Type The System.Array type is the abstract base type of all array types. An implicit reference conversion exists from any array type to System.Array, and an explicit reference conversion exists from System.Array to any array type. System.Array itself is not an array-type; rather, it is a class-type from which all array-types are derived. At runtime, a value of type System.Array can be null or a reference to an instance of any array type.

8.3 Array Creation


Array instances are created by array-creation-expressions or by field or local variable declarations that include an array-initializer. When an array instance is created, the rank and length of each dimension are established and then remain constant for the entire lifetime of the instance. It is not possible to change the rank of an existing array instance, nor is it possible to resize its dimensions. An array instance is always of an array type. The System.Array type is an abstract type that cannot be instantiated. Elements of arrays created by array-creationexpressions are always initialized to their default value.

8.4 Array Element Access


Array elements are accessed using element-access expressions of the form A[I1, I2, ..., IN], where A is an expression of an array type and each IX is an expression of type int, uint, long, ulong, or of a type that X can be implicitly converted to one or more of these types. The result of an array element access is a variable, namely the array element selected by the indices. The elements of an array can be enumerated using a foreach statement.

8.5 Array Members


Every array type inherits the members declared by the System.Array type.

8.6 Array Initializers


Array initializers may be specified in field declarations, local variable declarations, and array creation expressions. The context in which an array initializer is used determines the type of the array being initialized. In an array DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

48

creation expression, the array type immediately precedes the initializer. In a field or variable declaration, the array type is the type of the field or variable being declared. When an array initializer is used in a field or variable declaration, such as: int[] ar = {1, 3, 5, 7, 9}; it is simply shorthand for an equivalent array creation expression: int[] arr = new int[] {1, 3, 5, 7, 9} For a single-dimensional array, the array initializer must consist of a sequence of expressions that are assignment compatible with the element type of the array. The expressions initialize array elements in increasing order, starting with the element at index zero. The number of expressions in the array initializer determines the length of the array instance being created. For example, the array initializer above creates an int[] instance of length 5 and then initializes the instance with the following values: a[0] = 1; a[1] = 3; a[2] = 5; a[3] = 7; a[4] = 9; For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension, and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level. The example: int[,] ar = {{10, 11}, {12, 13}, {14, 15}, {16, 17}}; creates a two-dimensional array with a length of four for the leftmost dimension and a length of two for the rightmost dimension: int[,] ar = new int[4, 2]; and then initializes the array instance with the following values: ar[0, 0] = 0; b[0, 1] = 11; ar[1, 0] = 2; b[1, 1] = 13; ar[2, 0] = 4; b[2, 1] = 15; ar[3, 0] = 6; b[3, 1] = 17; ar[4, 0] = 8; b[4, 1] = 19; When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths must be constant expressions and the number of elements at each nesting level must match the corresponding dimension length.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

49

9 Structures
9.1 Definition of a Struct
Structs are similar to classes in that they represent data structures that can contain data members and function members. Unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data, the latter known as an object. Structs are particularly useful for small data structures that have value semantics. Complex numbers, points in a coordinate system, or key-value pairs in a dictionary are all good examples of structs. The simple types provided by C#, such as int, double, and bool, are in fact all struct types. It is possible to use structs and operator overloading to implement new primitive types in the C# language.

9.2 Struct Declaration


A struct-declaration is a type-declaration that declares a new struct. Struct Modifiers A struct-declaration may optionally include a sequence of struct modifiers. It is a compile-time error for the same modifier to appear multiple times in a struct declaration. The modifiers of a struct declaration have the same meaning as those of a class declaration. Struct Interfaces A struct declaration may include a struct-interfaces specification, in which case the struct is said to implement the given interface types. Struct Body The struct-body of a struct defines the members of the struct.

9.3 Members of a Struct


The struct members are the members introduced by its struct-member-declarations and the members inherited from System.ValueType, which inherits from object.

9.4 Class and Struct Differences


Structs are value types. All struct types implicitly inherit from class object. Instance field declarations for a struct are not permitted to include variable initializers. Assignment to a variable of a struct type creates a copy of the value being assigned. The meaning of this is different for structs. A struct is not permitted to declare a destructor. The default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null. A struct is not permitted to declare a parameterless instance constructor. Boxing and unboxing operations are used to convert between a struct type and object. Structs are value types and are said to have value semantics. Classes, on the other hand, are reference types and are said to have reference semantics. A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data, the latter known as an object. With classes, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With structs, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other. Because structs are not reference types, it is not possible for values of a struct type to be null. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

50

All struct types implicitly inherit from class object. A struct declaration may specify a list of implemented interfaces, but it is not possible for a struct declaration to specify a base class. Struct types are never abstract and are always implicitly sealed. The abstract and sealed modifiers are therefore not permitted in a struct declaration. Since inheritance is not supported for structs, the declared accessibility of a struct member cannot be protected or protected internal. Function members in a struct cannot be abstract or virtual, and the override modifier is allowed only to override methods inherited from the object type. Assignment to a variable of a struct type creates a copy of the value being assigned. This differs from assignment to a variable of a class type, which copies the reference but not the object identified by the reference. Similar to an assignment, when a struct is passed as a value parameter or returned as the result of a function member, a copy of the struct is created. A struct may be passed by reference to a function member using a ref or out parameter. When a property or indexer of a struct is the target of an assignment, the instance expression associated with the property or indexer access must be classified as a variable. If the instance expression is classified as a value, a compile-time error occurs. A value of a class type can be converted to type object or to an interface type that is implemented by the class simply by treating the reference as another type at compile-time. Likewise, a value of type object or a value of an interface type can be converted back to a class type without changing the reference (but of course a run-time type check is required in this case). Since structs are not reference types, these operations are implemented differently for struct types. When a value of a struct type is converted to type object or to an interface type that is implemented by the struct, a boxing operation takes place. When a value of type object or a value of an interface type is converted back to a struct type, an unboxing operation takes place. A key difference from the same operations on class types is that boxing and unboxing copies the struct value either into or out of the boxed instance. Following a boxing or unboxing operation, changes made to the unboxed struct are not reflected in the boxed struct. Within an instance constructor or instance function member of a class, this is classified as a value. Thus, while this can be used to refer to the instance for which the function member was invoked, it is not possible to assign to this in a function member of a class. Within an instance constructor of a struct, this corresponds to an out parameter of the struct type, and within an instance function member of a struct, this corresponds to a ref parameter of the struct type. this is classified as a variable, and it is possible to modify the entire struct for which the function member was invoked by assigning to this or by passing this as a ref or out parameter. The default value of a struct consists of the value that results from setting all value type fields to their default value and all reference type fields to null. For this reason, a struct does not permit instance field declarations to include variable initializers. Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct implicitly has a parameterless instance constructor that always returns the value that results from setting all value type fields to their default value and all reference type fields to null. A struct instance constructor is not permitted to include a constructor initializer of the form base(...). The this variable of a struct instance constructor corresponds to an out parameter of the struct type, and, similar to an out parameter, this must be definitely assigned at every location where the instance constructor returns. A struct can declare instance constructors having parameters. It is not permitted to declare a destructor.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

51

10 Miscellaneous
10.1 C# 2.0
With the release of Visual Studio 2005, the C# language has been updated to version 2.0.

10.1.1 The System.Diagnostics Namespace


The System.Diagnostics namespace provides classes that allow the interaction with system processes, event logs, and performance counters. It also provides classes that allow debugging the application and tracing the code execution. The Stopwatch Class The Stopwatch class provides a set of methods and properties that can be used to accurately measure elapsed time.

10.1.2 The System.Drawing Namespace


The System.Drawing namespace provides access to GDI+ basic graphics functionality. The Rectangle Structure The Rectangle structure stores a set of four integers that represent the location and size of a rectangle. A rectangle is defined by its width, height, and upper-left corner. The Color Structure The Color structure represents an ARGB color. The PointF Structure The PointF structure represents an ordered pair of floating point x- and y-coordinates. The pair defines a x y point in a two-dimensional plane. The Point Structure The Point structure represents an ordered pair of integer x- and y-coordinates. The pair defines a point in x y a two-dimensional plane. The Font Class The Font class defines a particular format for text, including font face, size, and style attributes.

10.1.3 The System.Windows.Forms Namespace


The System.Windows.Forms namespace contains classes used for creating Windows-based applications that take full advantage of the user interface features available in the Microsoft Windows operating system. The Form Class The Form class represents a window or dialog box that makes up an applications user interface.

10.1.4 The System.Collections.Generic Namespace


The System.Collections.Generic namespace contains interfaces and classes that define generic collections. These interfaces and classes allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

52

The List<T> Class The List<T> class implements the IList<T> interface using an array whose size is dynamically increased as required.

10.2 Generics
Generics are a new feature in version 2.0 of the C# language. Generic types are added to the language to enable the programmer to achieve a high level of code reuse and enhanced performance for collection classes. They are used with collections and the methods that operate on them. Generics introduce the concept of type parameters. Generic classes encapsulate operations that are not specific to any particular data type. The most common use for generic classes is with collections like: 1. 2. 3. 4. 5. Linked lists Hash tables Stacks Queues Trees

It is useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. It is preferable to use generic interfaces with generic classes. A generic method is a method that is declared with type parameters. Non-generic methods can access the class-level type parameters within a generic class. Generic methods can be overloaded on a number of type parameters. Delegates defined within a generic class can use the generic class type parameters in the same way that class methods do. Generic delegates are especially useful in defining events based on the typical design pattern. A new namespace called System.Collections.Generic includes several ready-to-use generic collection classes and associated interfaces.

10.3 The Exception Class


The Exception class represents errors that occur during application execution. This class is the base class for all exceptions. When an error occurs, either the system or the currently executing application reports it by throwing an exception containing information about the error. Once thrown, an exception is handled by the application or by the default exception handler. If an application handles exceptions that occur during the execution of a block of application code, the code must be placed within a try statement. When an exception occurs in a try block, the system searches the associated catch blocks in the order they appear in application code, until it locates a catch block that handles the exception. A catch block handles an exception of type T if the type filter of the catch block specifies T or any type that T derives from. The system stops searching after it finds the first catch block that handles the exception.

10.4 DirectX
DirectX is a set of interfaces for creating games and other high-performance multimedia applications. It supports two-dimensional (2-D) and three-dimensional (3-D) graphics, sound effects and music, input devices, and networked applications.

10.4.1 Direct3D
Direct3D enables you to manipulate visual models of 3-dimensional objects and take advantage of hardware acceleration, such as video graphics cards. The RenderStateManager Class The RenderStateManager class defines device render states. The Texture Class DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview The Texture class manipulates a texture resource. The PresentParameters Class The PresentParameters class describes the presentation parameters.

53

The Device Class The Device class performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, gets and sets palettes, and creates shaders. The Sprite Class The Sprite class provides methods and properties that simplify the process of drawing sprites using Direct3D. The Font Class The Font class encapsulates the textures and resources needed to render a specific font on a specific device.

10.4.2 DirectInput
DirectInput is used to process data from a keyboard, mouse, joystick, or other game controller. The Device Class The Device class is used to gain and release access to DirectInput devices, manage device properties and information, set behavior, perform initialization, create and play force-feedback effects, and invoke a devices control panel. The Key Enumeration The Key enumeration includes all the available keyboard keys.

10.4.3 DirectSound
DirectSound is used to capture sounds from input devices and play sounds through various playback devices using advanced 3-dimensional positioning effects and filters for echo, distortion, reverberation, and other effects. The Device Class The Device class contains methods and properties that are used to create buffer objects, manage devices, and set up the environment. The SecondaryBuffer Class The SecondaryBuffer class contains methods and properties that are used to manage sound buffers that can support effects.

10.4.4 DirectX.AudioVideoPlayback
The AudioVideoPlayback interface provides for basic playback and simple control of audio and video files. The Audio Class The Audio class is primarily designed for very simple playback scenarios, or for use with the Video class.

10.4.5 The Vector2 Class


The Vector2 class describes and manipulates a vector in two-dimensional space. DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: C# Programming Overview

54

10.4.6 The Matrix Class


The Matrix class describes and manipulates a matrix.

DigiPen Game Development C# Webcast Series - Session Two, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Game Components
DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session Three:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
1 Graphics
1.1 Two-Dimensional Graphics 1.2 Three-Dimensional Graphics 1.3 Bitmap-Based Graphics 1.4 Vector-Based Graphics 3 3 4 4

2 Input
2.1 Keyboards 2.2 Mouse 2.3 Joystick 2.4 Force Feedback with Input Devices 5 5 5 6

3 Sounds
3.1 Introduction 3.2 Uncompressed Audio Format 3.2.1 WAV File Format 3.2.2 MIDI (Musical Instrument Digital Interface) 3.3 Compressed Audio Format 3.3.1 MP3 3.3.2 OGG VORBIS 3.4 Three-Dimensional Audio 3.5 Interactive Game Audio 7 7 7 7 7 7 8 8 8

4 Networks
4.1 Introduction 4.2 Implementation of Multi-Player Games 4.2.1 Peer-to-Peer 4.2.2 Client-Server 4.2.2.1 Web Cam 4.2.2.2 Voice-Over IP (VoIP) 9 9 9 10 10 10

Game Web Cast Project: Game Components

1 Graphics
1.1 Two-Dimensional Graphics
2D graphics are the computer-based generation of digital images mostly from two-dimensional models (2D geometric models, text, and digital images). These graphics are mainly used in applications that were originally developed using traditional printing and drawing techniques, such as typography, cartography, technical drafting, advertising, and so forth. In many domains, a description of a document based on 2D computer graphic techniques (vector-based), can be much smaller than the corresponding digital image often by a factor of 1/1000 or more. This representation is also more flexible since it can be rendered at different resolutions to suit specific output devices. 2D computer graphics started with vector-based graphic devices. In the following decades, bitmap-based devices largely supplanted these.

1.2 Three-Dimensional Graphics


3D graphics are works of graphic art that are created using the aid of digital computers and specialized 3D software. In general, the art of 3D graphics is similar to sculpting and photography, while the art of 2D graphics is similar to painting. In computer graphics software, this distinction is occasionally blurred; some 2D applications use 3D techniques to achieve certain effects, such as lighting, while some primarily 3D applications make use of 2D visual techniques. These graphics are distinct from 2D computer graphics in that a three-dimensional virtual representation of the game objects is stored in the computer for the purposes of performing calculations and rendering images.

In 3D graphics, an object moves freely on the three axes. The orientation can be over the x-, y-, x y-, and z-axis. The rotation along the x-axis is called x pitch, along the y-axis yaw, and along the y z-axis roll.

For example, in a flight simulation, the plane can pitch, yaw, and roll.

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

1.3 Bitmap-Based Graphics


A bitmap graphic image is a data file or structure representing a generally rectangular grid of pixels, or points of color, on a computer monitor or other display device. The color of each pixel is individually defined; images in the RGB color space often consist of colored pixels defined by three bytes (one byte each for red, green, and blue color values). Less colorful images require less information per pixel. For example, an image with black and white pixels only requires a single bit for each pixel. Bitmap graphics are different from vector graphics in that vector graphics represent an image using geometric objects such as curves and polygons. Instead, a bitmap corresponds bit to bit with an image displayed on a screen, probably in the same format as it would be stored in the displays video memory. A bitmap is characterized by the width and height of the image in pixels and the number of bits per pixel, which determines the number of colors it can represent. The total number of pixels (resolution) and amount of information in each pixel (color depth) determines the quality of a bitmap image. For example, an image that stores 24 bits of color information per pixel can represent smoother degrees of shading than one that only stores 16 bits per pixel, but not as smooth as one that stores 48 bits. Likewise, an image sampled at 640x480 pixels (containing 307,200 pixels) will look rough and blocky compared to one sampled at 1280x1024 (1,310,720 pixels). Storing a high-quality image requires a large amount of data; that is why data compression techniques are often used to reduce the size of images stored on disk. Bitmap graphics cannot be scaled to a higher or lower resolution without a loss of noticeable quality. Bitmap graphics are more practical than vector graphics for photographs and photo-realistic images.

1.4 Vector-Based Graphics


Vector graphics use geometrical primitives such as points, lines, curves, and polygons to represent images. These graphics are often more practical for typesetting or graphic design like text, logos, and diagrams. Vector graphics easily scale to the quality of the device on which they are rendered and therefore allow for much more precise control and resizing of the image.

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

2 Input
2.1 Keyboards

The keyboard is an input device with systematically arranged keys that allow users to type information, move the cursor, or activate functions assigned to keys. The keys on computer keyboards are often classified as follows: Alphanumeric keys: Letters and numbers to enter data. Punctuation keys: Comma, period, semicolon, and so on. Special keys: Function keys, control keys, arrow keys, Caps Lock key, and so on. In a game, the keyboard is used to enter text or data, to move objects, to access objects behaviors, to load, and to save the game.

2.2 Mouse

A mouse is a small object, which is rolled along a hard, flat surface. Mouse devices control the movement of the cursor or pointer on a display screen. As you move the mouse, the pointer on the display screen moves in the same direction. The mouse is used to move game objects and to shoot. The mouse frees users to a large extent from using the keyboard. In particular, the mouse is important for graphical user interfaces (GUI) because users can simply point to options and objects and click a mouse button to select that option. The mouse is also useful for graphics programs that allow users to draw images by using the mouse like a pen, pencil, or paintbrush.

2.3 Joystick

A joystick is a pointing device consisting of a hand-held stick that pivots about one end and transmits its angle in DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

two or three dimensions to a computer. Most joysticks are two-dimensional, having two axes of movement, just like a mouse, but three-dimensional joysticks do exist. Joysticks are often used to control games, and usually have one or more push-buttons whose state can also be read by the computer and often set by the user. An analog joystick is a joystick that has continuous states it returns an angle measure of the movement in any direction in the plane or the space. A digital joystick gives only on/off signals for four different directions and mechanically possible combinations (such as up-right, down-left, etc). Digital joysticks are very common as game controllers for video game consoles. Joysticks often have one or more fire buttons, used to trigger some kind of action which is visible on the screen. These are digital.

2.4 Force Feedback with Input Devices


Some input devices are capable of responding to force feedback, the pushing or resisting forced that a joystick exerts on a player, for example. The force is the push or resistance. Particular forces create specific effects for which a game is programmed. Effects can be categorized as follows: Constant force: A force in a single direction. Ramp force: A force that increases or decreases in magnitude. Periodic effect: A force that pulsates according to a defined wave pattern. Condition: A reaction to motion or position along an axis.

For example, a friction effect generates resistance to the movement of the joystick. The direction can be defined for the x-, y-, and z-axes. As with the joystick, the x-axis increases from left to right (- +), and the y-axis increases from far to near (- +). For example, an effect with a direction along the negative y-axis (-) tends to push the stick toward users along the y-axis. The magnitude is the strength of the force. It is measured in units starting from 0 (no force) until 10,000 (the maximum force). A negative value indicates that the force is in the opposite direction. The duration of an effect is measured in microseconds. Periodic effects have a period (the duration of one cycle), which is also measured in microseconds. The phase of a periodic effect is the point at which the playback begins.

A sawtooth periodic effect with a magnitude of 5,000, or half the maximum force for the device.

The Force Editor application offers the ability to design force-feedback effects and test them singly or in combination. Effects can be saved to file and then loaded into applications.

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

3 Sounds
3.1 Introduction
Sound and music always play a major role in games. Designers depend on sound and music to give players audio feedback, in addition to graphic feedback. Along with all the improvement in hardware technology, audio has also improved. Nowadays, various audio technologies exist, such as compressed, uncompressed, three-dimensional, and interactive audio. Game programmers often adjust between compressed and uncompressed audio formats, depending on the bottleneck in their data-loading pipeline. Game programmers change their decisions according to whether the load should be on the CPU (resources-decompressing audio at run time) or the hard-drive/CDROM/DVD where uncompressed file sizes can be10 to 20 times the size of a compressed file.

3.2 Uncompressed Audio Format 3.2.1 WAV File Format


WAV File Format is a file format for storing digital audio (waveform) data. A WAV file is characterized by the file extension .wav. This music file format provides raw, uncompressed audio data. This format is widely used in professional programs that process digital audio waveforms. Audio quality is excellent, but file sizes can be very large. A full pop song in WAV format may take up to 40 MB of disk space or more. Note: we can find a compressed (.wav) wave file format.

3.2.2 MIDI (Musical Instrument Digital Interface)


The MIDI file format was originally created for recording and playing music on digital synthesizers. MIDI files are very small in size. The reason for this is that the MIDI file only contains information on how music is produced (e.g. note-ons and note-offs ). The sound card, which plays back the MIDI file, takes this information and plays back music using a built-in soundcard wave-table (waves stored in the sound cards ROM).

3.3 Compressed Audio Format 3.3.1 MP3


MP3, short for MPEG-1/MPEG-2 Layer 3, is a format for storing digital audio. It uses an advanced type of audio compression, which reduces the file size (usually 1/12 of the original file size) with little reduction in audio quality. The trick behind MP3 is eliminating sounds that the human ear cannot perceive. The human hearing range is between 20Hz to 20Khz, and it is most sensitive between 2 and 4 KHz. When sound is compressed into an MP3 file, an attempt is made to get rid of the frequencies that cannot be heard. For example: There are certain sounds that the human ear cannot hear. There are certain sounds that the human ear perceives much better than others. If there are two sounds playing simultaneously, we hear the louder one but cannot hear the softer one. Using facts like these, voices shrink considerably by a factor of 10 to 12 at least.

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

3.3.2 OGG VORBIS


Ogg Vorbis is a high-compression, lossy algorithm for encoding audio content. It is a general-purpose compressed audio. This places Vorbis in the same competitive class as audio representations such as MPEG-4 (AAC), and similar to, but higher performance than MPEG-1/2 audio layer 3, MPEG-4 audio (TwinVQ), WMA, and PAC. Vorbis-encoded audio can be used both for soundtracks and general-purpose sound effects. Static buffers are only decoded once at load time, making their use practical for just about any audio source in a game.

3.4 Three-Dimensional Audio


3D audio to use its proper name, positional 3D audio is the latest technology designed to render sound in the entire audio field. Positional 3D audio plots a sounds velocity and trajectory in full three-dimensional space. Effects that are difficult or impossible to accomplish with simple stereo output can be achieved with 3D audio. For example, panning is no longer restricted to moving from side to side in a two-speaker system, and sounds can now go up, down, or literally anywhere in the three-dimensional auditory space. Imagine that you are listening to an orchestra playing in a symphony hall. If you close your eyes, you should be able to hear the sound of the basses coming from the back left corner of the stage and the trumpets from the middle right. 3D audio replicates this sound stage. With the appropriate soundcard and sound system, 3D audio presents some very exciting sonic possibilities. A game could include aural cues to alert players to the presence of an invisible opponent coming from the left rear corner; a composer might experiment with a sound spiraling towards the listener in a slowly tightening loop. Alternatively, the movement of images in an animation may be coordinated with sounds in unusual ways, such as a reverse Doppler effect with the sound decreasing in intensity and frequency as it nears the listener and reversing the effect as it leaves.

3.5 Interactive Game Audio


In a movie the composer knows exactly when and where certain events are going to happen. A film is a linear medium, experienced from start to finish upon each viewing and never changing. In games, the composer has limited knowledge of when or where events and actions will happen because games are interactive in real-time. It is a different way of thinking about music and flow, and must be accommodated. Otherwise, you can end up in an endless scene with a looping background track that easily becomes monotonous and boring. An interactive audio system allows the audio artist, the composer, to construct a compelling soundtrack that will adapt and change in response to the players actions in real-time and in an appropriate way for the current game situation.

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

4 Networks
4.1 Introduction
Multi-player games are games played by two or more people on a network. Each player controls one or more game objects, and the effect of this action has to be replicated to all other players. Multi-player problems: Delay: The communication delay affects the game playability. Reliability: A major problem is the reliability of the communication. In any multi-player game, the main goal is to have a reliable transfer with a minimum amount of information required to maintain playability.

4.2 Implementation of Multi-Player Games


Multi-player games can be implemented by the application setting up and sending the messages, or more commonly by using an API such as DirectPlay, which does this automatically. Such an application acts as an interface between the game application and the Internet, handling the transmission and reception of messages and the player management. The main modes used for managing multi-player games are peer-to-peer and client-server.

4.2.1 Peer-to-Peer
Peer-to-peer mode requires no server application to be developed. An application embedded in the game itself handles communication, and each player maintains his or her own copy of the game database. This solves the potential problem of a server bottleneck. If there are n players in the game, then a single change made by one player needs to be communicated to the other (n - 1) players. If all players make changesthe usual situation at any time during a gamethen n(n-1) messages need to be sent.

Player

Player

Player

Player

Player

Player

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Components

10

4.2.2 Client-Server
A client-server system is also known as a logical star configuration. In a client-server configuration, the server collects messages from all players. Clients, or players, only send messages to, or receive messages from, the server. Clients request from the server:
Player

To request to join an existing game or start a new one. To start the game and begin to play. To exit the session. Server response: The server collects messages from the clients and issues acknowledgements. On receipt of an acknowledgement, the client applies the command contained in the message.

Player

Player

Server

Player
Player

Player

In multi-player games, new technologies are adding excitement and amusement because players can see (using web cams) and talk (using voice-over IP) to each other during game play.

4.2.2.1 Web Cam


A web cam is camera that is attached to the computer. It captures images and makes them available to any other player. The web cam application continues to capture images over time while refreshing the image. Therefore, it broadcasts a real-time movie.

4.2.2.2 Voice-Over IP (VoIP)


VoIP technology converts spoken words from a microphone into digital packets that travel over the network. Then, it converts back at the other end so players can speak to each other.

DigiPen Game Development C# Webcast Series - Session Three, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Game Design Elements


DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session Four:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
1 World Dimension
1.1 What Is a World? 1.2 Why Do We Use the World? 3 3

2 Viewport 3 Bitmap
3.1 What is a Bitmap? 3.2 RGB Intensity 3 3

4 Background
4.1 What Is a Background? 4.2 Position 3 3

5 Animations
5.1 What Is an Animation? 5.2 What Do We Do When the Animation Ends? 4 4

6 Frames
6.1 What Is a Frame? 6.2 Transparency 4 4

7 Sprite
7.1 What Is a Sprite? 7.2 Active 7.3 Visible 7.4 Animation 8 ZOrder 4 5 5 5 5

Game Implementation
Step 1: Adding the Background Step 2: Adding Sprites Step 3: Adding the Trooper Sprite Step 4: Adding the Condor Sprite Step 5: Adding Another Animation Explosion to the Condor Sprite 5 6 8 9 10

Game Web Cast Project: Game Design Elements

Game Design Elements


1 World Dimension 1.1 What Is a World?
The world is the environment where the entire game will take place. It is a rectangle with a defined width and height.

1.2 Why Do We Use the World?


The simulation application applies mathematical transformations on game objects and cameras. These transformations operate on game object structures. All objects must be within a defined area. If a position of an object is outside the world, we can reposition the object or choose another option.

2 Viewport

A world rectangular coordinate selected for display is called a window. (Note: This is not the Operating System Window.) The window defines what is viewed, whereas the viewport defines where it is to be displayed. A viewport is a rectangular area of the display window. By default, it is the entire window (client area of the operating systems window) application. It can be set to any smaller size in pixels.

3 Bitmap 3.1 What is a Bitmap?


A bitmap is an image made up of thousands of pixels. Every pixel has its own position and qualities. Qualities such as color and brightness are stored in the bitmap file.

3.2 RGB Intensity


Monitors work with red, green, and blue signals. Every colored pixel contains a value of red, green, and blue within it. The intensity of every basic color creates a color. In other words, colors are displayed as varying intensities of red, green, and blue dots.

4 Background 4.1 What Is a Background?


A background is an image or bitmap that will be placed in the world at a certain position. The area of the screen display that is not covered by a game object is the background that can be seen by the player. Game objects are displayed on top of a background.

4.2 Position
The background should be positioned within the world. The backgrounds position is situated relatively to the world rectangle, not relatively to the viewport.

DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

5 Animations 5.1 What Is an Animation?


An animation is a series of images drawn in sequence with a specific delay for each image. Each frame of an animation is slightly different. When played in succession, they give the appearance of movement.

5.2 What Do We Do When the Animation Ends?


When the animation ends, it can: Restart from the first frame. Restart from a certain frame (not necessarily the first one). Remain at the last frame.

6 Frames 6.1 What Is a Frame?


A frame is simply an image that is part of an animation.

6.2 Transparency
A frame is made out of a bitmap, which is a matrix of points that has a rectangular or square shape. Of course, not all images have a rectangular shape. Areas not used in the rectangle will be filled with one specific color called the transparency color. The drawing mechanism will ignore all pixels having the transparency color; in other words, the transparent pixels will not be drawn.

With Transparency color 7 Sprite 7.1 What Is a Sprite?

Without Transparency color

Sprites are the pictures displayed on top of the background. They are also called game objects. Usually, sprites represent all the moving parts of the game. Sprites can be: o o o o Enemies Animated parts of the background Bullets Any other moving game object

Since the sprite status varies in terms of images, position, and values, the sprite needs to save information such as: o o o o o o Current position Current speed Current direction Visibility status Current frame Current animation DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements o Any other relevant information relating to image, position, or value

7.2 Active
This specifies whether the sprite will be active. When the sprite is not active, all its behaviors are disabled and it is not visible. Then the sprite will not be processed during the game loop. An inactive sprite is usually used when a sprite prototype is needed. A sprite prototype can be used to create identical sprites at run time. Inactive sprites are also used when a sprite does not need to be present when the level starts.

7.3 Visible
The code will process all the game objects with every game loop. Processing game objects includes handling and drawing. When the sprite is visible, it will be handled and drawn. However, when the sprite is invisible, it will be handled but not drawn.

7.4 Animation
The sprite or the game object requires an animation to be represented visually. During the process of the sprite creation, an animation with one or more frames should be specified.

8 ZOrder
To understand ZOrder, we have to simulate a 3D coordinates axis. Look at the diagram below and take note of the x-, y-, and z-axes. The z-axis specifies the depth axis, and the greater the value of the ZOrder, the deeper x y the object is to the screen. For example, the picture with the character has a lower ZOrder (1) while the picture with the ball has a higher ZOrder (2). Therefore, the ball appears to be behind the character. Because it is a 2D application, the ZOrder only affects the displacement order of pictures, without affecting their size. All the game objects are affected by the ZOrder.

Game Implementation Step 1: Adding the Background


The game concept (theory) is the same for all games, but sometimes there are special cases. In Star Trooper, Trooper the special case is that the background object is two instances of class Background, which is derived from Sprite class. In other words, two sprites will play the role of a scrolling background. In order to add a background, we have to add a sprite.

DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

Step 2: Adding Sprites


The sprites visual representation in the game is the animation, which is made of one or more frames. Each frame is made of one picture (image) and a delay. An image is a physical bitmap. Each picture is a class Picture object that holds the bitmap. Picture background=new Picture(Background.bmp, Color.FromArgb(0,255,0)); Where: The first argument is the name of the bitmap. The second argument is the transparency color. The Picture object is added to the game in order to be used by the frame. Game.Add(background); The frame is a class Frame object that uses the picture object. Frame backGroundFrame = new Frame(background, 0); Where: The first argument is a picture object. The second argument is the frame delay. The animation is a class Animation object that is composed of many frames. Animation backGroundAnimation = new Animation(); Each Frame is added to the animation object. backGroundAnimation.Add(backGroundFrame); Create the first background object bg that is derived from the Sprite class. Background bg = new Background(); Add the animation object already created. bg.Add(backGroundAnimation); Set the sprite position. bg.Position = new Point(320, 240); Add the sprite to the game. Game.Add(bg); Set the sprite Size to fit the screen. bg.ScaleX = 640.0f / background.Width; bg.ScaleY = 480.0f / background.Height; Set the background order to 10. bg.ZOrder = 10; DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements Add the sprite to the game. Game.Add(bg); Create the second background object bg2 that is derived from the Sprite class. Background bg2 = new Background(); Add the animation object already created. bg2.Add(backGroundAnimation); Set the sprite position. bg2.Position = new Point(320, 240); Set the sprite Size to fit the screen. bg2.ScaleX = 640.0f / background.Width; bg2.ScaleY = 480.0f / background.Height; Set the background order to 10. bg2.ZOrder = 10; Add the sprite to the game. Game.Add(bg2);

Note: The path of the directory holding the pictures as BMP files, should be specified in the constructor of the StarTrooperGame class. PicturesPath = Application.StartupPath+\\StarTrooperResources\\Pictures\\; Type the following code in the StarTrooperGame.cs file in order to add the background to the game.

Picture background = new Picture("Background.bmp", Color.FromArgb(0 , 255, 0)); Game.Add(background); Frame backGroundFrame = new Frame(background, 0); Animation backGroundAnimation = new Animation(); backGroundAnimation.Add(backGroundFrame); Background bg = new Background(); bg.Add(backGroundAnimation); bg.Position = new Point(320, 240); bg.ScaleX = 640.0f / background.Width; bg.ScaleY = 480.0f / background.Height; bg.ZOrder = 10; Game.Add(bg); Background bg2 = new Background(); bg2.Add(backGroundAnimation); bg2.Position = new Point(320, -240); bg2.ScaleX = 640.0f / background.Width; bg2.ScaleY = 480.0f / background.Height; bg2.ZOrder = 10; Game.Add(bg2);

DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

Step 3: Adding the Trooper Sprite


Type the following code in the StarTrooperGame.cs file in order to create a sprite prototype.

public static Trooper Trooper;


Each picture is a class Picture object that holds the bitmap. Picture trooper01 = new Picture(trooper01.bmp,Color.FromArgb(0,255,0)); Where: The first argument is the name of the bitmap. The second argument is the transparency color. The Picture object is added to the game in order to be used by the frame. Game.Add(trooper01); The frame is a class Frame object that uses the picture object. Frame afTrooper01 = new Frame(trooper01, 5); Where:The first argument is a picture object. The second argument is the frame delay. The animation is a class Animation object that is composed of many frames. Animation trooperAnimation = new Animation(); Each Frame is added to the animation object. trooperAnimation.Add(afTrooper01); Start playing the animation object. trooperAnimation.Play(); Set the loop property of the animation object to be true in order to loop continuously. trooperAnimation.Loop = true; Create a trooper sprite object. Trooper trooper = new Trooper(); Add the animation object already created. trooper.Add(trooperAnimation); Set the sprite position. trooper.Position = new Point(320, 450); Add the sprite to the game. DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

Game.Add(trooper); Trooper = trooper; Note: The path of the directory holding the pictures as BMP files, should be specified in the constructor of the StarTrooperGame class. PicturesPath = Application.StartupPath+\\StarTrooperResources\\Pictures\\; Type the following code in the StarTrooperGame.cs file in order to add two sprites to the game.
Picture trooper01 = new Game.Add(trooper01); Picture trooper02 = new Game.Add(trooper02); Picture trooper03 = new Game.Add(trooper03); Picture trooper04 = new Game.Add(trooper04); Picture trooper05 = new Game.Add(trooper05); Picture trooper06 = new Game.Add(trooper06); Frame Frame Frame Frame Frame Frame afTrooper01 afTrooper02 afTrooper03 afTrooper04 afTrooper05 afTrooper06 = = = = = = new new new new new new Picture("trooper01.bmp", Color.FromArgb(0 , 255, 0)); Picture("trooper02.bmp", Color.FromArgb(0, 255, 0)); Picture("trooper03.bmp", Color.FromArgb(0, 255, 0)); Picture("trooper04.bmp", Color.FromArgb(0, 255, 0)); Picture("trooper05.bmp", Color.FromArgb(0, 255, 0)); Picture("trooper06.bmp", Color.FromArgb(0, 255, 0)); Frame(trooper01, Frame(trooper02, Frame(trooper03, Frame(trooper04, Frame(trooper05, Frame(trooper06, 5); 5); 5); 5); 5); 5);

Animation trooperAnimation = new Animation(); trooperAnimation.Add(afTrooper01); trooperAnimation.Add(afTrooper02); trooperAnimation.Add(afTrooper03); trooperAnimation.Add(afTrooper04); trooperAnimation.Add(afTrooper05); trooperAnimation.Add(afTrooper06); trooperAnimation.Play(); trooperAnimation.Loop = true; Trooper trooper = new Trooper(); trooper.Add(trooperAnimation); trooper.Position = new Point(320, 450); Game.Add(trooper); Trooper = trooper;

Step 4: Adding the Condor Sprite


Type the following code in the StarTrooperGame.cs file in order to create a sprite prototype.

public static Condor Condor;

Each picture is a class Picture object that holds the bitmap. Picture condor01 = new Picture(condor01.bmp, Color.FromArgb(0, 255, 0)); Where: The first argument is the name of the bitmap. The second argument is the transparency color. The Picture object is added to the game in order to be used by the frame. Game.Add(condor01); The frame is a class Frame object that uses the picture object. DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements Frame afcondor01 = new Frame(condor01, 5); Where:The first argument is a picture object. The second argument is the frame delay. The animation is a class Animation object that is composed of many frames. Animation condorAnimation = new Animation(); Each Frame is added to the animation object. condorAnimation.Add(afcondor01); Start playing the animation object. condorAnimation.Play(); Set the loop property of the animation object to be true in order to loop continuously. condorAnimation.Loop = true;

10

Step 5: Adding Another Animation Explosion to the Condor Sprite


Each picture is a class Picture object that holds the bitmap. Picture condorExplosion01 = new Picture(condorExplosion01.bmp, Color.FromArgb(0, 255, 0)); Where: The first argument is the name of the bitmap. The second argument is the transparency color. The Picture object is added to the game in order to be used by the frame. Game.Add(condorExplosion01); The frame is a class Frame object that uses the picture object. Frame afcondorExplosion01 = new Frame(condorExplosion01, 4); Where: The first argument is a picture object. The second argument is the frame delay. The animation is a class Animation object that is composed of many frames. Animation condorExplosion = new Animation(); Each Frame is added to the animation object. condorExplosion.Add(afcondorExplosion01); Start playing the animation object. condorExplosion.Play(); Create a condor sprite object. Condor condor = new Condor(); DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

11

Add the animation to the object already created. condor.Add(condorAnimation); Add the explosion animation to condor sprite. condor.Add(condorExplosion); Add the sprite to the game. Condor = condor; Type the following code in the StarTrooperGame.cs file in order to add two sprites to the game.
Picture condor01 = new Game.Add(condor01); Picture condor02 = new Game.Add(condor02); Picture condor03 = new Game.Add(condor03); Picture condor04 = new Game.Add(condor04); Frame Frame Frame Frame afcondor01 afcondor02 afcondor03 afcondor04 = = = = new new new new Picture("condor01.bmp", Color.FromArgb(0, 255, 0)); Picture("condor02.bmp", Color.FromArgb(0, 255, 0)); Picture("condor03.bmp", Color.FromArgb(0, 255, 0)); Picture("condor04.bmp", Color.FromArgb(0, 255, 0)); Frame(condor01, Frame(condor02, Frame(condor03, Frame(condor04, 5); 5); 5); 5);

Animation condorAnimation = new Animation(); condorAnimation.Add(afcondor01); condorAnimation.Add(afcondor02); condorAnimation.Add(afcondor03); condorAnimation.Add(afcondor04); condorAnimation.Play(); condorAnimation.Loop = true; Picture condorExplosion01 = new Picture("condorExplosion01.bmp", Color.FromArgb(0, 255, 0)); Game.Add(condorExplosion01); Picture condorExplosion02 = new Picture("condorExplosion02.bmp", Color.FromArgb(0, 255, 0)); Game.Add(condorExplosion02); Picture condorExplosion03 = new Picture("condorExplosion03.bmp", Color.FromArgb(0, 255, 0)); Game.Add(condorExplosion03); Frame afcondorExplosion01 = new Frame(condorExplosion01, 4); Frame afcondorExplosion02 = new Frame(condorExplosion02, 3); Frame afcondorExplosion03 = new Frame(condorExplosion03, 4); Animation condorExplosion = new Animation(); condorExplosion.Add(afcondorExplosion01); condorExplosion.Add(afcondorExplosion02); condorExplosion.Add(afcondorExplosion03); condorExplosion.Play(); Condor condor = new Condor(); condor.Add(condorAnimation); condor.Add(condorExplosion); Condor = condor;

DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

12

Note: In this game, by default, sprite classes are created for you in order to only create an object from them (Trooper, Condor, and Fire (later)). Therefore, in order to add a new sprite class (for example, a car) add a file called car.cs and create a class called car that is derived from the Sprite class.
#region Using directives using using using using using System; System.Collections.Generic; System.Drawing; Microsoft.DirectX.DirectInput; Microsoft.DirectX;

#endregion using System.Windows.Forms; namespace StarTrooper { public class Car: Sprite { public Car() { } protected Car(Car car): base(car) { } public override Object Clone() { return new Car(this); } public override void Update() { } } }

Note: Using our game engine, you can even create a new game (totally different storyline) by adding a file called yourgame.cs in which you create a class called yourgame derived from the Game class.

DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Game Design Elements

13

#region Using directives using using using using System; System.Collections.Generic; System.Drawing; System.Windows.Forms;

#endregion namespace StarTrooper { public class YourGame: Game { public YourGame () { } public override void InitializeResources() { } public override void Update() { } } }

Under the program.cs you should create an instance of YourGame.

#region Using directives using System; using System.Windows.Forms; #endregion namespace StarTrooper { static class Program { [STAThread] static void Main() { using (YourGame yourGame = new YourGame()) yourGame.Run(); } } }

DigiPen Game Development C# Webcast Series - Session Four, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Transformations, Collisions & Velocity


DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session Five:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
1 Transformation
1.1 Translations 1.2 Rotations 1.3 Scaling 3 3 4

2 Rectangular Collision
2.1 What Is a Rectangular Collision? 2.2 Detecting Collision or Intersection 2.3 Different Coordinate Systems 4 4 5

3 Velocity
3.1 Direction 3.2 Sprite Direction Using Vector Coordinates (x, y) 3.3 Speed 6 6 7

Game Implementation: Adding movement to the Condor sprite


Step 1: Positioning the Condor Sprite Step 2: Setting Its Vector 7 8

Game Web Cast Project: Transformations, Collisions & Velocity

Transformations Collisions & Velocity


1 Transformation 1.1 Translations
A translation transformation is applied to an object by repositioning it along a straight-line path from one coordinate location to another.

Facts: tx and ty are called translation distances along the x-axis and the y-axis. (tx, ty) is called the translation vector.

1.2 Rotations
Rotations rotate a point along a circular path. To specify a rotation transformation we need: An angle. A pivot point (reference for the rotation). A rotation axis. In 2D, the axis is perpendicular to the x-y plane. It is the z-axis. To specify a positive (counterclockwise) and a negative (clockwise) rotation angles.

DigiPen Game Development C# Webcast Series - Session Five, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Transformations, Collisions & Velocity

1.3 Scaling
Scaling alters the size of objects. It requires scaling factors sx and sy that determine the change in the x-direction x and the y-direction. Scaling is carried out by multiplying the coordinate values of each vertex of object with the y scaling factors. Example: x = sx x y = sy y If sx sy, we have a uniform scaling; if sx sy, we have a non-uniform scaling.

2 Rectangular Collision 2.1 What Is a Rectangular Collision?


A rectangular collision is a rectangle relative to each frame. It sets the boundaries of a sprite and is used to check for collisions.

2.2 Detecting Collision or Intersection

Facts: The limit of the first bounding box is determined by: L = 1, R = 3, T = 3, and B = 1. The limit of the second bounding box is determined by: L = 2, R = 4, T = 4, and B = 2. Find the following: o Max(L, L) = Max(1, 2) = 2 o Min(R, R) = Min(3, 4) = 3 o Max(B, B) = Max(1, 2) = 2 o Min(T, T) = Min(3, 4) = 3 The two bounding boxes intersect if: o Max(Max(L,L) Min(R, R), Max(B, B) Min(T, T) )<=0. DigiPen Game Development C# Webcast Series - Session Five, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Transformations, Collisions & Velocity o Max(2 3, 2 - 3) )<=0 => Max(-1, -1) = -1 Result: The two bounding boxes intersect and form a new rectangle: L=2, R=3, T=3, and B=2.

Facts: The limit of the first bounding box is determined by: L = 1.2, R = 2.8, T = 2.6, and B = 1. The limit of the second bounding box is determined by: L = 3.6, R = 5.3, T = 4.5, and B = 3. Find the following: o Max(L, L) = Max(1.2, 3.6) = 3.6 o Min(R, R) = Min(2.8, 5.3) = 2.8 o Max(B, B) = Max(1, 3) = 3 o Min(T, T) = Min(2.6, 4.5) = 2.6 The two bounding boxes intersect if: o Max(Max(L,L) Min(R, R), Max(B, B) Min(T, T) )<=0. o Max(3.6 2.8, 3 2.6) )<=0 => Max(0.8, 0.4) = 0.8 >0 Result: The two bounding boxes do not intersect since the result is positive.

2.3 Different Coordinate Systems


A screen coordinate system is where top left is (0, 0). For example:

DigiPen Game Development C# Webcast Series - Session Five, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Transformations, Collisions & Velocity

The same equation of the previous section applies with the following modifications: Nothing changes regarding the left and the right. Since the top has a value less than the bottom: o Max(B, B) will be Min(B, B), and o Min(T, T) will be Max(T, T). Finally, the equation should be: o Max(Max(L,L) Min(R, R), Min(B, B) Max(T, T) )<=0.

3 Velocity
The velocity V(x, y) is the speed and direction describing a moving object.

3.1 Direction
Unit vectors are used to indicate the direction. Example: uv (x/l , y/l) where l = (x2 + y2)

3.2 Sprite Direction Using Vector Coordinates (x, y)


To find the sprite direction using vector coordinates (x, y): DigiPen Game Development C# Webcast Series - Session Five, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Transformations, Collisions & Velocity Get the length L of the vector (x, y) L = (x2 + y2) The unit vector will be: X=x/L Y=y/L

x
According to a different coordinate system, X=x/L Y = -y / L y the unit vector will be:

y X = 0, Y = -1 Upward X = 0, Y = 1 Downward

X = 1, Y = 0 Forward

X = -1, Y = 0 Backward

3.3 Speed
The speed is the magnitude of V(x, y). Example: S = (x2 + y2)

Game Implementation: Adding movement to the Condor sprite Step 1: Positioning the Condor Sprite
Create a copy of the Condor object with all its properties and values and called it condor. Condor condor = (Condor)Condor.Clone(); Set the sprite position. condor.Position = new Point(m_Random.Next(-100, 740), -150); Add the sprite to the game. Game.Add(condor); Type the following code in the StarTrooperGame.cs file in the StarTrooper class under public override void Update() function inside the if (){ } statement.

Condor condor = (Condor)Condor.Clone(); condor.Position = new Point(m_Random.Next(-100, 740), -150); Game.Add(condor);


DigiPen Game Development C# Webcast Series - Session Five, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Transformations, Collisions & Velocity

Step 2: Setting Its Vector


Save the reference of the existing sprite Trooper in the StarTrooperGame class object b. Trooper b = StarTrooperGame.Trooper; v is a class Vector2 object with a direction to the Trooper position. Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y Position.Y); Normalize the vector by changing the speed value to 1 while preserving the direction. v.Normalize(); Assign the new setting to the velocity. Velocity = v; The condor will flip its animation relative to the troopers position. if (v.X >= 0) ScaleX = 1; else ScaleX = -1; Type the following code in the StarTrooperSprite.cs file in the Condor class under public override void Update() function.
Trooper b = StarTrooper.Trooper; Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y - Position.Y); v.Normalize(); Velocity = v; if (v.X >= 0) ScaleX = 1; else ScaleX = -1;

DigiPen Game Development C# Webcast Series - Session Five, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Dynamic Sprites Creation


DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session Six:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
1 Dynamic Sprite Basics
1.1 How To Create a Sprite Dynamically 1.2 When Are Dynamic Sprites Used? 3 3

2 Input
2.1 Keyboard 2.2 Pressed 2.3 Triggered 4 4 4

Game Implementation
Step 1: Create a Prototype Step 2: Enter Input Information 4 5

Game Web Cast Project: Dynamic Sprites Creation

Dynamic Sprite Creation


1 Dynamic Sprite Basics
Dynamic sprite creation means creating sprites at run time (during the game play).

1.1 How To Create a Sprite Dynamically


To create a sprite dynamically, we need a Sprite Prototype. The sprite prototype will be the basis sprite from which sprite copies are created. The sprite prototype is not handled and it is not displayed. Example: Type the following code in the StarTrooperGame.cs file in order to create a sprite prototype:

public static Fire Fire;

Under public override void InitializeResources() function, type:


Picture fire01 = new Picture("fire01.bmp", Color.FromArgb(0, 255, 0)); Game.Add(fire01); Picture fire02 = new Picture("fire02.bmp", Color.FromArgb(0, 255, 0)); Game.Add(fire02); Frame affire01 = new Frame(fire01, 5); Frame affire02 = new Frame(fire02, 5); Animation fireAnimation = new Animation(); fireAnimation.Add(affire01); fireAnimation.Add(affire02); fireAnimation.Play(); fireAnimation.Loop = true; Fire fire = new Fire(); fire.ZOrder = -10; fire.Add(fireAnimation); Fire = fire;

Note: The sprite fire is not added to the game, so it is not active. After creating a prototype, create a copy of it. Then set the copy properties like position, velocity, etc. To create a copy of the prototype, type the following: Fire fire = (Fire)StarTrooperGame.Fire.Clone(); fire.Position = new PointF(Position.X, Position.Y 35); fire.Velocity = new Vector2(0, -4); // up direction, speed=4 Game.Add(fire);

1.2 When Are Dynamic Sprites Used?

Dynamic sprites are used when we need to create sprites and use them during the game play. For example, dynamic sprites are used to create bullets to shoot or a new life. DigiPen Game Development C# Webcast Series - Session Six, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Dynamic Sprites Creation

2 Input 2.1 Keyboard


A keyboard is a device that returns character codes, the American Standard Code for Information Interchange (ASCII). Since the computer machine can only understand binary numbers, numbers represent the character set. For example, the number 65 in base 10 (which is 01000001 in binary) represents uppercase character A.

2.2 Pressed
The action assigned to the keyboard input will be executed as long as the key is pressed. To get the status of a key (if it is pressed or not), call the IsPressed() function. Example: Keyboard.IsPressed(Key.UpArrow) This function returns a Boolean value even TRUE (1) or FALSE (0).

2.3 Triggered
The action assigned to the keyboard input will be executed when the key status changes from not pressed to pressed. To get the status of a key (if it is triggered or not), call the IsTriggered() function. Example: Keyboard.IsTriggered(Key.Space) This function returns a Boolean value even TRUE (1) or FALSE (0).

Game Implementation
In the game, the arrow keys are used to move the main sprite (trooper sprite). Pressing the Up arrow moves the trooper upward. Pressing the Down arrow moves the trooper downward. Pressing the Left arrow moves the trooper to the left. Pressing the Right arrow moves the trooper to the right. Also, when the Space key is triggered, then the main sprite (trooper) will shoot a bullet (fire sprite), which is a sprite created dynamically.

Step 1: Create a Prototype


Use the steps noted above (1.1) to create your prototype (fire sprite for shooting).

DigiPen Game Development C# Webcast Series - Session Six, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Dynamic Sprites Creation

Step 2: Enter Input Information


Type the following code in the StarTrooperSprites.cs file in the Trooper class under public override void Update() function:

int vx = 0, vy if (Position.Y vy = -2; if (Position.Y vy = 2; if (Position.X { vx = -2; ScaleX = } if (Position.X { vx = 2; ScaleX = }

= 0; > 50 && Keyboard.IsPressed(Key.UpArrow)) // if trooper is under y=50 then go upward < 450 && Keyboard.IsPressed(Key.DownArrow)) // if trooper is over y=450 then go upward > 30 && Keyboard.IsPressed(Key.LeftArrow)) -1; // go to the left // left flip trooper

< 610 && Keyboard.IsPressed(Key.RightArrow)) 1; // go to the right // right flip trooper // set new velocity for Trooper

Velocity = new Vector2(vx, vy);

// if space bar is triggered if (Keyboard.IsTriggered(Key.Space)) { // dynamically create a new sprite Fire fire = (Fire)StarTrooper.Fire.Clone(); fire.Position = new PointF(Position.X, Position.Y - 35); fire.Velocity = new Vector2(0, -4); Game.Add(fire); // set the fire sprite active }

DigiPen Game Development C# Webcast Series - Session Six, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Adding Sound & Text


DigiPen Institute of Technology
5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

Session Seven:

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

1 Sound

Contents
3 3 3 3

1.1 Sound Effects 1.2 Game Implementation: Adding Sound Effects 1.3 Music 1.4 Game Implementation: Adding Music

2 Text
2.1 What Is Text? 2.2 Why Do We Use It? 2.3 Game Implementation: Adding Text 4 4 4

Game Web Cast Project: Adding Sound & Text

Adding Sound and Text


1 Sound 1.1 Sound Effects
A sound effect is a sound that is triggered by a certain event in the game. This sound can be triggered by a certain action or event, like firing a bullet, a tank explosion, or a ball hitting a wall.

1.2 Game Implementation: Adding Sound Effects


The sound effect is a class Sound object that holds a WAV file. public static Sound Shoot; Shoot = new Sound(Shoot.wav); The class Sound object is then added to the game. Game.Add(Shoot); Note: The path of the directory holding the sound effect WAV files should be specified in the constructor of the StarTrooperGame class. SoundPath = Application.StartupPath + \\StarTrooperResources\\Sounds\\; Type the following code in the StarTrooperGame.cs file in order to add two sound effects to the game:

public static Sound Shoot; public static Sound Die;

Under public override void InitializeResources() function, type:

Shoot = new Sound(Shoot.wav); Game.Add(Shoot); Die = new Sound(Die.wav); Game.Add(Die);

1.3 Music
The music is the sound that is played on the background while the game is being played. It can be repetitive or just played once. Music files can be midi, wave, or compressed.

1.4 Game Implementation: Adding Music


The music is a class Music object that holds a MIDI file. public static Music Music; DigiPen Game Development C# Webcast Series - Session Seven, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Adding Sound & Text

Music = new Music(music.wav); The class Music object is then added to the game. Game.Add(Music); Since the music is played continuously in the background, start playing it. Music.Play(); Note: The path of the directory holding the music MIDI files should be specified in the constructor of the StarTrooperGame class. MusicPath = Application.StartupPath + \\StarTrooperResources\\Music\\; Type the following code in the StarTrooperGame.cs file in order to add and start playing the music:

public static Music Music;

Under public override void InitializeResources() function, type:

Music = new Music(music.wav); Game.Add(Music); Music.Play();

To play the music continuously, add the following code in the StarTrooperGame.cs file under public override void Update() function:

if (!Music.IsPlaying) Music.Play();

2 Text 2.1 What Is Text?


The text is a string that is displayed on the screen. The text holds properties such as its value, font, color, position, and whether it is visible and active.

2.2 Why Do We Use It?


The text can be used for several reasons: To display the content of a text file. To display the value of a variable. To display certain messages during the game.

2.3 Game Implementation: Adding Text


First, set the font type of the text. The font is a class Font object. DigiPen Game Development C# Webcast Series - Session Seven, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Adding Sound & Text

Font font = new Font(Arial, 14.0f, FontStyle.Regular); Where: The first parameter is the font type. The second parameter is the font size. The third parameter is the font style. Add the created Font object to the game in order to be used by a text. Game.Add(font); Then, create the text to display. The text is a class Text2D object that holds the text displayed on the screen. public static Text2D Score; Text2D Score = new Text2D(font); Specify the text value, position, and color. Score.Text = Score: 0; Score.Position = new Point(150, 0); Score.Color = Color.Red; Specify the text to act like a number. StarTrooperGame.Score = Score; (Skip this step in order to display a string text) Finally, the class Text2D object is then added to the game in order to be seen. Game.Add(Score); Type the following code in the StarTrooperGame.cs file in order to display the score and number of shot bullets (Shoots) texts:

public static Text2D Shoots; public static Text2D Score;

Under public override void InitializeResources() function, type:

DigiPen Game Development C# Webcast Series - Session Seven, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Adding Sound & Text

Font font = new Font(Arial, 14.0f, FontStyle.Regular); Game.Add(font); Text2D Shoots = new Text2D(font); Shoots.Text = Shoots: 0; Shoots.Position = new Point(0, 0); Shoots.Color = Color.Green; StarTrooper. Shoots = Shoots; Game.Add(Shoots); Text2D Score = new Text2D(font); Score.Text = Score: 0; Score.Position = new Point(150, 0); Score.Color = Color.Red; StarTrooper.Score = Score; Game.Add(Score);

Optional: To display the Frame rate on the screen, first set the Frame rate value in the constructor of the StarTrooperGame class. FrameRate = 60; Then type the following code in the StarTrooperGame.cs file under public override void InitializeResources() function:

Text2D FrameRate = new FrameRate(font); Game.Add(FrameRate);

Note: In order to add a new text class, add a file called yourText.cs and create a class called YourText, which is derived from the Text2D class.

#region Using directives using using using using System; System.Collections.Generic; System.Drawing; Microsoft.DirectX.DirectInput;

#endregion namespace StarTrooper { class FrameRate : Text2D { public FrameRate(Font font): base(font) { } public override void Update() { } } }

DigiPen Game Development C# Webcast Series - Session Seven, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

DigiPen Institute of Technology


Presents

Session Eight:

Behavior

DigiPen Institute of Technology


5001 150th Ave NE, Redmond, WA 98052 Phone: (425) 558-0299 www.digipen.edu

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
1 Behavior Overview 2 Game Objects Behavior
2.1 Game Implementation: Condor Behavior 2.2 Game Implementation: Fire Behavior 3 5

Game Web Cast Project: Behavior

Behavior
1 Behavior Overview
A behavior is a set of functions, defined by the user, which describes how a game and its objects should behave and act. You can define a behavior to the game and to some of its objects, like sprites and texts. The functions assigned in a game object behavior (the game object is an instance of a class derived from the Game class) are executed successively.

2 Game Objects Behavior


In StarTrooper, the game behavior is divided into several objects that make the game. These objects are: StarTrooper Trooper, Condor, and Fire. The behavior of the Trooper object is a combination of several kinds of behavior: Movement Behavior (already seen) Shooting (already seen) The behavior of the Condor object depends on the collision detection. If it collides with Fire, which means that Fire hits it, it will explode and die with a consequent score increment. If it is hit by the Trooper, which means that condor wins, Condor will die and the score will decrement. Therefore, we have: Condor Behavior The behavior of the Fire object is simple. It will die if it is hit by Condor, otherwise it will continue. Therefore, we have: Fire Behavior

2.1 Game Implementation: Condor Behavior


Create a list variables collidedSprites that contains all the sprites with which the enemy collides: List<Sprite> collidedSprites = Game.GetCollidedSprites(this); Test if the list is empty or not; if not empty: if (collidedSprites != null) o For each element of the list: foreach (Sprite s in collidedSprites) o Test if this element is the fire object: if (s is Fire) { //The enemy object will die RaptorGame.Die.Play(); // Playing the enemy explosion animation AnimationIndex = 1; //Incrementing the score by one DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Behavior m_Score++; //Updating the value of the score displayed //on the screen RaptorGame.Score.Text = Score: + m_Score.ToString(); //Removing the enemy sprite from the game Game.Remove(s); break; } o Test if this element is the main character: else if (s is Bat) { m_CollisionWithBat = true; //The enemy object will die RaptorGame.Die.Play(); //The Animation will stop looping Animation.Stop(); //Decrementing the score by one m_Score--; //Updating the value of the score displayed //on the screen RaptorGame.Score.Text = Score: + m_Score.ToString(); break; } Testing if the animation of the Condor sprite is the explosion animation or not: if (AnimationIndex != 1) { } else { //Testing if it is the last frame of the explosion animation if (Animation.PlayingLastFrame) //delete the explosion sprite Game.Remove(this); } Delete the following code in the StarTrooperSprites.cs file under public override void Update() function:
Trooper b = StarTooper.Trooper; Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y - Position.Y); v.Normalize(); Velocity = v; if (v.X >= 0) ScaleX = 1; else ScaleX = -1;

Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:

DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Behavior

Trooper b = StarTrooper.Trooper; if (AnimationIndex != 1) { Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y - Position.Y); v.Normalize(); Velocity = v; if (v.X >= 0) ScaleX = 1; else ScaleX = -1; List<Sprite> collidedSprites = Game.GetCollidedSprites(this); if (collidedSprites != null) { foreach (Sprite s in collidedSprites) { if (s is Fire) { StarTrooper.Die.Play(); AnimationIndex = 1; m_Score++; StarTrooper.Score.Text = "Score: " + m_Score.ToString(); Game.Remove(s); break; } else if(s is Trooper) { m_CollisionWithTrooper = true; StarTrooper.Die.Play(); Animation.Stop(); m_Score--; StarTrooper.Score.Text = "Score: " + m_Score.ToString(); break; } } } } else { if (Animation.PlayingLastFrame) Game.Remove(this); }

2.2 Game Implementation: Fire Behavior


If the Condor hits the Fire it will be deleted: this is already seen with the Condor behavior. If the Fire does not hit anything, it will delete itself if it is out of the game: if (y < -100) Game.Remove(this); Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:
if (y < -100) Game.Remove(this);

When the spacebar is triggered, a new Fire sprite is created, the Shoot sound effect is played, and the text Shoots is incremented and displayed. Delete the following code in the StarTrooperSprites.cs file under public override void Update() function:

DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Behavior

if (Keyboard.IsTriggered(Key.Space)) { Fire fire = (Fire)StarTrooper.Fire.Clone(); fire.Position = new PointF(Position.X, Position.Y - 35); fire.Velocity = new Vector2(0, -4); Game.Add(fire); }

Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:
if (Keyboard.IsTriggered(Key.Space)) { Fire fire = (Fire)StarTrooper.Fire.Clone(); fire.Position = new PointF(Position.X, Position.Y - 35); fire.Velocity = new Vector2(0, -4); Game.Add(fire); StarTrooper.Shoot.Play(); m_Shoots++; StarTrooper.Shoots.Text = "Shoots:" + m_Shoots.ToString(); }

DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0 Copyright 2005 DigiPen (USA) Corporation.

Das könnte Ihnen auch gefallen