41
1 A First Game Program Session 11.1

11 A First Game Program Session 11.1. Session Overview Begin the creation of an arcade game Learn software design techniques that apply to any form

Embed Size (px)

Citation preview

11

A First Game Program

Session 11.1

Session Overview

Begin the creation of an arcade game

Learn software design techniques that apply to any form of game development

Find out more about how C# programs are constructed and executed

See how to properly organize the content of a game and the program itself

Perform some simple refactoring of a C# solution

Chapter 11.1: A First Game Program 2

Game Idea: Bread and Cheese

The starting point for our game is the contents of a shopping bag

This contains some food that we bought

We are going to use these objects as the basis of a game where the bread hits the cheese around the screen

Chapter 11.1: A First Game Program 3

Creating Game Graphics

You really need someone who can work with images to create good game graphics

This might mean involving an artist in your game development

The graphics editor that you use must be able to work with transparency

This makes it possible to create natural looking game objects

Chapter 11.1: A First Game Program 4

Graphics Editors

I use Photoshop Elements (tm) to make my game graphics

A good free solution is Paint.NET: www.getpaint.net

This lets you manipulate images in layers and supports transparency

Chapter 11.1: A First Game Program 5

Image File Formats

XNA can use image files in many formats

The jpg and gif formats can be used, but they do not support transparency

I use the PNG format for all my images

You should also make sure that your images are not too large

You don’t need more than a few hundred pixels for these sprite images

You can use Paint.NET to resize images

Chapter 11.1: A First Game Program 6

Projects, Resources, and Classes

To start making a game you create a new project in Microsoft Visual Studio

This serves as the container for all the game resources

Before we build our game, it is worth taking a look at how the elements of the project fit together and how best to organize your solution

This knowledge will make your programs much easier to look after in the future

Chapter 11.1: A First Game Program 7

The BreadAndCheese Solution

We are going to create a solution called BreadAndCheese

This contains all the program files and game content

Visual Studio organizes the content into a set of folders

We can add our own folders

Chapter 11.1: A First Game Program 8

Adding a Folder

A folder is a place in the computer filestore where you can put things you want to store in one place

All the tracks from a particular album are stored in a single folder with your music

You can create your own folders to organize your content

Chapter 11.1: A First Game Program 9

Creating an Images Folder

I like to keep image and sound resources separate

This makes it easier to reuse the files as I can just take the entire contents of the folder

I’ve therefore created an Images folder in the Content of the BreadAndCheese game

Chapter 11.1: A First Game Program 10

Adding Content to a Folder

You can add content to a folder in exactly the same way as before

In BreadAndCheese project , the Images folder now contains the images for the Bread and Cheese sprites

We can create folders inside folders if we like

Chapter 11.1: A First Game Program 11

Loading Content from Folders

When the textures are loaded you need to add the folder name to the asset name so that the Content Manager can find the asset

The / character is used to separate folder names in the path to the asset

If you get the path wrong the game will compile OK but will fail when it runs

Chapter 11.1: A First Game Program 12

breadTexture = Content.Load<Texture2D>("Images/Bread");cheeseTexture = Content.Load<Texture2D>("Images/Cheese");breadTexture = Content.Load<Texture2D>("Images/Bread");cheeseTexture = Content.Load<Texture2D>("Images/Cheese");

Game Program Files

We are very familiar with the Game1.cs file

This is the file that contains the Draw and Update methods that make the game work

However this is not the only C# program file in the game

There is also a Program.cs file

This is actually the file that makes the game run

Now we are going to take a look at this file

Chapter 11.1: A First Game Program 13

The Program.cs File

The Program.cs file is what actually runs our game

Chapter 11.1: A First Game Program 14

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

The Using Statement

This is a using statement

Chapter 11.1: A First Game Program 15

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

Identifying a Namespace with Using

• The using keyword is used to identify a namespace to use

• This one tells the compiler to use the System namespace

• A namespace is somewhere for the compiler to look to find descriptions of resources

• Lots of important C# classes, including DateTime, are described in the System namespace

Chapter 11.1: A First Game Program 16

using System;using System;

Creating Our Own Namespace

This creates a namespace for our game program

Chapter 11.1: A First Game Program 17

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

Creating a Namespace

• The designers of C# created a namespace called System where they put system utilities

• This statement creates a namespace with the name BreadAndCheese (to match the game project)

• It stops the names of our items from clashing with those from other C# programs

• Other programs can locate items in the BreadAndCheese namespace by using it

Chapter 11.1: A First Game Program 18

namespace BreadAndCheesenamespace BreadAndCheese

Fully-Qualified Names in Namespaces

You can use objects from namespaces without having to have a using statement at the top of the file

