BOSS said, to have life, then there is life.
In order to show his long life, boss often does the stupid thing is to show his pants to others. Come on, mortal, look at my blood volume.
The life bar can be drawn with a line with width or a rectangle filled with color.
First, the life value of the boss and the life value when full blood are imported. You can calculate the proportion of the remaining blood and draw a dynamic blood bar.
If you want the blood bar to dance with the BOSS, you have to enter the BOSS coordinates.
So just pass in an object self. The layer of blood bar belongs to allgroup, which is only used to display, no collision check and so on, so just draw it.
When BOSS has no blood, it will disappear.
from setting import * class Lifebar(pygame.sprite.Sprite): """ //Display a blood bar with the same width as BOSS //With the decrease of BOSS's life, it will be reduced in proportion. When BOSS is destroyed, it will also be buried“ def __init__(self, boss): self.groups = allgroup self.boss = boss self._layer = self.boss._layer pygame.sprite.Sprite.__init__(self, self.groups) self.oldpercent = 0 self.paint() def paint(self): self.image = pygame.Surface((self.boss.rect.width,12)) self.image.set_colorkey((0,0,0)) # Black transparent #Set borders pygame.draw.rect(self.image, (0,255,0), (0,0,self.boss.rect.width,10),1) pygame.draw.rect(self.image,(0,255,0),(0,0,self.boss.rect.width,10),10) self.rect = self.image.get_rect() def update(self): self.percent = self.boss.HP / self.boss.HPFULL * 1.0 #After x blood volume changes, redraw if self.percent != self.oldpercent: self.paint() # After the OSS is deformed, the blood bar size will change and needs to be recalculated #Set the background color black, and add the paint's colorkey to make the black transparent. pygame.draw.rect(self.image, (0,0,0), (1,1,self.boss.rect.width-2,10)) #Draw pygame.draw.rect(self.image, (255,0,0), (1,1,int(self.boss.rect.width * self.percent),10)) self.oldpercent = self.percent self.rect.centerx = self.boss.rect.centerx self.rect.centery = self.boss.rect.centery - self.boss.rect.height /2 - 10 if self.boss.HP < 1: # You die and I die self.kill()
Add blood volume and blood bar in boss.py, very simple lines of code
def __init__(self): #Add to self.HP = 500 self.HPFULL = 500 lifebar = Lifebar(self)
Blood bar appeared, no less blood, no effect.
Then add in update
self.HP -= 1
The blood bar was gone in a few seconds.
Put the BOSS out, too.
Add in update
if self.HP <=0:
The implementation effect is as follows: