health_bar.py - gotowy

This commit is contained in:
XsedoX 2022-04-28 14:13:59 +02:00
parent 0f6f2354d3
commit dca9c82b70
10 changed files with 83 additions and 60 deletions

View File

@ -65,3 +65,5 @@ ACTION = {
# HEALTH_BAR # HEALTH_BAR
BAR_ANIMATION_SPEED = 1 BAR_ANIMATION_SPEED = 1
BAR_WIDTH_MULTIPLIER = 0.9 # (0;1>
BAR_HEIGHT_MULTIPLIER = 0.1

View File

@ -7,40 +7,40 @@ from models.knight import Knight
class KnightsQueueTest(unittest.TestCase): class KnightsQueueTest(unittest.TestCase):
def test_should_skip_dead_knights(self): def test_should_skip_dead_knights(self):
knight1 = Knight(None) knight1 = Knight(None)
knight1.health = 0 knight1.max_hp = 0
knight2 = Knight(None) knight2 = Knight(None)
knight2.health = 0 knight2.max_hp = 0
knight3 = Knight(None) knight3 = Knight(None)
knight3.health = 1 knight3.max_hp = 1
knight4 = Knight(None) knight4 = Knight(None)
knight4.health = 0 knight4.max_hp = 0
knight5 = Knight(None) knight5 = Knight(None)
knight5.health = 0 knight5.max_hp = 0
knight6 = Knight(None) knight6 = Knight(None)
knight6.health = 1 knight6.max_hp = 1
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4, knight5, knight6]) knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4, knight5, knight6])
res1 = knights_queue.dequeue_knight() res1 = knights_queue.dequeue_knight()
res2 = knights_queue.dequeue_knight() res2 = knights_queue.dequeue_knight()
self.assertEqual(res1.health, 1) self.assertEqual(res1.max_hp, 1)
self.assertEqual(res2.health, 1) self.assertEqual(res2.max_hp, 1)
def test_should_return_first_alive_knight(self): def test_should_return_first_alive_knight(self):
knight1 = Knight(None) knight1 = Knight(None)
knight1.health = 222 knight1.max_hp = 222
knight2 = Knight(None) knight2 = Knight(None)
knight2.health = -1 knight2.max_hp = -1
knight3 = Knight(None) knight3 = Knight(None)
knight3.health = 1 knight3.max_hp = 1
knights_queue = KnightsQueue([knight1, knight2], [knight3]) knights_queue = KnightsQueue([knight1, knight2], [knight3])
@ -55,22 +55,22 @@ class KnightsQueueTest(unittest.TestCase):
def test_should_raise_when_knight_died_and_whole_team_dead(self): def test_should_raise_when_knight_died_and_whole_team_dead(self):
with self.assertRaises(Exception): with self.assertRaises(Exception):
knight1 = Knight(None) knight1 = Knight(None)
knight1.health = 222 knight1.max_hp = 222
knight2 = Knight(None) knight2 = Knight(None)
knight2.health = 1 knight2.max_hp = 1
knights_queue = KnightsQueue([knight1], [knight2]) knights_queue = KnightsQueue([knight1], [knight2])
knights_queue.dequeue_knight() knights_queue.dequeue_knight()
knights_queue.dequeue_knight() knights_queue.dequeue_knight()
knight2.health = -2 knight2.max_hp = -2
knights_queue.dequeue_knight() knights_queue.dequeue_knight()
knights_queue.dequeue_knight() knights_queue.dequeue_knight()
def test_should_make_valid_next_turn(self): def test_should_make_valid_next_turn(self):
knight1 = Knight(None) knight1 = Knight(None)
knight1.health = 222 knight1.max_hp = 222
knight2 = Knight(None) knight2 = Knight(None)
knight2.health = 1 knight2.max_hp = 1
knights_queue = KnightsQueue([knight1], [knight2]) knights_queue = KnightsQueue([knight1], [knight2])
previous_turn = knights_queue.team_idx_turn previous_turn = knights_queue.team_idx_turn
@ -82,13 +82,13 @@ class KnightsQueueTest(unittest.TestCase):
def test_should_raise_when_team_has_dead_knights(self): def test_should_raise_when_team_has_dead_knights(self):
with self.assertRaises(Exception): with self.assertRaises(Exception):
knight1 = Knight(None) knight1 = Knight(None)
knight1.health = 0 knight1.max_hp = 0
knight2 = Knight(None) knight2 = Knight(None)
knight2.health = -1 knight2.max_hp = -1
knight3 = Knight(None) knight3 = Knight(None)
knight3.health = -2 knight3.max_hp = -2
knight4 = Knight(None) knight4 = Knight(None)
knight4.health = 20 knight4.max_hp = 20
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4]) knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4])
@ -97,9 +97,9 @@ class KnightsQueueTest(unittest.TestCase):
def test_should_return_knight_from_any_team_and_add_to_queue_again(self): def test_should_return_knight_from_any_team_and_add_to_queue_again(self):
knight1 = Knight(None) knight1 = Knight(None)
knight1.health = 10 knight1.max_hp = 10
knight2 = Knight(None) knight2 = Knight(None)
knight2.health = 20 knight2.max_hp = 20
knights_queue = KnightsQueue([knight1], [knight2]) knights_queue = KnightsQueue([knight1], [knight2])
result1 = knights_queue.dequeue_knight() result1 = knights_queue.dequeue_knight()
@ -109,12 +109,12 @@ class KnightsQueueTest(unittest.TestCase):
self.assertIsNotNone(result1) self.assertIsNotNone(result1)
self.assertIsNotNone(result2) self.assertIsNotNone(result2)
self.assertIsNotNone(result3) self.assertIsNotNone(result3)
self.assertTrue(result1.health == result3.health) self.assertTrue(result1.max_hp == result3.max_hp)
def test_should_raise_when_only_one_team_alive(self): def test_should_raise_when_only_one_team_alive(self):
with self.assertRaises(Exception): with self.assertRaises(Exception):
knight = Knight(None) knight = Knight(None)
knight.health = 21 knight.max_hp = 21
knights_queue = KnightsQueue([knight], []) knights_queue = KnightsQueue([knight], [])
knights_queue.dequeue_knight() knights_queue.dequeue_knight()

