Programming in C#

14. Games

Writing games in Visual Studio is much more user friendly that it has been before. Before you had to learn the DirectX API and try to implement its complex functions and 3D functionality for the learner or average developer making it heard to develop anything useful in a short space of time. The .NET Framework now has builtin functionality to make it easier and now with Windows Presentation Framework (WPF) windows rathe than the older WinForms, games can now be easier to develop.

14.1 Developing a game

To write a game you need to design and develop it before you start using Visual Studio. So you need a plan of what the game does and how to do it:

a) Description of the game. Here you need what the game is about, and what the goal of the game is.

b) Player's character. You need to descibe and design the player's character which could be a human, an animal, a tank, a car or whatever.

c) Enemy character.You need to descibe and design the enemy characters which can be other human chacters,animals, monsters, tanks, etc.

d) Background. You will need to add some background pictures, and decide if it static, scrolling horizontally or vertically, jump screen or 3D.

e) Character interaction. Then you need to decide how characters interact, whether they can inflict damage or provide health and how it affects scoring.

f) Sound and music. For any good game, some background sound, whether its opening music before or during the game, do characters speech to one another, animal or monster or vehicle sounds, and any explosion sounds etc.

g) Game end. Finally you need to decide how a level or game ends. Do the characters need to collect things, reach a certain location or point for game to progress and how that affects scoring. Also, any dealth scenes may need to be designed if the player dies.

14.2 Background

To set background in a WFP C# program can be done via the Canvas and use the Rectangle tags.
e.g.

<Canvas x:Name="myCanvas" Focusable="True" KeyDown="Canvas_KeyDown" KeyUp="Canvas_KeyUp" Background="LightBlue">
<Rectangle Name="background" Height="400" Width="1252" Fill="Orange"/>

</Canvas>
</Window>

In the C# Code, you can create an ImageBrush and set its Image Source attribute to an image from a BitmapImage file from the images folder for the applicaiton:
e.g.

ImageBrush backgroundSprite = new ImageBrush();

// first set the background sprite image
backgroundSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/background.gif"));

For scrolling images, you need to make the bitmap image bigger than the screen, and using a timing thread, move the image smotthly either across or down (or up) the screen, and loop it or load a new bitmap the image to a new area or new level.

14.3 Characters

To play a game you need characters in it to move around and you would load Bitmap images the make how they look and ImageBrushes to place them on the screen. You can change the image while they move and this provides animation, such as a man walking, running and jumping, or a ship diving, rising or banking side to side for instance. Use the direction of the mouse, joystick or keyboard to indicate what to animate and how.To ensure smooth animation use a timed thread to apply changes to images.

// An animated running sprite
Rectangle player;
ImageBrush playerSprite = new ImageBrush();

private void runSprite(double i)
{
switch (i)
{

case 1:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_01.gif"));
   break;
case 2:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_02.gif"));
   break;
case 3:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_03.gif"));
   break;
case 4:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_04.gif"));
   break;
case 5:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_05.gif"));
   break;
case 6:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_05.gif"));
   break;
case 7:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_07.gif"));
   break;
case 8:
   playerSprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Runner_08.gif"));
   break;

}
// assign the player rectangle to the player sprite
player.Fill = playerSprite;
}

To position and move characters, you can use the SetLeft() and SetTop() functions which sets the x,y positions on the screen. To retreive the current position use the GetLeft() and GetTop() functions, and to move a character you add a value to move a character left or right using a negative or positive value or up and down using a negative or positive value.
E.g.

// set the player x to 110 and y to 140
Canvas.SetLeft(player, 110);
Canvas.SetTop(player, 140);

// move the player character down using the speed integer
Canvas.SetTop(player, Canvas.GetTop(player) + speed);

14.4 Character interaction

For game to work, then characters need to interact either with each other, or shoot or hit one another and interact with obstacles such as walls and other objects. This is done by using Rectangles. So if a rectangle for the player intersects with a another character or a bullet or something with a charcter, then you have a 'hit'. Then your game can determine what to do, such as reduce health, blow up or die etc. The IntersectsWith function can determine if one sprite's rectangle area with another sprite's rectangle area, then you have a hit.
e.g.

playerHitBox = new Rect(Canvas.GetLeft(player), Canvas.GetLeft(player), player.Width, player.Height);
groundHitBox = new Rect(Canvas.GetLeft(ground), Canvas.GetLeft(ground), ground.Width, ground.Height);
obstacleHitBox = new Rect(Canvas.GetLeft(obstacle), Canvas.GetLeft(obstacle), obstacle.Width, obstacle.Height);

// check player and ground collision
// If player hits the ground
if (playerHitBox.IntersectsWith(groundHitBox))
{
   // Do something with character,
}

14.5 Music and Sound

To play music and play sound, it is best using sampled or created music saved as Wave files and then loaded and played back using SoundPlayer() function.

file = "pack://application:,,,/wave/aargh.wav";

if (playerHitBox.IntersectsWith(groundHitBox))
{
    SoundPlayer snd = new SoundPlayer();
     snd.SoundLocation = file;
     snd.Play();
}

14.6 Links for writing Games in C#