Python Forum
Using an Abstract base for Game Weapon
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using an Abstract base for Game Weapon
#1
Hoping to get feedback and help when using Abstract classes. I had an example before, but it probably wasn't the best case for abstract classes. I came up with a new context, and decided to create a small text adventure game.

from abc import ABCMeta
from abc import abstractmethod


class Weapon(metaclass=ABCMeta):

    def __init__(self, name, damage):
        self.name = name
        self.damage = damage

    @abstractmethod
    def prepare(self):
        pass

    @abstractmethod
    def attack(self):
        pass

    @abstractmethod
    def cleanup(self):
        pass

    def __str__(self):
        return "Name: {} Damage: {}".format(self.name, self.damage)
from Weapon import Weapon


class ChargeGun(Weapon):

    def __init__(self, name, damage):
        super().__init__(name, damage)

    def prepare(self):
        print("Aiming my Gun");

    def attack(self):
        print("Shooting")

    def cleanup(self):
        print("Lowering gun, and turning off laser sight")
        
    def charge(self):
        print("Charging Gun. Please hold..(elevator music playing)")
from Weapon import Weapon


class Sword(Weapon):

    def __init__(self, name, damage):
        super().__init__(name, damage)

    def prepare(self):
        print("Raising my Sword");

    def attack(self):
        print("Attacking enemy with Sword")

    def cleanup(self):
        print("Wiping blood from Sword")
from ChargeGun import ChargeGun
from Sword import Sword


def main():

    cg =  ChargeGun("Standard ChargeGun",5)
    sw = Sword("Standard Sword", 3)

    cg.prepare()
    sw.prepare()
    
    cg.charge()


if __name__ == "__main__":
    main()
Goal: Implement an abstract base class, and create subclass to override the methods.

Questions:
1. Did I archive that goal? It complies and runs, but I've written enough code to know, just because it complies and runs doesn't mean it's the best practice. Have I correctly implemented and used an abstract base class?

2. Is my use of pass in the abstract base class appropriate? Given those methods are implemented in the subclass rather than the base class.
Reply
#2
Quote:
class Sword(Weapon):
 
    def __int__(self, name, damage):
        super().__init__(name, damage)

That will never run.  __int__ != __init__
But since you don't actually do anything in either of the constructors, it would appear to work fine, since the base constructor is being called anyway, as it's never overwritten.

Using pass for the base/abstract class is fine.
Reply
#3
Quote:That will never run.  __int__ != __init__
But since you don't actually do anything in either of the constructors, it would appear to work fine, since the base constructor is being called anyway, as it's never overwritten.

I meant init. I'm using PyCharm with a dark background, and a call to init turns it purple. Tongue
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text base adventure game ForbNovak 12 7,417 Aug-29-2018, 03:11 PM
Last Post: Nwb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020