View File

@ -10,6 +10,7 @@ from ui.screens.credits import Credits
from ui.screens.main_menu import MainMenu from ui.screens.main_menu import MainMenu
from ui.screens.options import Options from ui.screens.options import Options
from ui.stats import Stats from ui.stats import Stats
from logic.health_bar import HealthBar
class Game: class Game:
@ -35,6 +36,7 @@ class Game:
menu.display_screen() menu.display_screen()
def game(self): def game(self):
health_bar = HealthBar(self.screen, pygame.Rect(150, 150, 100, 15), 100, 100)
stats = Stats(self.screen) stats = Stats(self.screen)
# setup clock for rounds # setup clock for rounds
@ -63,7 +65,7 @@ class Game:
if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds
self.level.handle_turn() self.level.handle_turn()
stats.draw() stats.update()
self.logs.draw() self.logs.draw()
self.level.update() self.level.update()

View File

@ -1,5 +1,5 @@
import pygame import pygame
from common.constants import BAR_ANIMATION_SPEED, GRID_CELL_SIZE from common.constants import BAR_ANIMATION_SPEED, BAR_WIDTH_MULTIPLIER, BAR_HEIGHT_MULTIPLIER
from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK
@ -11,32 +11,30 @@ class HealthBar:
self.current_hp = current_hp self.current_hp = current_hp
self.target_hp = current_hp self.target_hp = current_hp
self.max_hp = max_hp self.max_hp = max_hp
self.width = self.rect.width
self.height = self.rect.height
self.x = self.rect.x self.x = self.rect.x
self.y = self.rect.y self.y = self.rect.y
self.calculate_xy = calculate_xy self.calculate_xy = calculate_xy
self.calculate_size = calculate_size
if calculate_size:
self.width = int(self.rect.width * BAR_WIDTH_MULTIPLIER) - 2
self.height = int(self.rect.width * BAR_HEIGHT_MULTIPLIER) - 2
else:
self.width = self.rect.width - 2
self.height = self.rect.height - 2
self.update_stats() self.update_stats()
def update(self): def update(self):
self.show()
self.update_stats() self.update_stats()
self.show()
def update_stats(self): 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: if self.calculate_xy:
self.x = int(GRID_CELL_SIZE * 0.1) + self.rect.x self.x = int(self.rect.width * (1 - BAR_WIDTH_MULTIPLIER)/2) + self.rect.x + 1
self.y = int(GRID_CELL_SIZE/2) + self.rect.y self.y = int(self.rect.height * BAR_HEIGHT_MULTIPLIER/2) + self.rect.y + 1
else: else:
self.width = self.rect.width self.x = self.rect.x + 1
self.height = self.rect.height self.y = self.rect.y + 1
self.health_ratio = self.max_hp / self.width self.health_ratio = self.max_hp / self.width
@ -55,7 +53,7 @@ class HealthBar:
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)-1, self.height)) pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.target_hp / self.health_ratio), self.height))

View File

@ -10,7 +10,7 @@ class KnightsQueue:
def dequeue_knight(self): def dequeue_knight(self):
if self.both_teams_alive(): if self.both_teams_alive():
knight = self.queues[self.team_idx_turn].popleft() knight = self.queues[self.team_idx_turn].popleft()
if knight.health <= 0: if knight.max_hp <= 0:
return self.dequeue_knight() return self.dequeue_knight()
else: else:
self.queues[self.team_idx_turn].append(knight) self.queues[self.team_idx_turn].append(knight)