You do this by putting the namespace in front of the name

This “fully-qualified name” can also be used if there is a name “clash” between two namespaces If you use two namespaces that both hold an

item with a particular name

Chapter 11.1: A First Game Program 19

System.DateTime currentTime = System.DateTime.Now;System.DateTime currentTime = System.DateTime.Now;

Creating a Program Class

This creates a static class called Program

Chapter 11.1: A First Game Program 20

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

The Program Class

• A class is a collection of variables and methods

• The Program class is the class that actually starts the program going

• It is static because it only contains static members

• Static in C# means “always present”

• There is no need to ever make a Program instance

Chapter 11.1: A First Game Program 21

static class Program{ // Program class members go here...}

static class Program{ // Program class members go here...}

Declaring the Main Method

This creates a namespace for our game program

Chapter 11.1: A First Game Program 22

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

The Main Method

• This declares the Main method for this project

• It is static (which means always there)

• It returns nothing (it is void)

• It is given an array of strings as a parameter

• Main is the method that is called to start a C# program running

Chapter 11.1: A First Game Program 23

static void Main(string[] args){ // Main method statements go here}

static void Main(string[] args){ // Main method statements go here}

Creating a Game1 Instance and Using It

This is another use of the keyword using

Chapter 11.1: A First Game Program 24

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

The Other Face of Using

• This form of using is a way to help the garbage collector and tell it when an object can be removed

• The using construction creates an object and gives the only block of statements in which it is used

• When that block is finished the object can be removed from memory

Chapter 11.1: A First Game Program 25

using (Game1 game = new Game1()){ // Code that uses the game1 instance}// When we get here game1 can be destroyed

using (Game1 game = new Game1()){ // Code that uses the game1 instance}// When we get here game1 can be destroyed

Running the Game

This statement calls the Run method on the game

Chapter 11.1: A First Game Program 26

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

using System;namespace BreadAndCheese{ static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }}

Calling the Run Method on an XNA Game

• The Run method is the method that starts everything going in an XNA game:

• It calls Initialize

• It calls LoadContent

• It starts a process that calls Update and Draw 60 times a second

• You can think of it as the Main method for XNA

• When the Run method finishes the game ends

Chapter 11.1: A First Game Program 27

game.Run();game.Run();

The Program Class

There is actually no need to change the content of the Program class

The class is created automatically when Microsoft Visual Studio creates a new project

However it is useful to know how it fits together and what really happens when you run a program

If you make a command line program (not a game) the Program.cs file is the only one you get, and you can put your code directly into the Main method

Chapter 11.1: A First Game Program 28

Renaming the Game1 Class

One thing that we should do however is change the name of our game class from Game1

This is really just a placeholder for a name which has a bit more meaning

The game class we are making should really be called BreadAndCheeseGame

Visual Studio makes this very easy

Chapter 11.1: A First Game Program 29

Using Rename in Visual Studio

You can rename an item by using right-click on it and then typing in the required name

Renaming things so that their names are more meaningful is called “refactoring” and is a great way to make your programs easier to understand

Chapter 11.1: A First Game Program 30

Entering the New Name

Make sure that you don’t change the file extension from “.cs” though, as this will stop your program from building

This will rename the file as stored on the disk, and update the reference to it in the project file

Chapter 11.1: A First Game Program 31

Renaming the Class

If you rename a program file Visual Studio will offer to rename the class in the file as well

This is very useful, and so you should say yes so that the class Game1 will be renamed to BreadAndCheeseGame

Chapter 11.1: A First Game Program 32

1. Renaming Game Classes Files

Chapter 11.1: A First Game Program 33

This shows how easy it is to rename a game class

From now on I will expect classes to always have the correct names

Summary

A good solution is a well organized one

C# provides a namespace mechanism to allow a programmers to manage the names of their items

An XNA game is actually started by code in a Program class which calls a static Main method

In C# the word static means “always present”

The using construction makes garbage collection easier

Names of items should always reflect what they are

Chapter 11.1: A First Game Program 34

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually.

Chapter 11.1: A First Game Program 35

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually.

Chapter 11.1: A First Game Program 36

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually.

Chapter 11.1: A First Game Program 37

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually.

Chapter 11.1: A First Game Program 38

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually.

Chapter 11.1: A First Game Program 39

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually

Chapter 11.1: A First Game Program 40

True/False Revision Quiz

Visual Studio projects can contain folders.

Static means that a variable cannot be changed.

A namespace must start with the letter n.

A C# program starts by calling the Run method.

The using construction is used to make a program smaller.

You have to create the Program.cs file manually.

Chapter 11.1: A First Game Program 41