poprawki do healthbar oraz dostawania obrazen. TRZEBA UZYWAC FUNKCJI W knight.py

This commit is contained in:
XsedoX 2022-05-11 16:50:14 +02:00
parent 4ab8065879
commit 504cfd1af0
4 changed files with 37 additions and 18 deletions

View File

@ -37,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, level.list_knights_blue, level.list_knights_red)
# setup clock for rounds # setup clock for rounds
NEXT_TURN = pygame.USEREVENT + 1 NEXT_TURN = pygame.USEREVENT + 1
@ -45,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
@ -60,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

@ -15,8 +15,14 @@ class Stats:
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):
@ -35,6 +41,10 @@ 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
self.red_team_hp_bar.take_dmg(self.red_team_hp_bar.current_hp -
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()