IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

how to create weapon


Sujet :

Python

  1. #1
    Membre averti
    Homme Profil pro
    ETUDIANT
    Inscrit en
    Septembre 2017
    Messages
    17
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : ETUDIANT
    Secteur : High Tech - Multim�dia et Internet

    Informations forums :
    Inscription : Septembre 2017
    Messages : 17
    Par d�faut how to create weapon
    hi, i have to create a " weapon " with damage, dps, range etc.. and this would change if the weapon got gem(s)


    here is my code:

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    class Weapon(object):
        def __init__(self, speed, dmg_min, dmg_max, wrange):
            return
     
     
     
    class Sword(Weapon):
        def __init__(self, name='sword', speed=1, dmg_min=1, dmg_max=1, wrange=1, gems=[], gems_nb=0):
                super(Sword, self).__init__(speed, dmg_min, dmg_max, wrange)
                self.gems = gems
                self.gems_nb = gems_nb
                self.name = name
     
     
     
        def infos(self):
            print "Name = {}".format(self.name)
            print "Damage range = {}".format()
            print "Dps = {}".format()
            print "Nb gems = {}".format()
            print "Golden gem: {}".format(1)
     
     
    class Gem(object):
        def __init__(self):
            return
     
    excalibur = Sword('Excalibur', 10, 50, 120, 4)
    gold_gem = Gem("Golden gem", 100)
    excalibur.add_gem(gold_gem)
     
    excalibur.infos()
    the output should be :

    Name = Excalibur
    Dammage range = 50-120
    Dps = 1850
    Nb gems = 1
    Golden gem:
       +100 dmg, +0 speed, +0 range.

    Thanks for help

  2. #2
    Membre exp�riment� Avatar de cervo
    Homme Profil pro
    D�veloppeur informatique
    Inscrit en
    D�cembre 2012
    Messages
    220
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : C�te d'Ivoire

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : D�cembre 2012
    Messages : 220
    Par d�faut

    Hi buddy ! It would be great ifyour post was in french, many other could help. for now i dont understand your problem... because all you want is on your code

  3. #3
    Membre averti
    Homme Profil pro
    ETUDIANT
    Inscrit en
    Septembre 2017
    Messages
    17
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : ETUDIANT
    Secteur : High Tech - Multim�dia et Internet

    Informations forums :
    Inscription : Septembre 2017
    Messages : 17
    Par d�faut
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    class Weapon(object):
        def __init__(self, name, speed, dmg_min, dmg_max, wrange):
            self.name = name
            self.speed = speed
            self.dmg_min = dmg_min
            self.dmg_max = dmg_max
            self.wrange = wrange
     
     
        def infos(self):
            print "Name = {}".format(self.name)
            print "Damage range = {}".format(self.wrange)
            print "Dps = {}".format((self.dmg_min + self.dmg_max) / 2 * self.speed)
            print "Nb gems = {}".format(self.gems_nb)
            print "Golden gem: {}".format(0)
     
     
    class Sword(Weapon):
        def __init__(self, name='sword', speed=None, dmg_min=None, dmg_max=None, wrange=None, gems=[], gems_nb=1):
                super(Sword, self).__init__(name, speed, dmg_min, dmg_max, wrange)
                self.gems = gems
                self.gems_nb = gems_nb
     
     
    class Gem(object):
        def __init__(self, name, gems):
            return
     
     
     
    excalibur = Sword('Excalibur', 10, 50, 120, "50-120")
    gold_gem = Gem("Golden gem", 100)
    excalibur.infos()
    # excalibur.add_gem(gold_gem)
    here is my new code
    it works well but my " excalibur " has one gem so dmg_min and dmg_max should be 150 / 220 instead of 50/120 ( one gem = +100 damage)
    any idea how to set that ?

  4. #4
    Membre prolifique
    Avatar de Sve@r
    Homme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    F�vrier 2006
    Messages
    12 848
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels
    Secteur : A�ronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : F�vrier 2006
    Messages : 12 848
    Billets dans le blog
    1
    Par d�faut
    Citation Envoy� par hayee Voir le message
    it works well but my " excalibur " has one gem so dmg_min and dmg_max should be 150 / 220 instead of 50/120 ( one gem = +100 damage)
    any idea how to set that ?
    Write a methode "chance_gem" in Weapon class
    Code python : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Weapon(object):
        def __init__(self, name, speed, dmg_min, dmg_max, wrange):
            self.name = name
            self.speed = speed
            self.dmg_min = dmg_min
            self.dmg_max = dmg_max
            self.wrange = wrange
     
     
        def infos(self):
            print "Name = {}".format(self.name)
            print "Damage range = {}".format(self.wrange)
            print "Dps = {}".format((self.dmg_min + self.dmg_max) / 2 * self.speed)
            print "Nb gems = {}".format(self.gems_nb)
            print "Golden gem: {}".format(0)
     
        def chance_gem(self, value):
            if random.choice(False, True): self.dmg_max+= value
     
     
    excalibur = Sword('Excalibur', 10, 50, 120, "50-120")
    excalibur.chance_gem(100)
    excalibur.infos()
    Mon Tutoriel sur la programmation �Python�
    Mon Tutoriel sur la programmation �Shell�
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les diff�rentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  5. #5
    Membre averti
    Homme Profil pro
    ETUDIANT
    Inscrit en
    Septembre 2017
    Messages
    17
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : ETUDIANT
    Secteur : High Tech - Multim�dia et Internet

    Informations forums :
    Inscription : Septembre 2017
    Messages : 17
    Par d�faut
    thanks for your answer!
    now i have this error:

    File "ex03.py", line 38, in <module>
    excalibur.chance_gem(100)
    File "ex03", line 18, in chance_gem
    if random.choice(False, True):
    NameError: global name 'random' is not defined

  6. #6
    Membre exp�riment� Avatar de cervo
    Homme Profil pro
    D�veloppeur informatique
    Inscrit en
    D�cembre 2012
    Messages
    220
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : C�te d'Ivoire

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : D�cembre 2012
    Messages : 220
    Par d�faut

    buddy, you have to import library (module ) before using it.

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    import random
     
    .... (post your code here)

  7. #7
    Membre averti
    Homme Profil pro
    ETUDIANT
    Inscrit en
    Septembre 2017
    Messages
    17
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : ETUDIANT
    Secteur : High Tech - Multim�dia et Internet

    Informations forums :
    Inscription : Septembre 2017
    Messages : 17
    Par d�faut
    hmm, thanks but still the same..

    Traceback (most recent call last):
    File "ex03.py", line 40, in <module>
    excalibur.change_gem(100)
    File "ex03", line 20, in chance_gem
    if random_choice(False, True):
    NameError: global name 'random_choice' is not defined

  8. #8
    Membre prolifique
    Avatar de Sve@r
    Homme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    F�vrier 2006
    Messages
    12 848
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels
    Secteur : A�ronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : F�vrier 2006
    Messages : 12 848
    Billets dans le blog
    1
    Par d�faut
    Citation Envoy� par hayee Voir le message
    File "ex03", line 20, in chance_gem
    if random_choice(False, True):
    NameError: global name 'random_choice' is not defined

    Code python : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
        def chance_gem(self, value):
            if random.choice(False, True): self.dmg_max+= value
    Mon Tutoriel sur la programmation �Python�
    Mon Tutoriel sur la programmation �Shell�
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les diff�rentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  9. #9
    Membre �clair�
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    795
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 795
    Par d�faut
    Hello all and sorry for my bad english
    I am not sure about actual solution


    in his fist message, hayee show us it Sword class

    Code python : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    class Sword(Weapon):
        def __init__(self, name='sword', speed=1, dmg_min=1, dmg_max=1, wrange=1, gems=[], gems_nb=0):
                super(Sword, self).__init__(speed, dmg_min, dmg_max, wrange)
                self.gems = gems
                self.gems_nb = gems_nb
                self.name = name

    As you can see, he puts an attribute gems and I suppose that it is a list of all gems, isn't it ?

    Thus, I think we must not change self.dmg_max value. For me, this value is "read-only" and defined with constructor
    Then, we must calculate Sword damage
    Sword damage = initial damage + sum("gems boost")


    I hope to be understandable

    let me know if I am totally wrong

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo