Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
67 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Arrays
 Vectors
 OOP
 Files
 Arrays in Games

 Printable version
 Discuss this article
 in the forums


The Series
 The Basics
 Making a
 Simple Game

 The Power of Arrays

Object-Oriented Programming

When you hear programmers talking about Java one of the phrases that comes up frequently is object-oriented programming. It seems that everyone who is anyone is on the OOP bandwagon these days so we should take a look and see if there is anything we can get from this.

Now you may think that working with classes is going to be complicated and it can be, but we have actually already been working with classes. That’s right! If you look back at our Java applets they have been located inside a big class and we have also used classes created by others to make our lives easier. You can actually get away with only ever using this one class and never creating any additional ones, but if you want to make a game of any depth or with any complexity you should learn to love classes or at least realize that they are worth the effort it takes to learn them.

Classes consist of two main parts: member variables and methods. Member variables are variables which can be accessed by all of the methods within the class and possibly by methods outside the class. The member methods are basically the same as member variables in that they can be accessed by all of the methods within the class and possibly by methods outside the class. Now you are probably asking why is it possible that the methods and variables might not be accessible from outside the class. The answer to this question is that methods inside a class can either be private or public. If they are public then any method outside the class can access/call it, but if they are private then only methods inside the class can access/call it.

Now that we have a basic idea of what classes are why should we bother using them? The reason we want to use classes is that they help us to organize our code, make our code more intuitive, and enable us to make complex programs with greater ease. An example of a situation in which a class would be useful is in an arcade shoot’em-up with spaceships. What is the difference from one spaceship to the next? The location and what the ships happen to be doing are different from one to ship to the next. Is the type of information that we keep track of for the spaceships different from one to the next? Can one spaceship do anything different from the next spaceship? The answer to both of these questions is generally no. By creating a class for a spaceship we can create a template of information and actions that can be called upon for all of our ships without having to type out a bunch of code for each ship.

Eg. SpaceShip class

class SpaceShip
{
  private int amount_of_ammo;
  private float x_coordinate;
  private float y_coordinate;
  private float z_coordinate;

  public void MoveShip();
  public void Shoot();
};

Now this is only a sample class so I haven’t included any code for the methods in the class, but this gives you an idea of the setup that we would need. Once I have this class laid out I could then create an array of ships that I could access and manipulate.

Eg. Creating an array of SpaceShips

SpaceShip aliens[] = new SpaceShip[10];

This is all that we need to worry about for the time being in terms of classes. There are even more modifiers available for you to use so if you are interested I would encourage you to read more about this subject. I hope that one point you will take from this section is that classes can be used effectively in your code to make your life easier and to make your code neater and more organized.





Next : Files in Java Applets