chapter 14 classes, objects, and games xna game studio 4.0

37
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Upload: anissa-mathews

Post on 19-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

CHAPTER 14Classes, Objects, and Games

XNA Game Studio 4.0

Page 2: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Objectives• Find out about making programs using software objects.

• Learn some software engineering terms and what they mean when we write programs.

• Use objects to add some new elements to our game easily.

Page 3: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

LECTURE 15.01 Understand classes, objects, properties, and instance variables. (5%)

6.03 Understand data encapsulation. (5%)

Page 4: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

DESIGN WITH OBJECTS

Page 5: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Design with Objects• Structures are useful, but we would like to be able to solve

other problems when we create large programs.

• Our program cannot be placed into an invalid state

• Be able to separate a large system into distinct and isolated parts that can be developed independently and made to work together

• The effort involved with making new types of game elements is as small as possible

Page 6: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Design with Objects• Objects don’t add any new behaviors to our program.

• We know just about everything we need to know to write programs when we know about variables, statements, loops, conditions, and arrays.

• Objects are best regarded as a solution to the problem of design.

• They let us talk about systems in general terms.

• We can decide which objects we need and then go back and refine how the objects actually do their tasks once we have decided how they fit together.

Page 7: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

AN OBJECT REFRESHER COURSE

Page 8: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

What is an Object?• An object is a collection of data (fields or instance

variables) and behaviors (methods) that a programmer creates for a particular purpose.

• We create a class to represent our object.

• The stool is an object• It contains data such as color, location, etc. and

methods such as turn, move, etc.

• An XNA game is an object. • It contains data such as the GraphicsDevice

(which describes the display system our game can use) and methods such as Draw (which provides the draw behavior for the game).

Page 9: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Cohesion and Objects• Cohesion is a term used by software engineers to

express how “together” an object is.

• What you want is an object that can look after itself and keep its internal data private.

• It should be able to behave as the object, and it should not be possible for other programs to upset this behavior.

• To do this, you need to protect the data inside the object, so that programs outside can’t see or change the values that control how the objectbehaves.

Page 10: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Protecting Data Inside Objects• The C# language provides a means by which data in an object can be made private, so that only methods inside the object can access the data.

• Add some public behaviors (methods) so that the object can be asked to do things.

• Public members of a class can be used by code in any other class.

• When creating objects with high cohesion, the data in the object should be private and the methods should be public.

Page 11: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Protecting Data Inside Objects• You select whether or not a member is private or public by putting the appropriate C# keyword in front of the class member when you declare it.

• From now on you will take care to make sure that only members of a class that need to be used by other classes are made public, and that all other members are made private.

Page 12: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Four Things an Object Should Do

1. Load its content.

2. Start a new Game.

3. Update itself

4. Draw itself

Page 13: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Object Behaviors• You can create a method for each of these actions.

• If this looks like you are taking the different methods out of the regions that were set up when we refactored the program and putting them inside the object, that is exactly what you are doing.

• The methods that make a bat behave like the are now being moved into that object’s class or structure.

Page 14: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Struct BatSpriteStruct Example

Sample Code: Bat Sprite Structure All the sample projects can be obtained from the Web resources for this text, which can be found at http://oreilly.com/catalog/9780735651579.

Page 15: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Objects and Encapsulation• Encapsulation is the approach where everything relating to a particular part of a system is placed in a single object.

• Example

• Your MP3 player contains a computer and some very complicated software, but as far as you are concerned, it just has buttons that you press to select music tracks and play them. You don’t know (or even care) how it works internally. You just know which buttons to press to get sound out.

Page 16: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

DATA ENCAPSULATION6.03 Understand Data Encapsulation

Page 17: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

What is Data Encapsulation?• Data encapsulation is also know as data hiding.

• When creating a class, you must consider how it will be used.

• The purpose of data encapsulation is

• to protect the object from corruption.

• To give restricted access to the object’s components.

• You are already using data encapsulation by creating a class with private variables and public methods.

Page 18: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Implementing Data Encapsulation• Remember we use access modifiers to determine the

accessibility or visibility of the code outside of a type.

• Usually you will want to limit access to your object’s state from external code.

Access Modifier Description

private Only members (methods) of the class can access

