In this exercise you'll start with a fully working 3rd person world with Ralph as your avatar. You'll then add a couple of classic HUD features, namely the health bar and the overhead map.
class HealthBar(NodePath): """ Adapted from drwr's code on the Panda 3D forums """ def __init__(self): NodePath.__init__(self, 'healthbar') self.group = self.attachNewNode('group') self.group.setPos(-0.5,0,0) cmfg = CardMaker('fg') cmfg.setFrame(0, 1, -0.1, 0.1) self.fg = self.group.attachNewNode(cmfg.generate()) cmbg = CardMaker('bg') cmbg.setFrame(-1, 0, -0.1, 0.1) self.bg = self.group.attachNewNode(cmbg.generate()) self.bg.setPos(1, 0, 0) self.fg.setColor(0, 1, 0, 1.0) self.bg.setColor(0.5, 0.5, 0.5, 1.0) self.value = 0.8 self.update() def update(self): self.fg.setScale(self.value, 1, 1) self.bg.setScale(1.0 - self.value, 1, 1) def setHealth(self, value): self.value = value self.update() def decrease(self, delta): if (self.value - delta) >= 0: self.value -= delta self.update()
class OverheadMap(NodePath): def __init__(self, x1, y1, x2, y2): """ Create a square map with the given world corners in the x-y plane """ NodePath.__init__(self, 'overheadmap') cmbg = CardMaker('bg') cmbg.setFrame(0,1,0,1) self.prime = self.attachNewNode(cmbg.generate()) self.prime.setColor(0.5,0.5,0.5,0.5) self.setBoundary(x1,y1,x2,y2) cmav = CardMaker('am') cmav.setFrame(-0.02,0.02,-0.02,0.02) self.avatar = self.prime.attachNewNode(cmav.generate()) self.avatar.setPos(0,0,0) self.avatar.setColor(0,1,0,1) def addLandmarks(self, landmarks, color=P.Vec4(1,0,0,0.5)): """ Take in a list of (x,y) touples in world coordinates and place them as little dots on the map in the given color """ ## To be filled in... def setBoundary(self, x1, y1, x2, y2): """ Define the extent of the map in world coordinates. Create the proper offset and scaling values that transform world coordinates to map coordinates""" self.xoffset = x1 self.xscale = 1.0/(x2-x1) self.yoffset = y1 self.yscale = 1.0/(y2-y1) def setAvatarPos(self, x, y): """ Sets the position of the avatar marker to the given world coordinates """ self.avatar.setPos((x-self.xoffset)*self.xscale,-0.01,(y-self.yoffset)*self.yscale)