logo.jpg (7978 bytes)

OO Game Programming tutorial - Part 5



The other objects

 


The Walls

The class diagram:

CWall
x, y (Top left coordinates)
width
height
color[3]
Draw()

 

* We’ll assume the width runs along X-axis and height along the Y-axis.

These are just rectangles that we can position at the top and bottom of the board. We will use these to bounce the ball off when we do some collision detection.

We add lines to the Game_Init() function that position the walls and as with the other objects we need to draw we add a Draw() command to the rendering section.

Here is the code:-

CWall.h
CWall.cpp

 

The paddles

Class diagram:

CPaddle
x, y (Top left coordinates)
width
height
speed
color[3]
score

Draw()
Reset()

 

:Paddles aren’t much different from wall, except we will give them a movement speed and a player score. We also initialise their positions in Game_Init().

The Reset() function will be called when a new game starts, it will just reset the player's score back to zero.

CPaddle.h
CPaddle.cpp

The goal

Since the wall and goal are so similar, we can use a wall object to represent the goal. We don't need to see the goal so we'll just leave out the command for drawing it.

When checking we have positioned the goal in the right spot we can just do a call to the Draw() function and see it on screen.

Note: The objects share a lot of attributes which we could use to make an abstract base class to derive them from, but since this is a beginners project and there’s not too many objects we shall leave it be. If you don’t know what I mean, don’t worry it’s just a more advanced OO technique that can be learnt later.

 


Looks like things are starting to take place!

Get the complete project here

 

Next page

Back to main page