Module game_qu.platformer.health_bar
Expand source code
from game_qu.gui_components.component import Component
from game_qu.base.important_variables import SCREEN_HEIGHT
from game_qu.base.velocity_calculator import VelocityCalculator
from game_qu.gui_components.component import Component
from game_qu.base.colors import *
class HealthBar(Component):
"""A health bar for a game character"""
game_character = None
default_height = VelocityCalculator.get_dimension(SCREEN_HEIGHT, 3)
height = default_height
health_remaining_bar = None
health_gone_bar = None
set_size_function = None
is_addable = False
def __init__(self, game_character, set_size_function=None):
"""Initializes the object"""
super().__init__("")
self.game_character = game_character
self.health_remaining_bar = Component("")
self.health_gone_bar = Component("")
self.health_remaining_bar.color = medium_green
self.health_gone_bar.color = red
self.set_size_function = set_size_function if set_size_function is not None else self.default_set_size_function
self.set_size_function()
def run(self):
"""Runs all the code necessary for the health bar to work"""
self.set_size_function()
def render(self):
"""Renders the health bar onto the screen"""
# Setting where the bars should be
length_ratio = self.length / self.game_character.total_hit_points
self.health_gone_bar.top_edge, self.health_remaining_bar.top_edge = self.top_edge, self.top_edge
self.health_remaining_bar.height, self.health_gone_bar.height = self.height, self.height
self.health_remaining_bar.left_edge = self.left_edge
self.health_remaining_bar.length = length_ratio * self.game_character.hit_points_left
self.health_gone_bar.left_edge = self.health_remaining_bar.right_edge
# So there is not a rounding error producing a small part of the health bar being red
self.health_gone_bar.length = self.length - self.health_remaining_bar.length
# Rendering
self.health_remaining_bar.render()
# Even if the length of it is 0, then it is still being rendered on the screen (a small sliver), so this prevents that
if self.health_gone_bar.length != 0:
self.health_gone_bar.render()
def default_set_size_function(self):
"""Runs the default way to size the health bar"""
self.height = self.game_character.height * .1
self.left_edge, self.length = self.game_character.left_edge, self.game_character.length
self.top_edge = self.game_character.top_edge - self.height
Classes
class HealthBar (game_character, set_size_function=None)-
A health bar for a game character
Initializes the object
Expand source code
class HealthBar(Component): """A health bar for a game character""" game_character = None default_height = VelocityCalculator.get_dimension(SCREEN_HEIGHT, 3) height = default_height health_remaining_bar = None health_gone_bar = None set_size_function = None is_addable = False def __init__(self, game_character, set_size_function=None): """Initializes the object""" super().__init__("") self.game_character = game_character self.health_remaining_bar = Component("") self.health_gone_bar = Component("") self.health_remaining_bar.color = medium_green self.health_gone_bar.color = red self.set_size_function = set_size_function if set_size_function is not None else self.default_set_size_function self.set_size_function() def run(self): """Runs all the code necessary for the health bar to work""" self.set_size_function() def render(self): """Renders the health bar onto the screen""" # Setting where the bars should be length_ratio = self.length / self.game_character.total_hit_points self.health_gone_bar.top_edge, self.health_remaining_bar.top_edge = self.top_edge, self.top_edge self.health_remaining_bar.height, self.health_gone_bar.height = self.height, self.height self.health_remaining_bar.left_edge = self.left_edge self.health_remaining_bar.length = length_ratio * self.game_character.hit_points_left self.health_gone_bar.left_edge = self.health_remaining_bar.right_edge # So there is not a rounding error producing a small part of the health bar being red self.health_gone_bar.length = self.length - self.health_remaining_bar.length # Rendering self.health_remaining_bar.render() # Even if the length of it is 0, then it is still being rendered on the screen (a small sliver), so this prevents that if self.health_gone_bar.length != 0: self.health_gone_bar.render() def default_set_size_function(self): """Runs the default way to size the health bar""" self.height = self.game_character.height * .1 self.left_edge, self.length = self.game_character.left_edge, self.game_character.length self.top_edge = self.game_character.top_edge - self.heightAncestors
Class variables
var default_heightvar game_charactervar health_gone_barvar health_remaining_barvar heightvar is_addablevar set_size_function
Methods
def default_set_size_function(self)-
Runs the default way to size the health bar
Expand source code
def default_set_size_function(self): """Runs the default way to size the health bar""" self.height = self.game_character.height * .1 self.left_edge, self.length = self.game_character.left_edge, self.game_character.length self.top_edge = self.game_character.top_edge - self.height def render(self)-
Renders the health bar onto the screen
Expand source code
def render(self): """Renders the health bar onto the screen""" # Setting where the bars should be length_ratio = self.length / self.game_character.total_hit_points self.health_gone_bar.top_edge, self.health_remaining_bar.top_edge = self.top_edge, self.top_edge self.health_remaining_bar.height, self.health_gone_bar.height = self.height, self.height self.health_remaining_bar.left_edge = self.left_edge self.health_remaining_bar.length = length_ratio * self.game_character.hit_points_left self.health_gone_bar.left_edge = self.health_remaining_bar.right_edge # So there is not a rounding error producing a small part of the health bar being red self.health_gone_bar.length = self.length - self.health_remaining_bar.length # Rendering self.health_remaining_bar.render() # Even if the length of it is 0, then it is still being rendered on the screen (a small sliver), so this prevents that if self.health_gone_bar.length != 0: self.health_gone_bar.render() def run(self)-
Runs all the code necessary for the health bar to work
Expand source code
def run(self): """Runs all the code necessary for the health bar to work""" self.set_size_function()
Inherited members