using class to make games ..following the OOP way

one thing that i like a lot of python is these thing :
you call a obj , and this have different tools to manage the obj …
as example :

L = []

now L is a list , so , there varius tools as : L:append() , L.pop() etc

these things here done by class i guess. (right?)
so i want have the same thing to manage my game since it seem a lot cool.

the problem is i not sure how is doable ,and even when worth make it and when not worth.(maybe there cases where not worth ?)

this is correct ? or you see something of wrong ?
(note that not work in BGE , just in the consolle )


class Player:  
    def __init__(self) :
        self.life = 1.0
        self.curWeapon = ["magnum", 10] 
        class Weapon:
            def __init__(self, owner):
                self.owner = owner
            def fire(self):
                own = self.owner
                wp = own.curWeapon
                if wp[1] > 0: 
                    wp[1] -= 1
                    print("boom")
                else:
                    print("click")
                print("ammo=", wp[1])
        self.weapon = Weapon(self)
        
ob = Player()
ob.weapon.fire()
ob.weapon.fire()
ob.weapon.fire()

You use tools because they look cool?
Better use the right tools.

Lucky you the bge is already oop ;).
You are in the comfortable position to look at it as a user and you do not need to reinvent the wheel.

Nevertheless you can use OOP methods to implement your internal game model.

of course , BGE is already OOP a lot.

just i want it also in my script , to make it more easy (??)

in the script above lacks the class Magnum , i guess :eyebrowlift2:

but it should run on the magnum-gun gameObject maybe … mhh… (is this point where i’m lost a bit)

You should follow a design pattern that suits the need. OO is one form of programming style that is suited to certain applications. Be sure to use style convention regardless of what it is you’re implementing.
For example, don’t mix CamelCase attribute names and names_with_underscores. Leave spaces between functions and class declarations. Use well named attributes. For example, curWeapon would be better named “current_weapon”. You don’t save yourself any time or execution speed in using shorter attribute names.

i cannot put empty line using the consolle , otherwise give error.
you think not worth making class for player weapons?

this should be more clean , but not work :


class Weapon:
    class Magnum:
        def __init__(self):
            self.bullets = 3
            self.maxBullets = 5
            self.soundFire = "boom"
            self.soundFail = "click"
        def fire():
            if self.bullets > 0:
                self.bullets -= 1
                print(self.soundFire)
            else :
                print(self.soundFail)
        
        
        
class Player :
    def __init__(self):
        self.ammo = 15
        self.currentWeapon = None
    def getWeapon(self, obj):
        if isinstance(obj, Weapon):
            self.currentWeapon = obj
    def attack(self):
        if self.currentWeapon :
            self.currentWeapon.fire()
        else :
            print("floff")
            
            
player = Player()
magnum = Weapon().Magnum()




player.attack()
player.attack()
player.getWeapon(magnum)
player.attack()

ahah , cool cool, making it a bit more easy work


class Magnum :
    def __init__(self):
        self.bullets = 3
        self.maxBullets = 5
        self.soundFire = "boom"
        self.soundFail = "click"
    def fire(self):
        if self.bullets > 0:
            self.bullets -= 1
            print(self.soundFire)
        else :
            print(self.soundFail)
        
        
        
class Player :
    def __init__(self):
        self.ammo = 15
        self.currentWeapon = None
    def getWeapon(self, obj):
        print(obj)
        if isinstance(obj, Magnum):
            print("is a magnum")
            self.currentWeapon = obj
        else :
            print("is not a magnum")
    def attack(self):
        if self.currentWeapon :
            self.currentWeapon.fire()
        else :
            print("floff")
            
            
player = Player()
magnum = Magnum()




player.attack()
player.attack()
player.getWeapon(magnum)
player.attack()



how make the magnum as a instance (or something) of Weapon ? (if have sense…)

ok, more or less , the part more hard is done…:wink: , now still the problem of the recharge


class Weapon:
    def fire(self):
        if self.bullets > 0 :
            self.bullets -= 1
            print(self.soundFire)
        else :
            print(self.soundFail)
            
class Magnum(Weapon) :
    def __init__(self):
        self.owner = None
        self.bullets = 4
        self.soundFire = "boom"
        self.soundFail = "click"
        
class Rifle(Weapon) :
    def __init__(self):
        self.owner = None
        self.bullets = 2
        self.soundFire = "booooom"
        self.soundFail = "tic"
        
class Player :
    def __init__(self):
        self.ammoRifle = 15
        self.ammoMagnum = 10
        self.currentWeapon = None
    def getWeapon(self, obj):
        if isinstance(obj, Magnum) or isinstance(obj, Rifle):
            self.currentWeapon = obj
            obj.owner = self
        else :
            print("is not a weapon")
    def attack(self):
        if self.currentWeapon :
            self.currentWeapon.fire()
        else :
            print("floff")
        
magnum = Magnum()
rifle  = Rifle()
player = Player()


player.attack()
player.getWeapon(magnum)
player.attack()
player.attack()
player.attack()
player.attack()
player.attack()
player.getWeapon(rifle)
player.attack()

MarcoIT, please stay on topic. Just now this thread turns out to be a WIP.

in the sense that i’m go far to the OOP in few line of code ? :wink:

now have to port it in a “game” , i see already some problem something is good but… i not sure if player.attack() >> that become magnum.fire() , is very OOP…

I’m glad he posted this because it is really helping me make sense of classes. Before this I had no idea other than the notion that classes were somehow a template for something. How or why to use them was beyond me. In just a few minutes of playing with the code I’ve added a reload class. I know it’s not amazing but it has opened my mind a bit. Thank you MarcoIT.

You should write a method that handles the player attacking. saying player.attack() over and over is basically ruining the point of the OOP. :stuck_out_tongue:
ex;

if mouse_click_left.positive:
    if player.ammo > 0:
        player.attack()
    else:
        player.reload()

Lol lol… I remember when macroIT used to program using functions only but now he’s gone crazy with the classes! That’s cool man but as it has been said before; “blender is already object oriented” e.g you can use properties and actuators for data, like your ammo can be a “int” property and your riffle sound can be the sound actuator and you python script can then monitor input and produce the desired output you’ll have to have a property actuator set to “Add -1”. See your object already exists as a combination of all you might need