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.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Advertisements

Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Video Game Design Lesson 1. Game Designer Person involved in the development of a video game Person involved in the development of a video game Usually.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Code Club Session 3 Shark Eats Fish. Picture of finished product here.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Introduction to a Programming Environment
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
Games and Simulations O-O Programming in Java The Walker School
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Getting started with Microsoft.NET Gadgeteer Comberton Village College Gadgeteer Club.
CHAPTER 1 XNA Game Studio 4.0. Your First Project A computer game is not just a program—it is also lots of other bits and pieces that make playing the.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
A First Program Using C#
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
Introduction to VB.NET Tonga Institute of Higher Education.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Topics Introduction Hardware and Software How Computers Store Data
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Week 1 - Wednesday.  What did we talk about last time?  Syllabus  Colors  RGB  CMYK  HSL and HSV.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
11 Adding Sounds Session 7.1. Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Game Maker – Getting Started What is Game Maker?.
Java Methods Methods contain a group of instructions that together perform a single task. For example if I want to perform the task of “making a pizza”,
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
M1G Introduction to Programming 2 5. Completing the program.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
Object Oriented Software Development 4. C# data types, objects and references.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Building Packages BCIS 3680 Enterprise Programming.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
Know what a computer is used for Understand the difference between hardware and software Be able to describe the way that data is stored in a computer.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
11 Computers, C#, XNA, and You Session 1.1. Session Overview  Find out what computers are all about ...and what makes a great programmer  Discover.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Topics Introduction Hardware and Software How Computers Store Data
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Topics Introduction Hardware and Software How Computers Store Data
Presentation transcript:

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

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

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

Graphics Editors  I use Photoshop Elements (tm) to make my game graphics  A good free solution is Paint.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:   This lets you manipulate images in layers and supports transparency Chapter 11.1: A First Game Program5

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

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

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

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

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

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

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");

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

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(); } } } }

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(); } } } }

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;

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(); } } } }

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

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;

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(); } } } }

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... }

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(); } } } }

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 }

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(); } } } }

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

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(); } } } }

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();

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

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

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

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

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

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

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

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

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

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

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

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

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

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