Python:口袋妖怪战斗(类,功能) [英] Python: Pokemon battle (classes, functions)

查看:625
本文介绍了Python:口袋妖怪战斗(类,功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习python,我希望你们能帮助我理解一些更好的东西。如果你曾经为玩家玩过一款口袋妖怪游戏,那么你会更明白我正在尝试做什么。我从文本冒险开始,在那里你做简单的事情,但现在我正处在与对方进行对抗的小精灵的地步。所以这就是我想要实现的。
$ b


  • 口袋妖怪战斗开始

  • 您攻击目标

  • 目标失去惠普并且遭到攻击返回

  • 第一个到0 hp损失



当然,所有这是打印出来。



这是我迄今为止的战斗,我不知道我现在有多准确。

  class宠物小精灵(物品):
sName = pidgy
nAttack = 5
nHealth = 10
nEvasion = 1


def __init __(self,name,atk,hp,evd):
self.sName = name
self.nAttack = atk
self.nHealth = hp
self.nEvasion = evd


def fight (目标,自我):
target.nHealth - self.nAttack



def battle():
print出现狂野
pre $ b pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ pikachu.fight(pidgy)

完整代码: http://pastebin.com/ikmRuE5z



我也在寻找如何管理变量的建议;我似乎在顶部有一个变量的购物清单,我认为这不是好的做法,他们应该去哪里? 解决方案

如果我将 fight 作为一个实例方法(我不确定我会如何),那么我可能会将它编码为这样:


$ b $

  class Pokemon(object):
def __init __(self,name,hp,damage):
self.name =名称#pokemon名称
self.hp = hp#这个特定口袋妖怪的命中点数
self.damage = damage#这个口袋妖怪每次攻击造成的伤害额度

def fight(自我,其他):
if(self.hp> 0):
print(%s did%d%s%s%(self.name,self.damage,other.name) )
print(%s has%d hp left%(other.name,other.hp))

other.hp - = self.damage
return other。战斗(自己)#现在另一个宠物小精灵反击!
else:
print(%s wins!(%d hp left)%(other.name,other.hp))
return other,self#return tuple(winner,失败者)

pikachu =宠物小精灵('pikachu',100,10)
pidgy =宠物小精灵('pidgy',200,12)
赢家,失败者= pidgy.fight(皮卡丘)

当然,这有点无聊,因为伤害的数量并不取决于小精灵的类型并没有以任何方式随机化......但希望它能说明这一点。



至于你的类结构:

  class Foo(object):
attr1 = 1
attr2 = 2
def __init __(self,attr1,attr2):
self.attr1 = attr1
self.attr2 = attr2

它并不真正使感觉(对我来说)声明类属性,如果你保证覆盖它们在 __ init __ 中。只要使用实例属性,你应该罚款(即):

pre $ class Foo(object):
def __init __( self,attr1,attr2):
self.attr1 = attr1
self.attr2 = attr2v


I just started learning python and I am hoping you guys can help me comprehend things a little better. If you have ever played a pokemon game for the gameboy you'll understand more as to what I am trying to do. I started off with a text adventure where you do simple stuff, but now I am at the point of pokemon battling eachother. So this is what I am trying to achieve.

  • Pokemon battle starts
  • You attack target
  • Target loses HP and attacks back
  • First one to 0 hp loses

Of course all of this is printed out.

This is what I have for the battle so far, I am not sure how accurate I am right now. Just really looking to see how close I am to doing this correctly.

class Pokemon(object):  
    sName = "pidgy"
    nAttack = 5
    nHealth = 10
    nEvasion = 1


    def __init__(self, name, atk, hp, evd):
        self.sName = name
        self.nAttack = atk
        self.nHealth = hp
        self.nEvasion = evd


    def fight(target, self):
        target.nHealth - self.nAttack



def battle():
    print "A wild  appeared" 
    #pikachu = Pokemon("Pikafaggot", 18, 80, 21)
    pidgy = Pokemon("Pidgy", 18, 80, 21)
    pidgy.fight(pikachu)
    #pikachu.fight(pidgy)   

Full code here: http://pastebin.com/ikmRuE5z

I am also looking for advice on how to manage variables; I seem to be having a grocery list of variables at the top and I assume that is not good practice, where should they go?

解决方案

If I was to have fight as a instance method (which I'm not sure I would), I would probably code it up something like this:

class Pokemon(object):
    def __init__(self,name,hp,damage):
        self.name = name     #pokemon name
        self.hp = hp         #hit-points of this particular pokemon
        self.damage = damage #amount of damage this pokemon does every attack

    def fight(self,other):
        if(self.hp > 0):
            print("%s did %d damage to %s"%(self.name,self.damage,other.name))
            print("%s has %d hp left"%(other.name,other.hp))

            other.hp -= self.damage
            return other.fight(self)  #Now the other pokemon fights back!
        else:
            print("%s wins! (%d hp left)"%(other.name,other.hp))
            return other,self  #return a tuple (winner,loser)

pikachu=Pokemon('pikachu', 100, 10)
pidgy=Pokemon('pidgy', 200, 12)
winner,loser = pidgy.fight(pikachu)

Of course, this is somewhat boring since the amount of damage does not depend on type of pokemon and isn't randomized in any way ... but hopefully it illustrates the point.

As for your class structure:

class Foo(object):
    attr1=1
    attr2=2
    def __init__(self,attr1,attr2):
        self.attr1 = attr1
        self.attr2 = attr2

It doesn't really make sense (to me) to declare the class attributes if you're guaranteed to overwrite them in __init__. Just use instance attributes and you should be fine (i.e.):

class Foo(object):
    def __init__(self,attr1,attr2):
        self.attr1 = attr1
        self.attr2 = attr2v

这篇关于Python:口袋妖怪战斗(类,功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