Basic OOP Tutorial II : Zombie Master

This is a continuation of a three part series of tutorials for beginners about object oriented programming using Brutus2D. If you haven't read Basic OOP tutorial I - Rising From The Dead, you should do so now. If you are an old hand at object oriented programming, you should skip it and this tutorial as you can already bend zombies to your will.

In the last tutorial, you learned how to make one hundred zombies jump out of their grave ready to seek out the flesh (or bytes) of innocent game characters. In this tutorial, you will learn how to control the feeble minds of your legion of dead.

Recap

Before we begin, let's recap. Zombies have qualities that make them zombies. Qualities such as hit points, the number of body parts left, and even their smell. In OOP, these are known as properties. Zombies can also do things that zombies do. They rise from the grave, amble around looking for brains, and eat brains when the find them. These things they can do are called methods.

The original prototypical zombie was created in a class. This zombie_cl class contains all the properties and methods that all other zombies have. From this original class, all the other zombies were created and stored in an array that can hold 100 zombies.

The Code Thus Far

Here is the code from the last tutorial :

' load the image that will be used to represent all the zombies
zombie_image = graphics.LoadImage("zombie.bmp")

' define the 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

' create an array that can hold 100 zombies
Dim zombies(100)

' use a For-Next loop to make them rise from the dead
For i = 1 to 100
   Set zombies(i) = New zombie_cl
   zombies(i).rise_from_the_dead()
Next

Zombies Want to Serve

Mindless though they are, zombies want to do your evil bidding. You have already told them to rise_from_the_dead() but now they are looking around, semi-dumbfounded, waiting for you to give them an order.

The problem so far is that they cannot do anything of which they are not capable. For example, it would be exceedingly silly to tell them to beat each other with their third arms —- as they have no third arms. You could try to raise them again, but they are already risen.

Your next step is to give them new capabilities. We do it exactly like we did it before, by giving them another method to their class. Before we included zombie_cl.rise_from_the_dead(), they could not rise from the dead. Now that they have it, they can. Let's make our zombies more useful.

Ambling Around

Given any zombie, you can change it's x coordinate by changing it's zombie_cl.x property.

'move zombie 1 2 pixels to the right
zombie(1).x = zombie(1).x + 2

This is nice, but it can get messy.. Let's say you wanted to keep zombies from moving off the right side of the screen which was 800 pixels wide :

zombie(1).x = zombie(1).x + 2
if zombie(1).x > 800 then zombie(1).x = 800

All this code is pretty simple, and clutters up the code you use in your main game loop. Unless you commented this code, you wouldn't know at first sight what it did. Instead of doing it for the zombie, let's make this a method so that the zombie can do it himself.

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
    Public Sub move_x( move_it )
        x = x + move_it
        if x > 800 then x = 800
        if x < 0 then x = 0
    End Sub
End Class

Our new Public Sub move_x() gives the zombie the ability to move horizontally on its own. When you tell it to move horizontally, it will automatically check to see if it is past the left side of the screen ( 0 ) or past the right side of the screen ( 800 ), and if it is, put itself back on the screen. When we give our zombie this command, it looks like this :

zombie(1).move_x(5)

Instantly we can tell that the zombie is moved 5 pixels to the right. Giving the zombie this command is this simple, since the screen-checking is built right into the zombie_cl class.

This also works for telling zombies to move vertically. There's not need to give you the code for this, as you basically already have it, but it will be included whenever the code for the zombie_cl class is shown.

Drawing Zombies

Once you've moved all of your zombies, you will need to use graphics.SetX and graphics.SetY to set their image locations and graphics.SetImage to draw them. We can easily set up another method so that they can draw themselves.

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
    Public Sub move_x( move_it )
        x = x + move_it
        if x > 800 then x = 800
        if x < 0 then x = 0
    End Sub
    Public Sub move_y( move_it )
        y = y + move_it
        if y > 600 then y = 600
        if y < 0 then y = 0
    End Sub
    Public Sub draw()
       graphics.SetX image, x
       graphics.SetY image, y
       graphics.SetImage image
    End Sub
End Class

Now, to draw all our zombies, we can just use a For loop just like we did with zombie_cl.rise_from_the_dead().

For i = 1 to 100
    zombies(i).draw()
Next

Part III

You now know everything you need to know to create a metric buttload of zombies, give them the capability to do something, and give them orders. Now that you can create an undead army and give it orders, in Basic OOP tutorial III : Reinforcements you will learn is how to manage that army and keep up with them all. Zombies do have a habit of getting distracted and lost.

Related Pages

Backlinks

These pages link back to this page. You may find them helpful.

This article is a stub. You can help us improve Brutus2D Wiki Complete by expanding it.
page_revision: 3, last_edited: 1215147593|%e %b %Y, %H:%M %Z (%O ago)