For this example we'll mainly be editing the files for our HUD, Cycle, MachineGun, and Cannon classes.

  1. Open GunClasses_03.py. First, we have a few small edits to make here.
  2. We need labels for the guns to show which HUD elements relate to which gun. Add this line to the __init__ method of the MachineGun class, right under the line that says self.cycle = cycle:
    self.name = "JR Martin J59 Jabber"
    
  3. Add this line to the same place in the Cannon class:
    self.name = "Virtue X-A9 Equalizer"
    
  4. Each gun needs to have an energy cost associated with it, so add the following line to the MachineGun class, right beneath the line that says self.damage = 10:
    self.energyCost = 1.25
    
  5. Add the following line in the same place in the Cannon class:
    self.energyCost = 5
    
  6. To actually deduct the energy costs from the cycle's total when the gun is fired, we need to add this line to the fire() method in both the classes, right beneath the line that starts fireSeq:
    self.cycle.energy -= self.energyCost
    
  7. Resave the file as GunClasses_04.py and open CycleClass_04.py. Update the gun classes import line to use the new file.
  8. Add these two lines to the setupVarsNPs() method, right under the line that says self.handling = 25:
    self.maxEnergy = 100
    self.energy = self.maxEnergy
    
  9. Resave the file as CycleClass_05.py and open HUDClass_01.py. It's time to do the real work.
  10. Add this line to the __init__ method of the HUD class, right after the line that calls the createLLHUD() method:
    self.createURHUD(fonts)
    
  11. Scroll down to setupCycle and add the following lines to the bottom of that method:
    self.guns = [self.cycle.LMG, self.cycle.RMG,
    self.cycle.cannon]
    self.gunNames[0]["text"] = self.guns[0].name
    self.gunNames[1]["text"] = self.guns[1].name
    self.gunNames[2]["text"] = self.guns[2].name
    
  12. We need a method to create the new section of the HUD, so add the following method right beneath the createLLHUD() method. It's a lot of code, but there's nothing in it that we haven't done before.
    def createURHUD(self, fonts):
    self.urFrame = DirectFrame(frameSize = (-.6, 0, -.4, 0),
    frameColor = (1,1,1,0),
    parent = base.a2dTopRight)
    energyEgg = loader.loadModel("../Models/EnergyBar.egg")
    self.energyBG = energyEgg.find("**/EnergyBG")
    self.energyBar = energyEgg.find("**/EnergyBar")
    self.energyFrame = energyEgg.find("**/EnergyFrame")
    self.energyBG.reparentTo(self.urFrame)
    self.energyBar.reparentTo(self.energyBG)
    self.energyFrame.reparentTo(self.energyBG)
    self.energyBG.setPos(-.35, 0, -.0375)
    alpha = loader.loadTexture("../Images/ReloadAlpha.png")
    alpha.setFormat(Texture.FAlpha)
    alpha.setWrapU(Texture.WMClamp)
    self.energyBar.setTexture(self.modTS, alpha)
    self.energyText = DirectLabel(text = "100",
    text_font = fonts["orange"], text_scale = .05,
    pos = (-.65, 0, -.0525), text_fg = (1,1,1,1),
    relief = None, text_align = TextNode.ARight,
    self.reloadGreen = loader.loadTexture(
    "../Images/ReloadGreen.png")
    self.reloadRed = loader.loadTexture(
    "../Images/ReloadRed.png").
    self.reloadBars = []
    self.gunNames = []
    for N in range(3):
    self.reloadBars.append(loader.loadModel(
    "../Models/ReloadBar.egg"))
    self.reloadBars[N].reparentTo(self.urFrame)
    self.reloadBars[N].setPos(-.6, 0, -.1125 + (N * -.05))
    self.reloadBars[N].setScale(.1, 0, .1)
    self.reloadBars[N].setTexture(self.modTS, alpha)
    self.reloadBars[N].setTexOffset(self.modTS, .015, 0)
    self.gunNames.append(DirectLabel(text = "Gun Name",
    text_font = fonts["orange"], text_scale = .035,
    pos = (-.55, 0, -.125 + (N * -.05)),
    text_fg = (1,1,1,1), relief = None,
    text_align = TextNode.ALeft,
    parent = self.urFrame))
    return
    
  13. Just like with the lower-left corner of the HUD, we need an update method for the upper-right corner. Here's that code; place it in right below the updateLLHUD() method:
    def updateURHUD(self):
    energyRatio = self.cycle.energy / self.cycle.maxEnergy
    self.energyBar.setTexOffset(self.modTS,
    -(1 - energyRatio) + .015, 0)
    self.energyText["text"] = str(int(self.cycle.energy))
    for N in range(3):
    if(self.guns[N].fireSeq.isPlaying() == True):
    if(self.reloadBars[N].getTexture != self.reloadRed):
    self.reloadBars[N].setTexture(self.reloadRed, 1)
    reloadRatio = (self.guns[N].fireSeq.getT()
    / self.guns[N].fireSeq.getDuration())
    self.reloadBars[N].setTexOffset(self.modTS,
    -(1 - reloadRatio) + .015, 0)
    elif(self.reloadBars[N].getTexture() != self.reloadGreen):
    self.reloadBars[N].setTexture(self.reloadGreen, 1)
    self.reloadBars[N].setTexOffset(self.modTS,
    .015, 0)
    return
    
  14. Scroll down to the updateHUD() method and add this line in, right after the call to updateLLHUD():
    self.updateURHUD()
    
  15. In the hide() method, add the following line right beneath the one that hides the lower left DirectFrame:
    self.urFrame.hide()
    
  16. Add this line to the show() method as well. It belongs right after the line that shows the lower-left DirectFrame:
    self.urFrame.show()
    
  17. Resave the file as HUDClass_02.py.
  18. Update RaceClass_03.py to use CycleClass_05.py, and then resave it as RaceClass_04.py.
  19. Update WorldClass_03.py to use RaceClass_04.py and HUDClass_02.py then resave it as WorldClass_04.py.
  20. Run WorldClass_04.py from the command prompt and fire some of the guns to see the new HUD pieces in action.
Time for action - adding a new HUD section