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

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Lecture 6: Software Design (Part I)
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Classes & Objects Computer Science I Last updated 9/30/10.
Road Map Introduction to object oriented programming. Classes
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Games and Simulations O-O Programming in Java The Walker School
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
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 Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
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.
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
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.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
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.
COMPUTER PROGRAMMING 2 Timers. Game Idea: Mob Reaction Timer Use a timer variable to keep track of time and a variable for each player to measure the.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Developing the Game User Interface (UI) Lesson 5.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
Game Maker Terminology
Rob Miles. How does an XNA game program work? Programs tell computers what to do A program is written in a programming language – C# is a programming.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Game Maker – Getting Started What is Game Maker?.
11 Getting Player Input Using a Gamepad Session 3.1.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Session 13 Pinball Game Construction Kit (Version 3):
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
M1G Introduction to Programming 2 5. Completing the program.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Introduction to Object-Oriented Programming Lesson 2.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Review of Previous Classes Declaring Variables - var myVar:DataType = value Data Types – Number, uint, String, Boolean Functions – parameters, return.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
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 Computers, C#, XNA, and You Session 1.1. Session Overview  Find out what computers are all about ...and what makes a great programmer  Discover.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
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.
OOP - Object Oriented Programming
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.
Using Procedures and Exception Handling
Lesson 16: Functions with Return Values
Arrays
Presentation transcript:

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.

LECTURE Understand classes, objects, properties, and instance variables. (5%) 6.03 Understand data encapsulation. (5%)

DESIGN WITH OBJECTS

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

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.

AN OBJECT REFRESHER COURSE

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

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.

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.

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.

Four Things an Object Should Do 1. Load its content. 2. Start a new Game. 3. Update itself 4. Draw itself

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.

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

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.

DATA ENCAPSULATION 6.03 Understand Data Encapsulation

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.

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 ModifierDescription privateOnly members (methods) of the class can access protectedMembers (methods of the same class and any derived classes can access publicAny code can access

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.

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.

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.

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.

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.

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.

Proper Design Is Vital

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

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.

New Code

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.

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.

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.

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.

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.

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

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:

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.

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