healthbar WIP - team health bars jako klasa health_bar.py

This commit is contained in:
XsedoX 2022-04-24 22:06:20 +02:00
parent e2fad68813
commit e5af464eb2
4 changed files with 62 additions and 43 deletions

View File

@ -2,5 +2,6 @@ BLACK = (0, 0, 0)
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
ORANGE = (249, 141, 42) ORANGE = (249, 141, 42)
RED = (255, 58, 58) RED = (255, 58, 58)
GREEN = (0, 255, 0)
FONT_DARK = (37, 37, 37) FONT_DARK = (37, 37, 37)

View File

@ -34,7 +34,7 @@ class Game:
menu.display_screen() menu.display_screen()
def game(self): def game(self):
stats = Stats() stats = Stats(self.screen)
logs = Logs() logs = Logs()
# setup clock for rounds # setup clock for rounds
@ -56,12 +56,16 @@ class Game:
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
running = False running = False
if event.key == 110: # clicked n letter on keyboard 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_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 if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds
self.level.handle_turn() self.level.handle_turn()
stats.draw(self.screen) stats.draw()
logs.draw(self.screen) logs.draw(self.screen)
self.level.update() self.level.update()

View File

@ -1,22 +1,34 @@
import pygame import pygame
from common.constants import BAR_ANIMATION_SPEED from common.constants import BAR_ANIMATION_SPEED, GRID_CELL_SIZE
from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK
class HealthBar: class HealthBar:
def __init__(self, rect, current_hp, max_hp): def __init__(self, screen, rect, current_hp, max_hp, x=None, y=None, width=None, height=None):
self.rect = rect self.rect = rect
self.bar_animation_speed = BAR_ANIMATION_SPEED self.screen = screen
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 * 0.8 self.width = width
self.height = self.rect.height * 0.05 self.height = height
self.x = self.rect.width * 0.1 + self.rect.x self.x = x
self.y = self.rect.height * 0.95 + self.rect.y 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 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
def update(self): def update(self):
self.animation() self.show()
def take_dmg(self, dmg_taken): def take_dmg(self, dmg_taken):
if self.target_hp > 0: if self.target_hp > 0:
@ -30,19 +42,12 @@ class HealthBar:
elif self.target_hp > self.max_hp: elif self.target_hp > self.max_hp:
self.target_hp = self.max_hp self.target_hp = self.max_hp
def animation(self): def show(self):
animation_bar_width = 0 pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2))
animation_bar_color = (255, 0, 0) 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))
if self.current_hp < self.target_hp:
self.current_hp += self.bar_animation_speed
animation_bar_width = int((self.current_hp - self.target_hp) / self.health_ratio)
animation_bar_color = (0, 255, 0)
elif self.current_hp > self.target_hp:
self.current_hp -= self.bar_animation_speed
animation_bar_width = int((self.target_hp - self.current_hp) / self.health_ratio)
animation_bar_color = (255, 255, 0)

View File

@ -1,44 +1,53 @@
import pygame import pygame
import time
from logic.health_bar import HealthBar
from common.colors import FONT_DARK, ORANGE, WHITE, RED 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.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_SIZE, BORDER_WIDTH, BORDER_RADIUS
from common.helpers import draw_text from common.helpers import draw_text
class Stats: class Stats:
def __init__(self): def __init__(self, screen):
self.grid = [] 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, (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)
def draw(self, screen): def draw(self):
x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
y = 5
# background # 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 # title
draw_text('STATS', FONT_DARK, screen, x + 120, y + 10, 36) draw_text('STATS', FONT_DARK, self.screen, self.x + 120, self.y + 10, 36)
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 65, 340, 3))
# shields # shields
shield_blue = pygame.image.load('./resources/textures/shield_blue.png') shield_blue = pygame.image.load('./resources/textures/shield_blue.png')
shield_red = pygame.image.load('./resources/textures/shield_red.png') shield_red = pygame.image.load('./resources/textures/shield_red.png')
screen.blit(shield_blue, (x + 20, y + 80)) self.screen.blit(shield_blue, (self.x + 20, self.y + 80))
screen.blit(shield_red, (x + 200, y + 80)) self.screen.blit(shield_red, (self.x + 200, self.y + 80))
draw_text('VS', FONT_DARK, screen, x + 150, 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) #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 + 210, y + 210, 100, 15), 0, 4)
self.red_team_hp_bar.update()
self.blue_team_hp_bar.update()
# texts # texts
draw_text('Rycerze: 2', FONT_DARK, screen, x + 35, y + 240, 18) draw_text('Rycerze: 2', FONT_DARK, self.screen, self.x + 35, self.y + 240, 18)
draw_text('Fortece: 1', FONT_DARK, screen, x + 35, y + 270, 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('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18)
draw_text('Fortece: 0', FONT_DARK, screen, x + 215, y + 270, 18) draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 215, self.y + 270, 18)
# points # points
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 390, 340, 3)) pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))
draw_text('PUNKTY: 10', FONT_DARK, screen, x + 35, y + 408, 18, True) draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True)
draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True) draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True)