Module game_qu.platformer.weapon

Expand source code
from game_qu.base.id_creator import id_creator
import abc
from game_qu.base.events import Event, TimedEvent


class Weapon(abc.ABC):
    """Something the user can use to hit enemies or anything else"""

    base_damage = 20
    damage = base_damage
    total_hit_points = 0
    hit_points_left = total_hit_points
    use_action = None
    use_key_event = None
    user = None
    collidable_components = []
    wait_event = None
    object_type = "" # Used for collisions, so the collision code knows the type of an object
    index = 0
    max_ammo = 10
    ammo_left = max_ammo
    has_limited_ammo = True

    def __init__(self, damage, hit_points, use_action, user, cool_down_time):
        """Initilizes the object"""

        self.use_key_event = Event()
        self.use_action = use_action
        self.user = user
        self.name = id_creator.get_unique_id()
        self.wait_event = TimedEvent(cool_down_time, False)
        self.collidable_components = []
        self.object_type = f"{self.user.user_type} Weapon"

        self.update_weapon_values(damage, hit_points, cool_down_time)

    def run(self):
        """Runs all the code for that is necessary to use a weapon"""

        self.use_key_event.run(self.use_action())
        self.wait_event.run(self.wait_event.current_time >= self.wait_event.time_needed, False)

        if self.use_key_event.is_click() and self.wait_event.has_finished():
            self.run_upon_activation()
            self.wait_event.start()

    def update_weapon_values(self, damage, hit_points, cool_down_time):
        """Updates the values of the weapon that can be modified"""

        self.base_damage, self.damage = damage, damage
        self.total_hit_points, self.hit_points_left = hit_points, hit_points
        self.wait_event.time_needed = cool_down_time

    def get_collidable_components(self):
        """
            Returns:
                Component[]: all the sub components that must be rendered and have collisions for"""

        return self.collidable_components

    def get_weapon_left_edge(self, horizontal_length, is_facing_right):
        """
            Returns:
                left_edge: the recommended x coordinate that the weapon should be at (right on the user)"""

        return self.user.right_edge if is_facing_right else self.user.left_edge - horizontal_length

    def reset(self):
        pass

    def update_for_side_scrolling(self, amount):
        pass

    @abc.abstractmethod
    def run_enemy_collision(self, enemy, index_of_sub_component):
        """Runs what should happen when an enemy and the weapon collide"""
        pass

    @abc.abstractmethod
    def run_inanimate_object_collision(self, inanimate_object, index_of_sub_component):
        """Runs what should happen when the weapon and an inanimate object collide"""
        pass

    @abc.abstractmethod
    def run_upon_activation(self):
        """Runs what should happen when the person who plays the game tries to use the weapon"""
        pass

    @abc.abstractmethod
    def reset(self):
        """Resets everything back to the start of the game"""
        pass

Classes

class Weapon (damage, hit_points, use_action, user, cool_down_time)

Something the user can use to hit enemies or anything else

Initilizes the object

