Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的口袋妖怪之战_Python_Project - Fatal编程技术网

Python中的口袋妖怪之战

Python中的口袋妖怪之战,python,project,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 = xh

我不太擅长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.')

您可以立即使用字符串格式在字符串中插入变量

#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 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.
这涵盖了对象的定义,但它仍然没有做任何事情。事实上,让它做点什么,好吗?我们想让它攻击。什么是不战斗的口袋妖怪?让我们给它一个函数(在类中,我们称函数为“方法”)

现在你需要做些东西。你定义了什么是口袋妖怪,它能做什么,但你还没有做一个。让我们做一些。幸运的是这很容易

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\u my\u 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))

这有意义吗?

我真的认为你应该重新学习OOP,然后再回到这个问题上来,因为这绝对是一个值得练习的问题

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

    self.damage = xdamage
    self.damage = random.randint(1,50)
这个函数保持打开状态,这将导致编译问题,除了丢失任何实际数据之外

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

你需要调用你的伤害变量和Charmander的伤害变量;想想在OOP中是如何实现的。

考虑到项目的类型,OOP在这方面不是有点过分吗?@cloudcoder2000:这是一个家庭作业问题,旨在教授OOP。我认为这对初学者来说是一个很棒的项目,但当然,它发布在这里的事实有点麻烦…这是一个很好的代码审查候选。如果你真的打算练习OOP,你会希望从使程序更模块化开始。根据需要将独立运行的代码分成不同的类或函数。如果操作正确,你的游戏不仅可以使用口袋妖怪,还可以使用你能想到的任何生物。我认为他留下了最后一部分,以显示他希望帮助显示对用户的伤害的位置。我正试图解决我的答案中更大的OOP问题。啊,是的,我自己刚刚了解到。我真的很喜欢这个问题的想法来教授OOP,但是很难决定在给出答案之间的界限(显然对学生学习如何在OOP中思考非常有害)给他们足够的钱来帮助他们培养面向对象的思维,我是否应该消除面向对象的伤害,将其设置为一个变量,然后将其随机设置?@Javier,我的回答可能会稍微阐明这些观点。OOP的目标是创建可扩展类,这些类可以处理您向它们抛出的任何东西,而不仅仅是Pokemon。我没有解释那么多,因为我不想一下子对你扔太多东西,但请记住类可以扩展其他类(也许
classcombatant:
可以有一些这样的东西,
classpokemon(Combatant)
具有口袋妖怪特有的功能stuff@adsmith:直接访问另一个对象的属性通常被认为是一种不好的做法(最好使用getter/setter方法,这样即使内部情况发生变化,也可以使用一致的API)。无论哪种方法都是有效的方法,但我认为你最好使用两种更通用的方法,而不是一种包容的方法!我的目标是让每次口袋妖怪攻击都持续到0点生命值。但是,我的老师让我们使用OOP,我还不完全理解,所以我在尝试使用它时遇到了困难。哦,另外,我希望每次攻击都随机进行rom 1-50 damage.OOP只是让一些对象有相似的父类。
类和
类可能都共享“父类”
动物
类。非常感谢!好吧,我现在明白了。让我非常困惑的是攻击部分,但你澄清了它。谢谢!@adsmith我确实有一个问题。什么返回函数能做什么?我从来没被教过
print ('You dealt '
sleep(1)
print ('Charmander did ')