healthbar WIP - team health bars jako klasa health_bar.py

This commit is contained in:
XsedoX 2022-04-28 09:50:24 +02:00
parent e5af464eb2
commit c11fdae8a5
5 changed files with 35 additions and 26 deletions

View File

@ -58,8 +58,6 @@ class Game:
running = False
if event.key == pygame.K_n:
print_numbers_flag = not print_numbers_flag
if event.key == pygame.K_b:
stats.blue_team_hp_bar.take_dmg()
if event.key == pygame.K_r:
stats.red_team_hp_bar.take_dmg(5)
if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds

View File

@ -4,31 +4,41 @@ from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK
class HealthBar:
def __init__(self, screen, rect, current_hp, max_hp, x=None, y=None, width=None, height=None):
def __init__(self, screen, rect: pygame.rect, current_hp, max_hp, calculate_xy=False, calculate_size=False):
self.health_ratio = None
self.rect = rect
self.screen = screen
self.current_hp = current_hp
self.target_hp = current_hp
self.max_hp = max_hp
self.width = width
self.height = height
self.x = x
self.y = y
if self.width is None:
self.width = int(GRID_CELL_SIZE * 0.9)
if self.height is None:
self.height = int(GRID_CELL_SIZE * 0.05)
self.health_ratio = self.max_hp/self.width
if self.x is None:
self.x = int(GRID_CELL_SIZE * 0.1) + self.rect.x
if self.y is None:
self.y = int(GRID_CELL_SIZE/2) + self.rect.y
self.width = self.rect.width
self.height = self.rect.height
self.x = self.rect.x
self.y = self.rect.y
self.calculate_xy = calculate_xy
self.calculate_size = calculate_size
self.update_stats()
def update(self):
self.show()
self.update_stats()
def update_stats(self):
if self.calculate_size:
self.width = int(GRID_CELL_SIZE * 0.9)
self.height = int(GRID_CELL_SIZE * 0.05)
else:
self.x = self.rect.x
self.y = self.rect.y
if self.calculate_xy:
self.x = int(GRID_CELL_SIZE * 0.1) + self.rect.x
self.y = int(GRID_CELL_SIZE/2) + self.rect.y
else:
self.width = self.rect.width
self.height = self.rect.height
self.health_ratio = self.max_hp / self.width
def take_dmg(self, dmg_taken):
if self.target_hp > 0:
@ -50,5 +60,3 @@ class HealthBar:

View File

@ -83,11 +83,11 @@ class Level:
# add objects, e.g. knights, monsters, castle
if col == "k_b":
knight = Knight((col_index, row_index), self.sprites, "blue")
knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue")
self.map[row_index][col_index] = knight
self.list_knights_blue.append(knight)
elif col == "k_r":
knight = Knight((col_index, row_index), self.sprites, "red")
knight = Knight(self.screen, (col_index, row_index), self.sprites, "red")
self.map[row_index][col_index] = knight
self.list_knights_red.append(knight)
elif col == "m":

View File

@ -3,6 +3,7 @@ import random
from common.constants import GRID_CELL_SIZE, Direction
from common.helpers import parse_cord
from logic.health_bar import HealthBar
def load_knight_textures():
@ -18,7 +19,7 @@ def load_knight_textures():
class Knight(pygame.sprite.Sprite):
def __init__(self, position, group, team):
def __init__(self, screen, position, group, team):
super().__init__(group)
self.direction = Direction.DOWN
@ -34,6 +35,8 @@ class Knight(pygame.sprite.Sprite):
self.attack = random.randint(4, 7)
self.defense = random.randint(1, 4)
self.points = 1
self.health_bar = HealthBar(screen, self.rect, self.health, self.health)
self.health_bar.update()
def rotate_left(self):
self.direction = self.direction.left()

View File

@ -13,8 +13,8 @@ class Stats:
self.screen = screen
self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
self.y = 5
self.blue_team_hp_bar = HealthBar(self.screen, (0, 0), 50, 100, self.x + 30, self.y + 210, 100, 15)
self.red_team_hp_bar = HealthBar(self.screen, (0, 0), 50, 100, self.x + 210, self.y + 210, 100, 15)
self.blue_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 30, self.y + 210, 100, 15), current_hp=50, max_hp=100)
self.red_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 210, self.y + 210, 100, 15), 50, 100)
def draw(self):