uzycie sprite.Group.update() - gdy umrze sprite, to healthbar nie bedzie renderowany

This commit is contained in:
XsedoX 2022-05-13 10:37:00 +02:00
parent 89c307535b
commit 3fce0a5b57
4 changed files with 11 additions and 14 deletions

View File

@ -147,19 +147,6 @@ class Level:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.') self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias() self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
def update_health_bars(self):
for knight in self.list_knights_blue:
knight.health_bar.update()
for knight in self.list_knights_red:
knight.health_bar.update()
for monster in self.list_monsters:
monster.health_bar.update()
for castle in self.list_castles:
castle.health_bar.update()
def update(self): def update(self):
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
@ -167,4 +154,5 @@ class Level:
# update and draw the game # update and draw the game
self.sprites.draw(self.screen) self.sprites.draw(self.screen)
self.update_health_bars() # has to be called last self.sprites.update()

View File

@ -18,3 +18,6 @@ class Castle(pygame.sprite.Sprite):
self.max_hp = 80 self.max_hp = 80
self.current_hp = random.randint(1, self.max_hp) self.current_hp = random.randint(1, self.max_hp)
self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True) self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
def update(self):
self.health_bar.update()

View File

@ -43,6 +43,9 @@ class Knight(pygame.sprite.Sprite):
self.direction = self.direction.left() self.direction = self.direction.left()
self.image = self.states[self.direction.value] self.image = self.states[self.direction.value]
def update(self):
self.health_bar.update()
def rotate_right(self): def rotate_right(self):
self.direction = self.direction.right() self.direction = self.direction.right()
self.image = self.states[self.direction.value] self.image = self.states[self.direction.value]

View File

@ -41,3 +41,6 @@ class Monster(pygame.sprite.Sprite):
self.max_hp = 7 self.max_hp = 7
self.attack = 2 self.attack = 2
self.points = 2 self.points = 2
def update(self):
self.health_bar.update()