Expand source code
class Weapon(abc.ABC):
    """Something the user can use to hit enemies or anything else"""

    base_damage = 20
    damage = base_damage
    total_hit_points = 0
    hit_points_left = total_hit_points
    use_action = None
    use_key_event = None
    user = None
    collidable_components = []
    wait_event = None
    object_type = "" # Used for collisions, so the collision code knows the type of an object
    index = 0
    max_ammo = 10
    ammo_left = max_ammo
    has_limited_ammo = True

    def __init__(self, damage, hit_points, use_action, user, cool_down_time):
        """Initilizes the object"""

        self.use_key_event = Event()
        self.use_action = use_action
        self.user = user
        self.name = id_creator.get_unique_id()
        self.wait_event = TimedEvent(cool_down_time, False)
        self.collidable_components = []
        self.object_type = f"{self.user.user_type} Weapon"

        self.update_weapon_values(damage, hit_points, cool_down_time)

    def run(self):
        """Runs all the code for that is necessary to use a weapon"""

        self.use_key_event.run(self.use_action())
        self.wait_event.run(self.wait_event.current_time >= self.wait_event.time_needed, False)

        if self.use_key_event.is_click() and self.wait_event.has_finished():
            self.run_upon_activation()
            self.wait_event.start()

    def update_weapon_values(self, damage, hit_points, cool_down_time):
        """Updates the values of the weapon that can be modified"""

        self.base_damage, self.damage = damage, damage
        self.total_hit_points, self.hit_points_left = hit_points, hit_points
        self.wait_event.time_needed = cool_down_time

    def get_collidable_components(self):
        """
            Returns:
                Component[]: all the sub components that must be rendered and have collisions for"""

        return self.collidable_components

    def get_weapon_left_edge(self, horizontal_length, is_facing_right):
        """
            Returns:
                left_edge: the recommended x coordinate that the weapon should be at (right on the user)"""

        return self.user.right_edge if is_facing_right else self.user.left_edge - horizontal_length

    def reset(self):
        pass

    def update_for_side_scrolling(self, amount):
        pass

    @abc.abstractmethod
    def run_enemy_collision(self, enemy, index_of_sub_component):
        """Runs what should happen when an enemy and the weapon collide"""
        pass

    @abc.abstractmethod
    def run_inanimate_object_collision(self, inanimate_object, index_of_sub_component):
        """Runs what should happen when the weapon and an inanimate object collide"""
        pass

    @abc.abstractmethod
    def run_upon_activation(self):
        """Runs what should happen when the person who plays the game tries to use the weapon"""
        pass

    @abc.abstractmethod
    def reset(self):
        """Resets everything back to the start of the game"""
        pass

Ancestors

  • abc.ABC

Class variables

var ammo_left
var base_damage
var collidable_components
var damage
var has_limited_ammo
var hit_points_left
var index
var max_ammo
var object_type
var total_hit_points
var use_action
var use_key_event
var user
var wait_event

Methods

def get_collidable_components(self)

Returns

Component[]
all the sub components that must be rendered and have collisions for
Expand source code
def get_collidable_components(self):
    """
        Returns:
            Component[]: all the sub components that must be rendered and have collisions for"""

    return self.collidable_components
def get_weapon_left_edge(self, horizontal_length, is_facing_right)

Returns

left_edge
the recommended x coordinate that the weapon should be at (right on the user)
Expand source code
def get_weapon_left_edge(self, horizontal_length, is_facing_right):
    """
        Returns:
            left_edge: the recommended x coordinate that the weapon should be at (right on the user)"""

    return self.user.right_edge if is_facing_right else self.user.left_edge - horizontal_length
def reset(self)

Resets everything back to the start of the game

Expand source code
@abc.abstractmethod
def reset(self):
    """Resets everything back to the start of the game"""
    pass
def run(self)

Runs all the code for that is necessary to use a weapon

Expand source code
def run(self):
    """Runs all the code for that is necessary to use a weapon"""

    self.use_key_event.run(self.use_action())
    self.wait_event.run(self.wait_event.current_time >= self.wait_event.time_needed, False)

    if self.use_key_event.is_click() and self.wait_event.has_finished():
        self.run_upon_activation()
        self.wait_event.start()
def run_enemy_collision(self, enemy, index_of_sub_component)

Runs what should happen when an enemy and the weapon collide

Expand source code
@abc.abstractmethod
def run_enemy_collision(self, enemy, index_of_sub_component):
    """Runs what should happen when an enemy and the weapon collide"""
    pass
def run_inanimate_object_collision(self, inanimate_object, index_of_sub_component)

Runs what should happen when the weapon and an inanimate object collide

Expand source code
@abc.abstractmethod
def run_inanimate_object_collision(self, inanimate_object, index_of_sub_component):
    """Runs what should happen when the weapon and an inanimate object collide"""
    pass
def run_upon_activation(self)

Runs what should happen when the person who plays the game tries to use the weapon

Expand source code
@abc.abstractmethod
def run_upon_activation(self):
    """Runs what should happen when the person who plays the game tries to use the weapon"""
    pass
def update_for_side_scrolling(self, amount)
Expand source code
def update_for_side_scrolling(self, amount):
    pass
def update_weapon_values(self, damage, hit_points, cool_down_time)

Updates the values of the weapon that can be modified

Expand source code
def update_weapon_values(self, damage, hit_points, cool_down_time):
    """Updates the values of the weapon that can be modified"""

    self.base_damage, self.damage = damage, damage
    self.total_hit_points, self.hit_points_left = hit_points, hit_points
    self.wait_event.time_needed = cool_down_time