Pygame Bullet Hell Game

Question:

I was wondering about bullets/guns in my bullet hell type game. In this game, there will be a shop that you can collect coins for and buy different types of guns and upgrades. I was wondering if I should create a class for each of these guns, or create one class and then add the different types of guns and upgrades as functions of the class.

Asked By: Hudson Rocke

||

Answers:

I prefer you should create a class for each type of new gun and then make methods for each class of gun and when you need the upgrade just call the method and the specific gun will be upgraded.
For example:

class Pistol:
    def __init__(self, grip, health, ammo):
        self.grip = grip
        self.health = health
        self.ammo = ammo

    def upgrade_ammo_capacity(self, value):
        self.ammo += value

    def health(self, value):
        self.health += value

    def grip(self, value):
        self.grip += value


# Here will be your logic or gameloop
# Simple example to implement this

pistol = Pistol()

if button_of_upgrade_ammo.clicked():
    pistol.upgrade_ammo_capacity(10)#any number based on how much player has bought. 
Answered By: Muhammad Umer

I would suggest you to create a Gun class and then extend it for different type of guns / guns. That way you will have a greater control and flexibility over different type of guns.
A change in how snipers will work should not affect how pistols work.

In your case, the upgrades on snipers will be different from a pistol, a sniper upgrade won’t be applied on a pistol. Right ?

@dataclass
class Gun:
    """
    Add global gun properties here
    """

    mag_capacity: int = 0
    shoot_range: int = 0
    mode: str = None
    ammo: int = 0

    def fire(self):
        """
        Sample fire implementation, can check if ammo is not none, if ammo is > 0 , then reduce ammo etc
        """

    def reload(self):
        """
        Sample reload capability
        """

   def equip_compensator(self):
       """
       This will be implemented by children classes
       """
       raise NotImplementedError()

   def equip_suppressor(self):
       """
       This will be implemented by children classes
       """
       raise NotImplementedError()
    

Now you can extend this Gun, to create different type of Guns.

@dataclass
class Sniper(Gun):
    mode: str = MODES.SINGLE_SHOT
    is_suppressor_equipped: bool = False

    def equip_suppressor(self):
       """
       Implementation to equip suppressor
       """
    

Similarly

@dataclass
class Rifle(Gun):
    mode: str = MODES.BURST
    is_compensator_equipped: bool = False

    def equip_compensator(self):
        """
        Implmentation of equipping compensator
        """
@dataclass
class Pistol(Gun):
    mode: str = MODES.MULTI_SHOT
    is_silencer_quipped: bool = False
   
    ...
    <required functionalities here>
    ...
    

This is just an example of how I would suggest you to decouple your entities to have a greater control and flexibility. Take the answer with a pinch of salt.

Thanks

Answered By: Vikrant Srivastava
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.