1. Game Development
  2. Programming

Quick Tip: Intro to Object-Oriented Programming for Game Development

This post is part of a series called Beginner's Guide to OOP.
Quick Tip: The OOP Principle of Cohesion

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.


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

  • rotation
  • momentum
  • thrust

and behaviors of:

  • turning
  • moving
  • firing

I'll let you determine what the remaining objects' states and behaviors are.


Next, let's consider another classic game, Tetris. Again, we start off by describing Tetris to find its objects.

A random sequence of Tetrominos fall down the playing field. The objective of the game is to manipulate these Tetrominos, by moving each one sideways and rotating it by 90 degree units, with the aim of creating a horizontal line of ten blocks without gaps.


In Tetris, really the only object is a Tetromino. It has states of:

  • rotation (in 90 degree units)
  • shape
  • color

and behaviors of:

  • falling
  • moving (sideways)
  • rotating


The last game we'll use as an example is Pac-Man.

The player controls Pac-Man through a maze, eating pac-dots or pellets. Four enemies [ghosts] roam the maze, trying to catch Pac-Man. Near the corners of the maze are four larger, flashing dots known as power pellets that provide Pac-Man with the temporary ability to eat the enemies. The enemies turn deep blue, reverse direction and usually move more slowly.

The original Pac-Man arcade game
The original Pac-Man arcade game

For Pac-Man, there are three objects: Pac-Man, a ghost, and a pac-dot. A power pellet isn't really its own object because it's a special pac-dot (we'll talk about what to do about this in a later article). We'll describe the ghost object because it's more interesting.

The ghost has states of:

  • color
  • name (+1 if you can name all the ghosts off the top of your head)
  • state (eatable or not)
  • direction
  • speed

and behaviors of:

  • moving
  • changing state


Object-oriented programming is one way to organize code in a video game. OOP focuses on objects that are defined by their state and their behavior.

In the next Quick Tip, we'll talk about the principle of cohesion and begin to code the basic structure of an object. Follow us on Twitter, Facebook, or Google+ to keep up to date with the latest posts.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.