Page 3

ISSUE 6: Know your Enemy

Add the bad guys!  Along the way we'll be doing a bit of audio editing, some image editing and some more neat tricks with inheritance.  To finish, we'll add some camera view toggling and a functional HUD RADAR!
  • Delicious
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

5. Understanding our HUD

Take a look back now at the HUD code we added in Issue 5:

from player import AlliedFlanker
import random
 
class EnemyFlanker(AlliedFlanker):
    def __init__(self,loader,scenegraph,taskMgr):
        # take an ally and make it join the dark side...
        AlliedFlanker.__init__(self,loader,scenegraph,taskMgr)
        tex = loader.loadTexture('volume1/models/enemyflanker.jpg')
        self.player.setTexture(tex,1)
 
    def teleport(self,boundingBox):
        # random X/Y within bounds
        self.player.setX(random.randint(0,boundingBox))
        self.player.setY(random.randint(0,boundingBox))
        self.player.setZ(100)

It’s generally straight forward. It loads and places two images on the screen – the RADAR and the main HUD information area. What is different (and often confusing to new comers) is the scaling and positioning.

Thus far in MGF we have only dealt with 3D objects and 3D positioning. Things change a little in the 2D world. First of all, coordinates are given via X and Z values. Y means nothing. The values are also in a much smaller range. It is important to understand that if you place an image at a position of 0,0 and a scale of 1 it will fill the whole screen and be exactly centered on the screen. Once you get used to this idea life becomes a lot simpler. The diagram below aims to clarify what we are describing:

2D Positioning

6. Making the RADAR function

Our aim is to represent the whole scene on the RADAR area of the HUD. The tricky part is having the player always central and everything else positioned relative to this. We also want to capture the players heading in the RADAR to complete its functionality. Update your GUI code to read as follows:

        self.level = 1; self.score = 0; self.baddies = list()
        self.startLevel()
        self.enemyCam = False; self.currentEnemy = 0;

For the code to work, you will need the two images referenced. They can be downloaded below. Place them in your ‘gfx’ sub-directory:

You will also need to call the new update method in the updateTask method of ArcadeFlightGame (in game.py):

    def startLevel(self):
        if (self.level == 1):
           self.spawnBadGuys(2,1) # two level 1 baddies please
        # else - other levels, perhaps splash screen/menu injects here

Arguably, we have could added the GUI update to the task manager instead, the same way we added updateTask back in Issue 3. This is a decision you can make for yourself – lean on/rely on the task manager or, as we have done here, add only one “update” method to the task manager and call other needed methods from it. It’s really a preference decision and nothing much else. Finally, add the following methods to the AlliedFlanker class in player.py:

    def spawnBadGuys(self,howMany,skillLevel):
        # note we're not doing anything with skill level yet, as there's only 1 level!
        for i in range(0,howMany):
            self.baddies.append(EnemyFlanker(self.loader,self.render,self.taskMgr))
        # jump start em
        for baddy in self.baddies:
           baddy.teleport(self.world.getSize())

Note they will be available to the EnemyFlanker class too via inheritance. Run the game again and you should find you have a fully functional RADAR making is much easier to locate the enemy! The enemy still appears quite small and can be hard to spot. We updated our enemy constructor with a call to:

        if (self.keyMap["cam"]!=0):
            if (self.enemyCam == False):
                self.enemyCam = True
                self.currentEnemy = 0
            else:
                # "drops off" see below
                self.currentEnemy = self.currentEnemy + 1
            # stop uber fast switching, have to key again
            self.keyMap["cam"] = 0

This makes the enemy larger and a little easier to see! As you fly around, you will see the player icon rotate to indicate heading while red dots will indicate enemy positions. You should now find it quite easy to locate the enemy on your map! Remember, the enemies are positioned at a height of 100 (the same as the player’s start height). The image below shows the RADAR in action:

RADAR

How does it work? The magic is in the update method. The code achieves the following:

  1. Double the bounding box value. The value details the world size (1024) and we need to scale everything on the RADAR accordingly. However, we plan to have the player centred on the RADAR. Thus, the maximum distance an enemy could be from the player at any point in the game is 1024. This means the distance from the RADAR centre to its edge should (i.e., its radius) should be able to capture a whole worlds width (1024) making the total RADAR width (diameter) equal to double the bounding box.
  2. Create some offset values, initially zero.
  3. When we created the player dot in the constructor we set the parent to self.radar and, because of this, positioning the dots on the RADAR now operates on coordinates between 0,0 (bottom left) and 1,1 (top right). Our code positions the player dot on the RADAR based on its position. If we were adding a mini-map/overhead map we would be done now. We are not done because we want the player to always be central on the RADAR. So why did we bother at all (as opposed to simply positioning the player dot to 0.5,0.5 in the constructor)? Read on!
  4. We go on to calculate the X,Z movement required to re-centre the player. This might seem pointless but it is not. Calculating what needs to be done to correct the players position gives us two offset values. We can use these same values thereafter to correctly adjust the position of the enemy (red) dots keeping the whole RADAR correct relative to the player sat in the middle.
  5. Move the player via the offsets.
  6. Delete every entry in our list of “dots”. Every time we perform a RADAR update an enemy may have moved, the player may have moved, enemies might have been created or destroyed and so we’re not even certain how many there are. We do, however, always need to maintain a list of the “last RADAR positions” so we can clear/wipe the RADAR. Failure to do this would result in “trails” on the RADAR.
  7. Update the heading of the players icon/dot. This captures the players heading on the screen.
  8. Go through the list of bad guys. For each, create a red dot. Position the red dot. Adjust via the calculated offsets. Store the dot in the list of dots so we can delete it on the next pass.

Not a bad little RADAR for such a short amount of code!

7. Some final Tweaks

You have seen several times now how software is iterative and ‘things change’. With the bad guys/RADAR on the screen we’re beginning to get a feel for how the game might play. Based on that, we made a few more tweaks to our code to make it feel like a smoother experience.

An update to ‘calculate’ in player.py:

    def updateCamera(self):
        if self.enemyCam == True:
           if len(self.baddies) > self.currentEnemy:
               self.baddies[self.currentEnemy].lookAtMe(self.camera)
           else:
               self.enemyCam = False
        else:
            self.player.lookAtMe(self.camera)

Updates to bank left/right in player.py:

    def bankLeft(self):
        if (self.speed > 0):
            self.player.setH(self.player.getH()+ (2*self.bankfactor))
            self.player.setP(self.player.getP()+self.bankfactor)
            # quickest return:
            if (self.player.getP() >= 180):
                self.player.setP(-180)
 
    def bankRight(self):
        if (self.speed > 0):
            self.player.setH(self.player.getH()- (2*self.bankfactor))
            self.player.setP(self.player.getP()-self.bankfactor)
            if (self.player.getP() <= -180):
                self.player.setP(180)

8. Wrap up and Summary

Another productive Issue! We have successfully added the bad guys to the scene while reusing as much code as possible, added camera toggling, created a fully functional RADAR and done some final tidying and tweaking. Join us in Issue 7 where we will tackle the problem of end-users (players) operating in different screen resolutions. There’s much more, of course, but we always like to end with a small teaser as we’re sure you noticed! (oh OK, hint: think…. Artificial Intelligence :-P)

Goto Issue 7 >>>

Quick Nav: Previous page |

Index: page 1 | page 2 | page 3 |
All: View full Issue on one Page