protected Members (methods of the same class and any derived classes can access

public Any code can access

Page 19: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Implementing Data Encapsulation• In declaring the class variables as private, only methods in

the class can access or modify those values.

• In the class House below, note that the Boolean variable hasPower is declared private.

• The public method HasPower is used outside of the class to modify the private class variable.

Page 20: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Implementing Data Encapsulation• When a derived class needs to access the base class’s

instance variables, the instance variables can be declared as protected.

• In this case, the derived class could access/modify the variable, but no external class could modify it.

Page 21: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Check Your Understanding• Which of the following is an example of data

encapsulation?

• Using the String class• Using a base class you created• Using a derived class you created• Using a class given to you that you can modify• Creating a new base class for your project.• Creating a derived class for your project.• Overwriting a method from a class you created in your project.• Using a method from a class you did not write but added to your

project.

Page 22: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

The Great Programmer Speaks: Cohesion Is Good• The Great Programmer is going to be talking a lot in this chapter. She really likes cohesion.

• She likes the way that the object is now “master of its own destiny.”

• There is no chance that any other part of the program can affect the behavior of the object.

• They have to use the four methods described in this section to drive it, and they cannot damage the data inside it.

• She could give this implementation of the object to a customer knowing that they could not break it, which for her is a very good thing.

Page 23: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Coupling Between Objects• Now that you have one object, you need to connect it to

the other game objects.

• Example• You have a bat object and need to connect it to other game

objects. You start with the cheese object, which is acting as the ball in the game and needs to be able to find the location of the bat so that it can make the ball change direction when it hits the bat.

• Software engineers like the Great Programmer call this kind of object linkage coupling.

Page 24: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Coupling Between Objects• Carefully manage the amount of coupling in your

programs because a lot of coupling in a program is bad.

• If all the objects in a program are coupled together, this makes it much harder to change one component because you have to worry about how the change might affect everything else.

• The fewer objects that are coupled, the easier it is to manage change in a program.

• It is also easier to manage change if the way that the objects are coupled is restricted to a few well- defined connections.

Page 25: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Proper Design Is Vital

Page 26: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Creating a Link Between the Bread Bat and the Cheese Ball

• You will implement the coupling between the bat and ball by providing a method in the BatSpriteStruct.

• The method is called CheckCollision:

public bool CheckCollision(Rectangle target){ return spriteRectangle.Intersects(target);}

Page 27: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Creating a Link Between the Bread Bat and the Cheese Ball

• Next, the ball has to be provided with a way that it can find the bat sprite when the game is running.

• The best way is to provide the Update method in the cheese with a reference to the BreadAndCheeseGame game object that represents the running game.

• The ball can then get hold of data items in the game, including the bat sprite.

• This link also allows the ball to tell the game itself that lives have been lost if the ball hits the bottom of the display.

• This information is provided as a parameter to the call of Update in the ball.

Page 28: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

New Code

Page 29: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Creating a Link Between the Bread Bat and the Cheese Ball• When the game is running, the ball can now check for collisions with the bat and behave correctly when it hits it.

• When the game calls the Update method in the ball, it needs to provide a reference to the game object. C# provides a key word called this, which allows an object to get hold of a reference to itself.

Page 30: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Designing Object Interactions• It is important to manage carefully what each of the game objects is responsible for, what it needs to interact with, and what it needs from what it interacts with.

Page 31: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Objects and Messages• Sometimes objects need to change the state of the game.

• Every time the ball hits the bottom of the screen, it needs to tell the game that a life has been lost.

• Table 14-2 lists methods for a game object.

• The ball object needs to use a reference to the game it is part of, so that it can call methods in the game to send these messages.

• When the ball is updated, it is given such a reference so that it can call the methods at the appropriate times.

Page 32: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Messages and Organization• At this level, designing a program sounds a lot like an

organizational problem, and it is.

• A good programmer must be able to work out what each object needs to do and how the objects communicate to create a solution.

• Note that there is never just one way to structure a program.

Page 33: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Container Objects• When adding objects (like tomatoes) to the game, we have

to ask ourselves whether the game should work with individual tomato targets or whether it should work in terms of a collection of them.

• It makes very good sense to regard the whole row of targets as a single item.

• The game just calls the Update and Draw behaviors for the row, as it would any other object in the game.

• Many games have “waves” of attacking aliens, and putting them all in a single collection like this makes them much easier to manage.

Page 34: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Container Objects• Table 14-3 lists methods for a target collection.

Page 35: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Container Objects• The next block of code shows the data that TargetRowStruct

stores for a row of tomato targets.

• The texture is stored once for the entire row, and there is an array of rectangles that holds the position on the screen of each of the targets.

• This illustrates another common game technique: the same texture is used for a large number of game objects.

• There is also an array called TargetVisibility, which is used to keep track of which targets in the row are visible.

• This controls the draw process and is also used to decide when a row needs to be restored and redrawn at the end of a level:

Page 36: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

struct TargetRowStruct

Note: All the responsibility for the draw behaviors has been delegated to the objects in our game.

If we add more game objects, we can use the same mechanism.

Page 37: CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0

Background and Title Screen Objects• The final two objects that are needed for the game are the

background and title screen.

• They are both just textures that must be drawn over the entire screen.

• However, the title screen is slightly different in that it has an Update behavior that checks for the player pressing the A button.

• When the player presses the A button, the game needs to start, which means that the BreadAndCheeseGame object must provide a method that can be called to get the game going.

public void Update(BreadAndCheeseGame game){

if (game.GamePad1.Buttons.A == ButtonState.Pressed){ game.StartGame();}

}