口袋妖怪用Python进行战斗

时间:2013-12-10 22:03:17

标签: python project

我对OOP并不是那么好,但我现在被困住了。首先,我认为我没有正确设置损坏,我试图弄清楚如何输出损害给用户。任何帮助将不胜感激。

#Pokemon Battle
import random
from time import sleep
from array import *

class Pokemon(object):
    def __init__(self, xname, xhealth):
        self.name = xname
        self.health = xhealth

    def damage1(self,Charmander):
        Squirtle.health - self.damage
    def damage2(self,Charmander):
        self.health - Squirtle.damage

print ('What is your name?')
name = input()
print ('Hello '+name+'! You are about to enter a pokemon battle.')
sleep(1)
print ('A wild Charmander appeared!')
sleep(1)
print ('You sent out Squirtle!')
sleep(1)
print ('Do you want to fight(1) or run away(2)?')
choice = input()

damage = random.randint(1,50)
damage = str(damage)

if choice == '1':
    print ('You dealt '
    sleep(1)
    print ('Charmander did ')
if choice == '2':
    print ('You ran away.')
else:
    print ('Not a valid response.')

2 个答案:

答案 0 :(得分:2)

马上就可以使用String Formatting在字符串中插入变量。

#old way
some_string = "the value of 2+2 = %i",4

#new way
some_string = "the value of 2+2 = {}".format(4)

对于您的代码,请尝试:

if choice == '1':
  print("You dealt {}".format(damage_goes_here))

然而,您的代码存在更深层次的问题。让我看一下,我会编辑。

面向对象编程

好的,所以你遇到的第一个问题是你从未真正做过任何事情。当你写class SomeClassLikePokemonOrWhatever:时,你正在做的是做一些事情的模板。它就像在制作之前制作一个项目的模型或模具 - 你想要所有的宠物小精灵(或其他)都是相似的,所以你制作它们的模具并将它们全部从同一个模具中铸造出来。你可以在那之后装饰和独特 - 但是我们希望它们都是相同的,基本上。所以相反,你应该有这样的东西:

class Pokemon:
  def __init__(self,name,base_hp):
    self.name = name
    self.base_hp = base_hp
  #the __init__ function gets called when you "instantiate" (e.g. actually MAKE)
  #whatever object the class is describing. In most cases, all it does it set
  #the starting properties of the object based on how you define it (like this)
  #you could also say all pokemon are beautiful, and add something like
    self.description = "Absolutely GORGEOUS darling!"
  #that will be constant for every pokemon you make through this definition.
  #you said you wanted damage to be random between 1-50, so we don't need to add
  #that statistic to the class.

这涵盖了对象的定义,但它仍然没有做任何事情。事实上,让我们做点什么吧,好吗?我们希望它能够攻击。什么是不会打架的口袋妖怪?所以让我们给它一个函数(在一个类中,我们称函数"方法。")

  def attack(self,target):
  #in this method we'll teach the pokemon how to fight
    damage = random.randint(1,50) #don't forget to import random to do this
    target.hp -= damage

现在你需要做一些事情。你定义了一个口袋妖怪是什么以及它可以做什么,但你还没有做到。让我们做点儿吧。幸运的是,这很容易。

my_awesome_pokemon = Pokemon("Charizard",200) #you give the args in the same order __init__ takes them
your_sucky_pokemon = Pokemon("Magikarp",20) #same deal here.

这会产生两个口袋妖怪,一个给你,一个给你。如果你想要一个完整的皮带,你可以定义一个数组all_my_pokemon并用这种方式定义的Pokemon对象填充它。只是想一想。

要真正打架,你要告诉你的口袋妖怪攻击。

my_awesome_pokemon.attack(your_sucky_pokemon)
#just that easy, now display how much damage it did....WAIT STOP WE HAVE A PROBLEM!

因为您每次都想要随机损坏,所以您无法使用类似my_awesome_pokemon.damage的内容来访问它,因为它是一个本地变量,它会在攻击方法结束时死掉。但是,您可以在方法中返回该值并使用它....让我们改变我们的方法。

def attack(self,target):
  damage = random.randint(1,50)
  target.hp -= damage
  return damage #now we have some way to access how much damage was done from our main thread

现在要显示它,我们可以做到

damage_done = my_awesome_pokemon.attack(your_sucky_pokemon) #since .attack() returns the damage it deals, this sets damage_done to a sane amount.
print("My pokemon {} dealt {} damage to {}".format(my_awesome_pokemon.name,damage_done,your_sucky_pokemon.name))

这有意义吗?

答案 1 :(得分:1)

我真的认为你应该提高你的OOP,然后再回到这个问题,因为这绝对是一个体面的问题。

首先,你设置了伤害,然后再次随机设置:

    self.damage = xdamage
    self.damage = random.randint(1,50)

此功能保持打开状态,除了因为您缺少任何实际数据外,还会导致编译问题!

print ('You dealt '
sleep(1)
print ('Charmander did ')

你想要调用你的伤害变量和Charmander的伤害变量;想一想如何在OOP中完成。