View File

@ -96,13 +96,13 @@ class Level:
self.map[row_index][col_index] = knight self.map[row_index][col_index] = knight
self.list_knights_red.append(knight) self.list_knights_red.append(knight)
elif col == "m": elif col == "m":
monster = Monster((col_index, row_index), self.sprites) monster = Monster(self.screen, (col_index, row_index), self.sprites)
self.map[row_index][col_index] = monster self.map[row_index][col_index] = monster
self.list_monsters.append(monster) self.list_monsters.append(monster)
elif col == "c": elif col == "c":
castle_count += 1 castle_count += 1
if castle_count == 4: if castle_count == 4:
castle = Castle((col_index, row_index), self.sprites) castle = Castle(self.screen, (col_index, row_index), self.sprites)
self.map[row_index][col_index] = castle self.map[row_index][col_index] = castle
self.list_castles.append(castle) self.list_castles.append(castle)
@ -147,6 +147,19 @@ 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
@ -154,5 +167,4 @@ 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

View File

@ -1,10 +1,13 @@
import random
import pygame.image import pygame.image
from common.helpers import parse_cord from common.helpers import parse_cord
from logic.health_bar import HealthBar
class Castle(pygame.sprite.Sprite): class Castle(pygame.sprite.Sprite):
def __init__(self, position, group): def __init__(self, screen, position, group):
super().__init__(group) super().__init__(group)
self._layer = 1 self._layer = 1
self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha() self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha()
@ -12,4 +15,6 @@ class Castle(pygame.sprite.Sprite):
self.position = position self.position = position
position_in_px = (parse_cord(position[0]), parse_cord(position[1])) position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
self.rect = self.image.get_rect(center=position_in_px) self.rect = self.image.get_rect(center=position_in_px)
self.health = 80 self.max_hp = 80
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)

View File

@ -33,11 +33,12 @@ class Knight(pygame.sprite.Sprite):
self.rect = self.image.get_rect(topleft=position_in_px) self.rect = self.image.get_rect(topleft=position_in_px)
self.team = team self.team = team
self.health = 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, self.health, self.health) self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_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()

View File

@ -2,6 +2,7 @@ import pygame.image
import random import random
from common.helpers import parse_cord from common.helpers import parse_cord
from logic.health_bar import HealthBar
monster_images = [ monster_images = [
pygame.image.load("./resources/textures/dragon2.png"), pygame.image.load("./resources/textures/dragon2.png"),
@ -12,7 +13,7 @@ monster_images = [
class Monster(pygame.sprite.Sprite): class Monster(pygame.sprite.Sprite):
def __init__(self, position, group): def __init__(self, screen, position, group):
super().__init__(group) super().__init__(group)
self._layer = 1 self._layer = 1
self.image = random.choice(monster_images) self.image = random.choice(monster_images)
@ -20,21 +21,23 @@ class Monster(pygame.sprite.Sprite):
position_in_px = (parse_cord(position[0]), parse_cord(position[1])) position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
self.rect = self.image.get_rect(topleft=position_in_px) self.rect = self.image.get_rect(topleft=position_in_px)
self.health = random.randrange(15, 25) self.max_hp = random.randrange(15, 25)
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.attack = random.randrange(2, 10) self.attack = random.randrange(2, 10)
if self.image == monster_images[0]: if self.image == monster_images[0]:
self.health = 20 self.max_hp = 20
self.attack = 9 self.attack = 9
self.points = 10 self.points = 10
elif self.image == monster_images[1]: elif self.image == monster_images[1]:
self.health = 15 self.max_hp = 15
self.attack = 7 self.attack = 7
self.points = 7 self.points = 7
elif self.image == monster_images[2]: elif self.image == monster_images[2]:
self.health = 10 self.max_hp = 10
self.attack = 4 self.attack = 4
self.points = 4 self.points = 4
elif self.image == monster_images[3]: elif self.image == monster_images[3]:
self.health = 7 self.max_hp = 7
self.attack = 2 self.attack = 2
self.points = 2 self.points = 2

View File

@ -14,9 +14,9 @@ class Stats:
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, 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) self.red_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 210, self.y + 210, 100, 15), 100, 100)
def draw(self): def update(self):
# background # background
pygame.draw.rect(self.screen, WHITE, pygame.Rect(self.x, self.y, 340, 450), 0, BORDER_RADIUS) pygame.draw.rect(self.screen, WHITE, pygame.Rect(self.x, self.y, 340, 450), 0, BORDER_RADIUS)