2
\$\begingroup\$

I just started to use classes in Python, so I don't know all concepts, rules and conventions about it. I made a little program of a machine gun, which simulates firing by user's action. Here it is:

import random, pygame, sys

class MachineGun(object):
    bullets = 200

    def __init__(self):
        self._magazine = 50

    def _get_magazine(self):
        return self._magazine

    def _set_magazine(self, val):
        self._magazine = val

    magazine = property(_get_magazine, _set_magazine)

    def fire(self):
        pygame.mixer.Sound("fire.wav").play()
        while pygame.mixer.get_busy():
            pass

        fired = random.randint(1, 21)
        self.magazine = self.magazine-fired

        if self.magazine <= 0 :
            print("Bullets remaining : 0")
        else :
            print("Bullets remaining : {}".format(self.magazine))

    def reload(self):
        if self.magazine == 50 :
            print("Full")

        else :
            if MachineGun.bullets >= 50 :
                if self.magazine == 0 :
                    self.magazine = 50
                    MachineGun.bullets = MachineGun.bullets-50
                else :
                    tmp = 50-self.magazine
                    self.magazine = self.magazine+tmp
                    MachineGun.bullets = MachineGun.bullets-tmp

            else :
                if self.magazine == 0 :
                    self.magazine = MachineGun.bullets
                    MachineGun.bullets = 0
                else :
                    if self.magazine+MachineGun.bullets > 50 :
                        tmp = 50 - self.magazine
                        self.magazine = self.magazine+tmp
                        MachineGun.bullets = MachineGun.bullets-tmp
                    else :
                        self.magazine += MachineGun.bullets
                        MachineGun.bullets = 0

        pygame.mixer.Sound("reload.wav").play()
        while pygame.mixer.get_busy():
            pass

    def action(self):
        a = "x"

        while a != "e" :
            if a == "" :
                if self.magazine > 0 :
                    self.fire()
                elif self.magazine <= 0 and MachineGun.bullets > 0 :
                    self.magazine = 0
                    print("Reload")
                else :
                    print("End")
                    sys.exit()

            elif a == "r" :
                self.reload()

            a = input()

M = MachineGun()
M.action()

This is somehow my first "complete" implementation using classes. So I don't know if the form is correct. For exemple, I modified the attribute bullets without using a class method. Should I ? Like properties for object attributs ? Is it very necessary to use properties ?

\$\endgroup\$
  • 1
    \$\begingroup\$ Added. bullets is somehow the stock, and magazine, bullets ready to fire, in the gun. When magazine is at 0, you reload it with bullets. I'm using python 3.7 \$\endgroup\$ – Biopy Oct 21 '18 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.