healthbar #22
@ -2,5 +2,6 @@ BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
ORANGE = (249, 141, 42)
|
||||
RED = (255, 58, 58)
|
||||
GREEN = (0, 255, 0)
|
||||
|
||||
FONT_DARK = (37, 37, 37)
|
||||
|
@ -62,3 +62,8 @@ ACTION = {
|
||||
"rotate_right": 1,
|
||||
"go": 0,
|
||||
}
|
||||
|
||||
# HEALTH_BAR
|
||||
BAR_ANIMATION_SPEED = 1
|
||||
BAR_WIDTH_MULTIPLIER = 0.9 # (0;1>
|
||||
BAR_HEIGHT_MULTIPLIER = 0.1
|
||||
|
@ -7,40 +7,40 @@ from models.knight import Knight
|
||||
class KnightsQueueTest(unittest.TestCase):
|
||||
def test_should_skip_dead_knights(self):
|
||||
knight1 = Knight(None)
|
||||
knight1.health = 0
|
||||
knight1.max_hp = 0
|
||||
|
||||
knight2 = Knight(None)
|
||||
knight2.health = 0
|
||||
knight2.max_hp = 0
|
||||
|
||||
knight3 = Knight(None)
|
||||
knight3.health = 1
|
||||
knight3.max_hp = 1
|
||||
|
||||
knight4 = Knight(None)
|
||||
knight4.health = 0
|
||||
knight4.max_hp = 0
|
||||
|
||||
knight5 = Knight(None)
|
||||
knight5.health = 0
|
||||
knight5.max_hp = 0
|
||||
|
||||
knight6 = Knight(None)
|
||||
knight6.health = 1
|
||||
knight6.max_hp = 1
|
||||
|
||||
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4, knight5, knight6])
|
||||
|
||||
res1 = knights_queue.dequeue_knight()
|
||||
res2 = knights_queue.dequeue_knight()
|
||||
|
||||
self.assertEqual(res1.health, 1)
|
||||
self.assertEqual(res2.health, 1)
|
||||
self.assertEqual(res1.max_hp, 1)
|
||||
self.assertEqual(res2.max_hp, 1)
|
||||
|
||||
def test_should_return_first_alive_knight(self):
|
||||
knight1 = Knight(None)
|
||||
knight1.health = 222
|
||||
knight1.max_hp = 222
|
||||
|
||||
knight2 = Knight(None)
|
||||
knight2.health = -1
|
||||
knight2.max_hp = -1
|
||||
|
||||
knight3 = Knight(None)
|
||||
knight3.health = 1
|
||||
knight3.max_hp = 1
|
||||
|
||||
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):
|
||||
with self.assertRaises(Exception):
|
||||
knight1 = Knight(None)
|
||||
knight1.health = 222
|
||||
knight1.max_hp = 222
|
||||
knight2 = Knight(None)
|
||||
knight2.health = 1
|
||||
knight2.max_hp = 1
|
||||
|
||||
knights_queue = KnightsQueue([knight1], [knight2])
|
||||
knights_queue.dequeue_knight()
|
||||
knights_queue.dequeue_knight()
|
||||
knight2.health = -2
|
||||
knight2.max_hp = -2
|
||||
knights_queue.dequeue_knight()
|
||||
knights_queue.dequeue_knight()
|
||||
|
||||
def test_should_make_valid_next_turn(self):
|
||||
knight1 = Knight(None)
|
||||
knight1.health = 222
|
||||
knight1.max_hp = 222
|
||||
knight2 = Knight(None)
|
||||
knight2.health = 1
|
||||
knight2.max_hp = 1
|
||||
|
||||
knights_queue = KnightsQueue([knight1], [knight2])
|
||||
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):
|
||||
with self.assertRaises(Exception):
|
||||
knight1 = Knight(None)
|
||||
knight1.health = 0
|
||||
knight1.max_hp = 0
|
||||
knight2 = Knight(None)
|
||||
knight2.health = -1
|
||||
knight2.max_hp = -1
|
||||
knight3 = Knight(None)
|
||||
knight3.health = -2
|
||||
knight3.max_hp = -2
|
||||
knight4 = Knight(None)
|
||||
knight4.health = 20
|
||||
knight4.max_hp = 20
|
||||
|
||||
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):
|
||||
knight1 = Knight(None)
|
||||
knight1.health = 10
|
||||
knight1.max_hp = 10
|
||||
knight2 = Knight(None)
|
||||
knight2.health = 20
|
||||
knight2.max_hp = 20
|
||||
knights_queue = KnightsQueue([knight1], [knight2])
|
||||
|
||||
result1 = knights_queue.dequeue_knight()
|
||||
@ -109,12 +109,12 @@ class KnightsQueueTest(unittest.TestCase):
|
||||
self.assertIsNotNone(result1)
|
||||
self.assertIsNotNone(result2)
|
||||
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):
|
||||
with self.assertRaises(Exception):
|
||||
knight = Knight(None)
|
||||
knight.health = 21
|
||||
knight.max_hp = 21
|
||||
knights_queue = KnightsQueue([knight], [])
|
||||
knights_queue.dequeue_knight()
|
||||
|
||||
|
@ -10,6 +10,7 @@ from ui.screens.credits import Credits
|
||||
from ui.screens.main_menu import MainMenu
|
||||
from ui.screens.options import Options
|
||||
from ui.stats import Stats
|
||||
from logic.health_bar import HealthBar
|
||||
|
||||
|
||||
class Game:
|
||||
@ -35,7 +36,8 @@ class Game:
|
||||
menu.display_screen()
|
||||
|
||||
def game(self):
|
||||
stats = Stats()
|
||||
health_bar = HealthBar(self.screen, pygame.Rect(150, 150, 100, 15), 100, 100)
|
||||
stats = Stats(self.screen)
|
||||
|
||||
# setup clock for rounds
|
||||
NEXT_TURN = pygame.USEREVENT + 1
|
||||
@ -56,14 +58,15 @@ class Game:
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
running = False
|
||||
if event.key == 110: # clicked n letter on keyboard
|
||||
if event.key == pygame.K_n:
|
||||
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
|
||||
self.level.handle_turn()
|
||||
|
||||
stats.draw(self.screen)
|
||||
stats.update()
|
||||
self.logs.draw()
|
||||
|
||||
self.level.update()
|
||||
|
||||
if print_numbers_flag:
|
||||
|
60
logic/health_bar.py
Normal file
60
logic/health_bar.py
Normal file
@ -0,0 +1,60 @@
|
||||
import pygame
|
||||
from common.constants import BAR_ANIMATION_SPEED, BAR_WIDTH_MULTIPLIER, BAR_HEIGHT_MULTIPLIER
|
||||
from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK
|
||||
|
||||
|
||||
class HealthBar:
|
||||
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.x = self.rect.x
|
||||
self.y = self.rect.y
|
||||
self.calculate_xy = calculate_xy
|
||||
|
||||
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()
|
||||
|
||||
def update(self):
|
||||
self.update_stats()
|
||||
self.show()
|
||||
|
||||
def update_stats(self):
|
||||
if self.calculate_xy:
|
||||
self.x = int(self.rect.width * (1 - BAR_WIDTH_MULTIPLIER)/2) + self.rect.x + 1
|
||||
self.y = int(self.rect.height * BAR_HEIGHT_MULTIPLIER/2) + self.rect.y + 1
|
||||
else:
|
||||
self.x = self.rect.x + 1
|
||||
self.y = self.rect.y + 1
|
||||
|
||||
self.health_ratio = self.max_hp / self.width
|
||||
|
||||
def take_dmg(self, dmg_taken):
|
||||
if self.target_hp > 0:
|
||||
self.target_hp -= dmg_taken
|
||||
elif self.target_hp < 0:
|
||||
self.target_hp = 0
|
||||
|
||||
def heal(self, amount):
|
||||
if self.target_hp < self.max_hp:
|
||||
self.target_hp += amount
|
||||
elif self.target_hp > self.max_hp:
|
||||
self.target_hp = self.max_hp
|
||||
|
||||
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, 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))
|
||||
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@ class KnightsQueue:
|
||||
def dequeue_knight(self):
|
||||
if self.both_teams_alive():
|
||||
knight = self.queues[self.team_idx_turn].popleft()
|
||||
if knight.health <= 0:
|
||||
if knight.max_hp <= 0:
|
||||
return self.dequeue_knight()
|
||||
else:
|
||||
self.queues[self.team_idx_turn].append(knight)
|
||||
|
@ -18,7 +18,7 @@ class Level:
|
||||
self.screen = screen
|
||||
self.logs = logs
|
||||
# sprite group setup
|
||||
self.sprites = pygame.sprite.Group()
|
||||
self.sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.map = [['g' for _ in range(COLUMNS)] for y in range(ROWS)]
|
||||
|
||||
@ -88,21 +88,21 @@ 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":
|
||||
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.list_monsters.append(monster)
|
||||
elif col == "c":
|
||||
castle_count += 1
|
||||
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.list_castles.append(castle)
|
||||
|
||||
@ -147,6 +147,19 @@ class Level:
|
||||
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()
|
||||
|
||||
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):
|
||||
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
|
||||
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
|
||||
@ -154,3 +167,4 @@ class Level:
|
||||
|
||||
# update and draw the game
|
||||
self.sprites.draw(self.screen)
|
||||
self.update_health_bars() # has to be called last
|
||||
|
@ -1,14 +1,20 @@
|
||||
import random
|
||||
|
||||
import pygame.image
|
||||
|
||||
from common.helpers import parse_cord
|
||||
from logic.health_bar import HealthBar
|
||||
|
||||
|
||||
class Castle(pygame.sprite.Sprite):
|
||||
def __init__(self, position, group):
|
||||
def __init__(self, screen, position, group):
|
||||
super().__init__(group)
|
||||
self._layer = 1
|
||||
self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha()
|
||||
self.image = pygame.transform.scale(self.image, (78, 78))
|
||||
self.position = position
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
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)
|
||||
|
@ -4,6 +4,7 @@ import pygame.image
|
||||
|
||||
from common.constants import GRID_CELL_SIZE, Direction
|
||||
from common.helpers import parse_cord
|
||||
from logic.health_bar import HealthBar
|
||||
|
||||
|
||||
def load_knight_textures():
|
||||
@ -19,7 +20,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
|
||||
@ -27,14 +28,17 @@ class Knight(pygame.sprite.Sprite):
|
||||
|
||||
self.image = self.states[self.direction.value]
|
||||
self.position = position
|
||||
self._layer = 1
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||
|
||||
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.defense = random.randint(1, 4)
|
||||
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)
|
||||
|
||||
def rotate_left(self):
|
||||
self.direction = self.direction.left()
|
||||
|
@ -2,6 +2,7 @@ import pygame.image
|
||||
import random
|
||||
|
||||
from common.helpers import parse_cord
|
||||
from logic.health_bar import HealthBar
|
||||
|
||||
monster_images = [
|
||||
pygame.image.load("./resources/textures/dragon2.png"),
|
||||
@ -12,28 +13,31 @@ monster_images = [
|
||||
|
||||
|
||||
class Monster(pygame.sprite.Sprite):
|
||||
def __init__(self, position, group):
|
||||
def __init__(self, screen, position, group):
|
||||
super().__init__(group)
|
||||
self._layer = 1
|
||||
self.image = random.choice(monster_images)
|
||||
self.image = pygame.transform.scale(self.image, (40, 40))
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
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)
|
||||
if self.image == monster_images[0]:
|
||||
self.health = 20
|
||||
self.max_hp = 20
|
||||
self.attack = 9
|
||||
self.points = 10
|
||||
elif self.image == monster_images[1]:
|
||||
self.health = 15
|
||||
self.max_hp = 15
|
||||
self.attack = 7
|
||||
self.points = 7
|
||||
elif self.image == monster_images[2]:
|
||||
self.health = 10
|
||||
self.max_hp = 10
|
||||
self.attack = 4
|
||||
self.points = 4
|
||||
elif self.image == monster_images[3]:
|
||||
self.health = 7
|
||||
self.max_hp = 7
|
||||
self.attack = 2
|
||||
self.points = 2
|
||||
|
@ -7,6 +7,7 @@ class Tile(pygame.sprite.Sprite):
|
||||
def __init__(self, position, image, group, tile_type=' '):
|
||||
super().__init__(group)
|
||||
self.image = image
|
||||
self._layer = 0
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||
self.tile_type = tile_type
|
||||
self.tile_type = tile_type
|
||||
|
47
ui/stats.py
47
ui/stats.py
@ -1,44 +1,53 @@
|
||||
import pygame
|
||||
import time
|
||||
|
||||
from logic.health_bar import HealthBar
|
||||
from common.colors import FONT_DARK, ORANGE, WHITE, RED
|
||||
from common.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_SIZE, BORDER_WIDTH, BORDER_RADIUS
|
||||
from common.helpers import draw_text
|
||||
|
||||
|
||||
class Stats:
|
||||
def __init__(self):
|
||||
def __init__(self, screen):
|
||||
self.grid = []
|
||||
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, 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), 100, 100)
|
||||
|
||||
def draw(self, screen):
|
||||
x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
|
||||
y = 5
|
||||
def update(self):
|
||||
|
||||
# background
|
||||
pygame.draw.rect(screen, WHITE, pygame.Rect(x, y, 340, 450), 0, BORDER_RADIUS)
|
||||
pygame.draw.rect(self.screen, WHITE, pygame.Rect(self.x, self.y, 340, 450), 0, BORDER_RADIUS)
|
||||
|
||||
# title
|
||||
draw_text('STATS', FONT_DARK, screen, x + 120, y + 10, 36)
|
||||
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3))
|
||||
draw_text('STATS', FONT_DARK, self.screen, self.x + 120, self.y + 10, 36)
|
||||
pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 65, 340, 3))
|
||||
|
||||
# shields
|
||||
shield_blue = pygame.image.load('./resources/textures/shield_blue.png')
|
||||
shield_red = pygame.image.load('./resources/textures/shield_red.png')
|
||||
screen.blit(shield_blue, (x + 20, y + 80))
|
||||
screen.blit(shield_red, (x + 200, y + 80))
|
||||
draw_text('VS', FONT_DARK, screen, x + 150, y + 120, 36)
|
||||
self.screen.blit(shield_blue, (self.x + 20, self.y + 80))
|
||||
self.screen.blit(shield_red, (self.x + 200, self.y + 80))
|
||||
draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36)
|
||||
|
||||
# HP bars
|
||||
pygame.draw.rect(screen, RED, pygame.Rect(x + 30, y + 210, 100, 15), 0, 4)
|
||||
pygame.draw.rect(screen, RED, pygame.Rect(x + 210, y + 210, 100, 15), 0, 4)
|
||||
#pygame.draw.rect(screen, RED, pygame.Rect(x + 30, y + 210, 100, 15), 0, 4)
|
||||
#pygame.draw.rect(screen, RED, pygame.Rect(x + 210, y + 210, 100, 15), 0, 4)
|
||||
self.red_team_hp_bar.update()
|
||||
self.blue_team_hp_bar.update()
|
||||
|
||||
|
||||
|
||||
# texts
|
||||
draw_text('Rycerze: 2', FONT_DARK, screen, x + 35, y + 240, 18)
|
||||
draw_text('Fortece: 1', FONT_DARK, screen, x + 35, y + 270, 18)
|
||||
draw_text('Rycerze: 2', FONT_DARK, self.screen, self.x + 35, self.y + 240, 18)
|
||||
draw_text('Fortece: 1', FONT_DARK, self.screen, self.x + 35, self.y + 270, 18)
|
||||
|
||||
draw_text('Rycerze: 4', FONT_DARK, screen, x + 215, y + 240, 18)
|
||||
draw_text('Fortece: 0', FONT_DARK, screen, x + 215, y + 270, 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)
|
||||
|
||||
# points
|
||||
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 390, 340, 3))
|
||||
draw_text('PUNKTY: 10', FONT_DARK, screen, x + 35, y + 408, 18, True)
|
||||
draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True)
|
||||
pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))
|
||||
draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True)
|
||||
draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True)
|
||||
|
Loading…
Reference in New Issue
Block a user