healthbar #24

Merged
s464869 merged 3 commits from healthbar into master 2022-05-11 16:53:15 +02:00
4 changed files with 42 additions and 26 deletions

View File

@ -27,7 +27,6 @@ class Game:
self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)} self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)}
def main_menu(self): def main_menu(self):
menu = MainMenu(self.screen, self.clock, self.bg, menu = MainMenu(self.screen, self.clock, self.bg,
self.game, self.game,
@ -38,7 +37,6 @@ class Game:
def game(self): def game(self):
logs = Logs(self.screen) logs = Logs(self.screen)
level = Level(self.screen, logs) level = Level(self.screen, logs)
stats = Stats(self.screen)
# setup clock for rounds # setup clock for rounds
NEXT_TURN = pygame.USEREVENT + 1 NEXT_TURN = pygame.USEREVENT + 1
@ -46,6 +44,7 @@ class Game:
# create level # create level
level.create_map() level.create_map()
stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red)
print_numbers_flag = False print_numbers_flag = False
running = True running = True
@ -61,8 +60,7 @@ class Game:
running = False running = False
if event.key == pygame.K_n: if event.key == pygame.K_n:
print_numbers_flag = not print_numbers_flag print_numbers_flag = not print_numbers_flag
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 if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds
level.handle_turn() level.handle_turn()

View File

@ -9,7 +9,6 @@ class HealthBar:
self.rect = rect self.rect = rect
self.screen = screen self.screen = screen
self.current_hp = current_hp self.current_hp = current_hp
self.target_hp = current_hp
self.max_hp = max_hp self.max_hp = max_hp
self.x = self.rect.x self.x = self.rect.x
self.y = self.rect.y self.y = self.rect.y
@ -38,22 +37,22 @@ class HealthBar:
self.health_ratio = self.max_hp / self.width self.health_ratio = self.max_hp / self.width
def take_dmg(self, dmg_taken): def take_dmg(self, amount):
if self.target_hp > 0: if self.current_hp - amount > 0:
self.target_hp -= dmg_taken self.current_hp -= amount
elif self.target_hp < 0: elif self.current_hp - amount <= 0:
self.target_hp = 0 self.current_hp = 0
def heal(self, amount): def heal(self, amount):
if self.target_hp < self.max_hp: if self.current_hp + amount < self.max_hp:
self.target_hp += amount self.current_hp += amount
elif self.target_hp > self.max_hp: elif self.current_hp + amount > self.max_hp:
self.target_hp = self.max_hp self.current_hp = self.max_hp
def show(self): def show(self):
pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2)) pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2))
pygame.Surface.fill(self.screen, RED, (self.x, self.y, self.width, self.height)) pygame.Surface.fill(self.screen, RED, (self.x, self.y, self.width, self.height))
pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.target_hp / self.health_ratio), self.height)) pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.current_hp / self.health_ratio), self.height))

View File

@ -34,11 +34,10 @@ class Knight(pygame.sprite.Sprite):
self.team = team self.team = team
self.max_hp = random.randint(7, 12) self.max_hp = random.randint(7, 12)
self.current_hp = random.randint(1, self.max_hp)
self.attack = random.randint(4, 7) self.attack = random.randint(4, 7)
self.defense = random.randint(1, 4) self.defense = random.randint(1, 4)
self.points = 1 self.points = 1
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=random.randint(1, self.max_hp), max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
def rotate_left(self): def rotate_left(self):
self.direction = self.direction.left() self.direction = self.direction.left()
@ -48,6 +47,18 @@ class Knight(pygame.sprite.Sprite):
self.direction = self.direction.right() self.direction = self.direction.right()
self.image = self.states[self.direction.value] self.image = self.states[self.direction.value]
def take_dmg(self, amount):
self.health_bar.take_dmg(amount)
def heal(self, amount):
self.health_bar.heal(amount)
def get_current_hp(self):
return self.health_bar.current_hp
def get_max_hp(self):
return self.health_bar.max_hp
def step_forward(self): def step_forward(self):
if self.direction.name == 'UP': if self.direction.name == 'UP':
self.position = (self.position[0], self.position[1] - 1) self.position = (self.position[0], self.position[1] - 1)

View File

@ -8,13 +8,21 @@ from common.helpers import draw_text
class Stats: class Stats:
def __init__(self, screen): def __init__(self, screen, list_knights_blue, list_knights_red):
self.grid = [] self.grid = []
self.list_knights_blue = list_knights_blue
self.list_knights_red = list_knights_red
self.screen = screen self.screen = screen
self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
self.y = 5 self.y = 5
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.blue_team_hp_bar = HealthBar(self.screen,
self.red_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 210, self.y + 210, 100, 15), 100, 100) pygame.Rect(self.x + 30, self.y + 210, 100, 15),
current_hp=sum([knight.get_current_hp() for knight in self.list_knights_blue]),
max_hp=sum([knight.get_max_hp() for knight in self.list_knights_blue]))
self.red_team_hp_bar = HealthBar(self.screen,
pygame.Rect(self.x + 210, self.y + 210, 100, 15),
current_hp=sum([knight.get_current_hp() for knight in self.list_knights_red]),
max_hp=sum([knight.get_max_hp() for knight in self.list_knights_red]))
def update(self): def update(self):
@ -33,16 +41,16 @@ class Stats:
draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36) draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36)
# HP bars # HP bars
#pygame.draw.rect(screen, RED, pygame.Rect(x + 30, y + 210, 100, 15), 0, 4) self.red_team_hp_bar.take_dmg(self.red_team_hp_bar.current_hp -
#pygame.draw.rect(screen, RED, pygame.Rect(x + 210, y + 210, 100, 15), 0, 4) sum([knight.get_current_hp() for knight in self.list_knights_red]))
self.blue_team_hp_bar.take_dmg(self.blue_team_hp_bar.current_hp -
sum([knight.get_current_hp() for knight in self.list_knights_blue]))
self.red_team_hp_bar.update() self.red_team_hp_bar.update()
self.blue_team_hp_bar.update() self.blue_team_hp_bar.update()
# texts # texts
draw_text('Rycerze: 2', FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) draw_text('Rycerze: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue
draw_text('Fortece: 1', FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) draw_text('Fortece: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) # red
draw_text('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18) draw_text('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18)
draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 215, self.y + 270, 18) draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 215, self.y + 270, 18)