Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 11 A First Game Program Session 11.1

2 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  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 Program2

3 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  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 Program3

4 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  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 Program4

5 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  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 Program5

6 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  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 Program6

7 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  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 Program7

8 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  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 Program8

9 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  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 Program9

10 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  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 Program10

11 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  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 Program11

12 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  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 Program12 breadTexture = Content.Load ("Images/Bread"); cheeseTexture = Content.Load ("Images/Cheese");

13 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  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 Program13

14 The Program.cs File  The Program.cs file is what actually runs our game Chapter 11.1: A First Game Program14 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

15 The Using Statement  This is a using statement Chapter 11.1: A First Game Program15 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

16 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 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 Program16 using System;

17 Creating Our Own Namespace  This creates a namespace for our game program Chapter 11.1: A First Game Program17 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

18 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 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 Program18 namespace BreadAndCheese

19 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  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 Program19 System.DateTime currentTime = System.DateTime.Now;

20 Creating a Program Class  This creates a static class called Program Chapter 11.1: A First Game Program20 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

21 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 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 Program21 static class Program { // Program class members go here... } static class Program { // Program class members go here... }

22 Declaring the Main Method  This creates a namespace for our game program Chapter 11.1: A First Game Program22 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

23 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 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 Program23 static void Main(string[] args) { // Main method statements go here } static void Main(string[] args) { // Main method statements go here }

24 Creating a Game1 Instance and Using It  This is another use of the keyword using Chapter 11.1: A First Game Program24 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

25 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 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 Program25 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

26 Running the Game  This statement calls the Run method on the game Chapter 11.1: A First Game Program26 using System; namespace BreadAndCheese { static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

27 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 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 Program27 game.Run();

28 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  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 Program28

29 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  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 Program29

30 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  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 Program30

31 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  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 Program31

32 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  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 Program32

33 1. Renaming Game Classes Files Chapter 11.1: A First Game Program33  This shows how easy it is to rename a game class  From now on I will expect classes to always have the correct names  This shows how easy it is to rename a game class  From now on I will expect classes to always have the correct names

34 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  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 Program34

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.  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 Program35

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.  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 Program36

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.  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 Program37

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.  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 Program38

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.  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 Program39

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  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 Program40

41 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.  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 Program41


Download ppt "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."

Similar presentations


Ads by Google