This entry is part 1 of 1 in the series
Beginner's Guide to OOP
Welcome to a new series of Quick Tips on Object-Oriented Programming! We’ll be going over the principles of OOP and how they can be used to create organized code. In this first part, we’ll talk about what OOP is and why it’s helpful, with a few examples of how it could be used in game development.
What Is Object-Oriented Programming?
Object-oriented programming (
OOP), in its most basic sense, is a programming style used to organize code. Video games can run anywhere from a few thousand lines of code (
Cut the Rope has 15,000) to millions of lines of code long (
Crysis has over a million). You can see why it’s so important to write code that can be easily modified and maintained.
Programming styles, such as
OOP, help to organize code in such a way that it becomes easier to maintain and modify.
OOP helps organize code by organizing it into what are known as
objects.
Objects hold information about
state and
behavior:
States are the characteristics of the object, or the words you would use to describe it, and usually take the form of
is or
has descriptors. A computer
is either on or off, a chair
has four legs, and you
have a name.
Behaviors are the things the object can
do, or the actions the object can
perform, and are usually verbs that end in
ing. You are
sitting,
using a computer, and
reading this article.
Why Is It Helpful?
As stated earlier,
OOP is helpful because it helps create
maintainable code – code that is understandable, adaptable, and extendable.
It also helps create reusable code by following the
DRY (Don’t Repeat Yourself) method: write the code once and then reuse it, rather than copying and pasting.
The
OOP way of thinking also lends itself well to directly translating real-world objects and interactions into code.
How to Apply It
I’ll list three different examples of how to apply
OOP to video games. In later articles, we’ll look at how to code these examples, but for now we’ll just stick to learning to identify objects and their states and behaviors.
Asteroids
First, let’s imagine that we wanted to make the classic game
Asteroids. To identify what the objects are in Asteroids, try describing it.
Wikipedia describes Asteroids as follows:
The objective of Asteroids is to score as many points as possible by destroying asteroids and flying saucers. The player controls a triangular-shaped ship that can rotate left and right, fire shots straight forward, and thrust forward. As the ship moves, momentum is not conserved – the ship eventually comes to a stop again when not thrusting.
Think about what in this description could stand alone, or the things that are described that could have state and behavior. These become our
objects.
The objects for Asteroids are: a ship, an asteroid, a flying saucer, and a bullet (can’t forget those!). The cool thing about objects is that we normally describe things in terms of objects in everyday talk, so they usually reveal themselves through a description.

The classic game of Asteroids
Now that we have identified our objects, let’s define the state and behavior for one of them: the player’s ship. Think about what attributes describe the ship; these are its states. Then think about what the ship can do; these are its behaviors.
A ship has states of:[...]
Read more: Quick Tip: Intro to Object-Oriented Programming for Game Development