.Netterpillars: Artificial Intelligence and Sprites - Object-Oriented Programming
(Page 2 of 9 )
There are many technical books that explain the academic details of object-oriented analysis (OOA) and object-oriented programming (OOP). It’s not our goal to enter into such particulars, but instead loosely define a few terms and demonstrate some practical uses of these techniques.
The main idea behind creating objects is to make your code simpler to write and easier to maintain. By creating high-level objects to take care of specific tasks, you can build your games using these objects without needing to remember every tiny detail about a new game.
A good analogy is a house. A house is composed of many different rooms with many purposes, and in any neighborhood, you’ll find a variety of shapes and sizes of homes, each uniquely defined by characteristics such as shape, size, and color. However, a house is built from a template, usually a blueprint, which describes how that house can be built in a repeatable way. You can even break the house down into smaller pieces called subsystems, which provide certain functions in a repeatable way (plumbing, electricity, and heating are simple examples). These subsystems are themselves built from repeatable and reusable components. In the case of an electrical subsystem, you have switches, outlets, and wiring.
The fundamental point is that good object-oriented approaches tend to mimic real-world environments and systems, and that these systems are often able to be used in even more complex systems.
Table 2-1 lists some common terms used when talking about object-oriented programming and analysis, along with a definition of each.
| TERM | DEFINITION |
| Class | The code you write that is used as a blueprint to create objects. It describes the characteristics of an object: what kind of attributes it has, how it can be asked to do things, and how it responds to events. |
| Object | An instance of a class. Generally created by invoking a class’s constructor. |
| Methods | Functions defined inside a class. Generally speaking, a method describes an action that the object can be told to do. |
| Properties or attributes | Variables defined inside a class. Class attributes typically describe the qualities (state) of the object. In some cases, attributes might not be accessible to the user of an object because you (the author) have decided those attributes should not be easily modified by a user. Properties are a special type of attribute that let you define more complex ways to read or write to an attribute. |
| Events | Methods in the object triggered by an external action. May be associated with a user action (such as clicking a button) or a system action (such as a specific time slice that has elapsed). |
| Constructor | Special method called when creating an object—in C#, this is done by using the keyword “new” followed by the class name. |
| Destructor | Special method called when the object is being destroyed. In C#, to code the destructor you have to override (see the Overriding entry) the Dispose method of the base class. However, because of the automatic garbage collection found in the common language runtime, explicitly calling a destructor is rarely needed. |
| Inheritance | Object-oriented concept that defines that one class can be derived from another class or classes (called base classes), and inherit their interface and code (called the derived or child class). |
| Overriding | Object-oriented concept that defines that a derived class can create a different implementation of a base class method. In effect, it completely overrides the base class’s behavior. |
| Interface | A “contract” that defines the structure of methods, properties, events, and indexers. You can’t create an object directly from an interface. You must first create a class that implements the interface’s features. |
| Encapsulation | The concept of gathering methods, properties, events, and attributes into a cohesive class and removing the details from the user. An example of encapsulation would be a car—you operate a car by steering, braking, and accelerating. Good encapsulation removes the need for you to worry about managing fuel injection flow, brake fluid hydraulics, and proper internal combustion. |
| Overloading | Object-oriented concept that states that one method can have many different interfaces, while keeping the same name. |
Table 2-1. Common Object-Oriented Terminology
Polymorphism Object-oriented concept that says that different objects can have different implementations of the same function. An Add method, for example, can sum integers and concatenate strings.
NOTE We’ll refer to these concepts and terms throughout the rest of the book, reinforcing their meanings as we go along.
Continuing with the introductory concepts of this chapter, let’s talk about artificial intelligence, demonstrating a real-life application of this concept born in science fiction books.
This chapter is from Beginning .NET Game Programming in C# by Ellen Hatton et al. (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today. Buy this book now.
|
Next: Artificial Intelligence >>
More ASP.NET Articles
More By Apress Publishing