class Block_002(Block): ''' Grass ''' def __init__(self, xy): Block.__init__(self, xy, bgColor=(50,200,50)) grasswh = (60,25) self.image = pygame.image.load(globs.datadir+"/png/blocks/grass.png") self.image.convert() self.grass = Sprite(self.rect.topleft, grasswh) self.grass.image.set_colorkey((255,0,255)) self.grass.image = pygame.image.load(globs.datadir+"/png/blocks/002_decoration.png") self.grass.image.convert() self.grass.image.convert_alpha() def blitDecoration(self, xy): self.grass.rect.topleft = (self.rect.x-5, self.rect.y-20) self.grass.worldBlit()
class LivingEntity(CollidableEntity): def __init__(self, xy, wh, health, add=True): CollidableEntity.__init__(self, xy, wh, (255,0,255), add=add) #self.attackArea = Sprite(parent, (0,0), (attackArea, attackArea)) #print(self.attackArea) #self.updateAttackArea() self.maxhealth = health self.health = self.maxhealth self.lasthealth = None self.hitted = [] if add: globs.currentgame.living_entities.add(self) # Initialize healthbar self.healthbar = Sprite((0,0), (self.image.get_width(), 5)) def drawHealthbar(self): self.healthbar.image.fill((0,0,0)) healthpercentage = float(self.health)/self.maxhealth w = self.healthbar.rect.w * healthpercentage newrect = pygame.Rect(0, 0, w, self.healthbar.rect.h) self.healthbar.image.fill((255,0,0), newrect) def blitHealthbar(self): xy = (self.rect.x, self.rect.y-10) if self.health != self.lasthealth: self.drawHealthbar() self.lasthealth = self.health self.healthbar.rect = self.healthbar.image.get_rect(topleft=xy) self.healthbar.worldBlit() def worldBlit(self): CollidableEntity.worldBlit(self) self.blitHealthbar() def isAlive(self): if self.health <= 0: return False return True def die(self): self.kill() print(str(self) + " died") for item in self.hitted: item.hitting = None print(self.hitted) def checkHurt(self): pass #collidedItems = pygame.sprite.spritecollide(self, globs.currentgame.lethals, False) #for collidedItem in collidedItems: # print(collidedItem) # if collidedItem and collidedItem.owner != self and collidedItem != self: # print(collidedItem.__class__) # print("hitted!") # collidedItem.hit(self) # # Needed so he is not hurt every frame the player is colliding with the lethal item # self.hitted.append(collidedItem.owner) # collidedItem.hitting.append(self)