Basic OOP Tutorial I : Rising From The Dead (Brutus2D)

This tutorial will teach you enough to get started using object oriented programming in Brutus2D. It will not teach you everything you need to know —- this is an introductory tutorial for both novice Brutus2D programmers and novice OO programmers. This tutorial will show you the only concepts you need to get started. This tutorial was written with Brutus2D 1.4.1

The Swarming Undead

Let's say you wake up one fine morning and decide that you want to make a zombie game. You think about the idea for a minute and then realize something very profound —- you're going to need a metric buttload of zombies. Not only are you going to need a metric buttload of zombies, but you are also going to have to keep track of this multitude of swarming undead. This may, at first, seem to be a daunting task for a beginning game designer, but luckily, Brutus2D has object oriented programming built in. With the power of objects with us, we can create as many swarthy zombies we could possibly want.

The Zombie

Let's think about our prototypical zombie for a minute. Zombies are zombies because they have qualities that make them zombies. They moan, they rot, they're ugly, and most likely, they stink to high hell. In terms of object oriented programming, these are known as properties. Zombies also do things, they rise from the dead, amble around slowly and tend to take bites out of people. The actions that a zombie can perform are known as methods.

Also, zombies tend to spring from a single source. In almost every zombie movie you can trace the undead infection to one undead guy (or girl) who began the plague. This original zombie, from which all other zombies come from, is known as a class.

The First Zombie

This first zombie is just the same as the last. He doesn't create weaker or varied underlings like a vampire. A zombie can only make other zombies. So, to make our original zombie, and thus, our entire putrid horde, is to make this first zombie and clone him.

Zombie Class

This original zombie is declared by this convention :

Class zombie_cl
End Class

You'll notice that this class is named zombie_cl. This tells us at once that this is the zombie class and keeps from running into naming conflicts later..

Zombie Properties

This is a perfectly valid class, but a perfectly useless one. We want our zombies to be able to be killed, so, let's give them some hit points. They need to be able to be some place, so they need x and y properties as well. Also, we can't forget to give them an image, so the player can see them, so let's give them an image too.

Class zombie_cl
    Public hit_points
    Public x
    Public y
    Public image
End Class

Public declares the property similar to the way dim declares a variable. Because it's a class we need to state whether only the zombie itself can change this property ( private ) or whether anything can change it public ).

Zombie Factory

A class by itself does nothing. It is only a prototype that will be used to create other zombies. Let's create our first zombie :

Dim zombie
Set zombie = New zombie_cl

We now have a zombie with all the properties of zombie_cl. When we create it, we should also give the properties values, otherwise, they will all be set to . This zombie we have cloned from zombie_cl is known as an instance in OOP lingo.

Dim zombie
Set zombie = New zombie_cl

zombie.hit_points = 10
zombie.x = 100
zombie.y = 100
zombie.image = graphics.LoadImage("zombie.bmp")

If your are going to have a metric buttload of zombies, this is going to be alot of work. Then again, in some movies, they don't rise from the dead one at a time, they do it all at once.

Rising From The Dead

Rising from the dead is usually the first thing a zombie does. Thus, it's going to be the first method our zombie class is going to get. But before we do, there's one more thing we need to do to save memory. We need to first load an image, which we can also clone. See the notes at the end if you want to know why.

zombie_image = graphics.LoadImage("zombie.bmp")

Now, back to our zombie class.

Class zombie_cl
    Public hit_points
    Public x
    Public y
    Public image
    Public Sub rise_from_the_dead()
        hit_points = 10
        x = 100
        y = 100
        image = graphics.CloneImage(zombie_image)
    End Sub
End Class

Here we have created a Sub called rise_from_the_dead() just like we would have if it wasn't in a class except we have prefixed it with Public. This makes the Sub usable by things other than a zombie. (see note 2).

To make a large amount of zombies rise from the dead, we need an array and a For loop.

Dim zombies(100)

This gives us an array that can hold 100 zombies. All that's left to do is make them rise from the dead.

For i = 1 to 100
   Set zombies(i) = New zombie_cl
   zombies(i).rise_from_the_dead()
Next

Our For loop goes through each zombie in turn, making each one rise from the dead, one by one, but very fast. (see note 3)

Part II and III

Like every good zombie movie, this object oriented programming tutorial has a sequel, actually two. There's more to be told about our evil enterprise.

Basic OOP tutorial part II : Zombie Master will teach you the black arts of controlling the very minimal minds of your ghoulish army.

Basic OOP tutorial III : Reinforcements will instruct you how to replace zombies when they finally wear out, get killed, or fall off a cliff like a lemming.

Notes

Note 1 - About cloning images

Each time an image is loaded with graphics.LoadImage, enough memory to hold that image is used. If you load the same image twice with graphics.LoadImage, it will use the same amount of memory for each, resulting in twice the amount of memory used for the same image. graphics.CloneImage only refers to the first image instead of loading a new copy of it, thus, consuming less memory.

In our example, if we loaded a copy of zombie.bmp for each of the zombies created, it would require 100 times as much memory as if we just loaded one copy of the image and pointed all the zombie_cl.image properties to it.

Note 2 - Initialize Subs

Normally, the method ( Sub ) we named zombie_cl.rise_from_the_dead() would be named zombie_cl.init(). init is short for initialize, which in programming terms means to start and prepare for operation. This is exactly what our zombie_cl.rise_from_the_dead() Sub does, it starts each zombie by giving their properties starting values.

Note 3 - x 100 - y 100

You may have noticed that each zombie will start out at the x/y coordinates of 100,100, basically being stacked upon each other. If this were a real game, we would use the zombie_cl.rise_from_the_dead() to make sure that they are not stacked upon each other and are spread out across the game world.

This page has been recommended for cleanup, integration, etc.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.