Search code examples
pythonpygamegun

Pygame Bullet Hell Game


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.


Solution

  • 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