在函数之间传递对象?

时间:2014-08-08 01:56:56

标签: python class python-3.x arguments transfer

我是python编程的新手,我开始编写一个简单的基于文本的冒险游戏。将对象从一个函数传递到另一个函数时遇到了一个问题。这是我的代码:

import random
import time
num = random.randint(0,2)
xp1 = random.randint(1,2)
class player:
    def __init__ (self, name, health, strength, defense, potion, xp, level):
        self.__health = health
        self.__strength = strength
        self.__defense = defense
        self.__name = name
        self.__potion = potion
        self.__xp = xp
        self.__level = level
        def getName(self):
            return self.__name

        def getHealth(self):
            return self.__health

        def getStrength(self):
            return self.__strength

        def getDefense(self):
            return self.__defense

        def getPotion(self):
            return self.__potion

        def getXP(self):
            return self.__xp

        def getLevel(self):
            return self.__level

        def setHealth(self):
            self.__health = 10

        def setLevel(self):
            self.__level = 1

        def subHealth(self, num):
            self.__health -= num

        def subPotion(self):
            self.__potion -= 1
            return self.__health

        def addPotion(self, num1):
            self.__potion += num1

        def addHealth(self):
            self.__health +=2

        def addStrength(self):
            self.__strength += 1

        def addDefense(self):
            self.__defense += 1

        def addXP(self):
            self.__xp += xp1

        def addLevel(self):
            self.__level += 1
            self.__addHealth += 1
            self.__defense += 1
            self.__strength += 1

    def battle(enemy, player1, name1):
        player1 = player(name1, player1.getHealth(), player1.getStrength(), player1.getDefense(), player1.getPotion(), player1.getXP(), player1.getLevel())
        enemy = player('Dongus', enemy.getHealth(), enemy.getStrength(), enemy.getDefense(), enemy.getPotion(), enemy.getXP(), enemy.getLevel())
        s = 0
        while s == 0:
            time.sleep(1)
            attack =int(input("Type 1 to attack, type 2 to use a potion."))
            if attack == 1:
                time.sleep(1)
                print("Dongus's health is", enemy.subHealth(num))
                print("Dongus hit you and your health is now at", player1.subHealth(num-player1.getDefense()))
            elif attack == 2:
                time.sleep(1)
                print("You used a potion.")
                player1.addHealth(), player1.subPotion()
                if player1.getHealth() > 10:
                    player1.setHealth()
                    print("Dongus hit you and your health is now at", player1.subHealth(num-player1.getDefense()))
            if enemy.getHealth()<=0:
                print("Congratulations, you won! You recieved", xp1, "xp!")
                player.addXP()
                s = 2
    def main():
        name1 = input("What would you like your name to be?")
        time.sleep(1)
        print("Hello,", name1, "you are on a quest to save otis from the evil Dongus. You must slay him, or Otis will poop.")
        time.sleep(2)
        player1 = player(name1, 10, 2, 1, 0, 0, 1)
        enemy = player('Dongus', 8, 4, 0, 0, 0, 0)
        print("Your stats are, health:", player1.getHealth(), "strength:", player1.getStrength(), "and defense:", player1.getDefense())
        time.sleep(2)
        print("Fight!")
        pick = input("You found a health potion! Press 'p' to pick it up.")
        p = 0
        while p == 0:
            if pick == "p":
                print("You added a potion to your inventory.")
                player1.addPotion(1)
                p = 2
            else:
                print("You have no potions, you should probably pick this one up.")
                player1.addPotion(1)
                p = 2
        battle(enemy, player1, name1)
        if self.__getXP() == 1:
            print("You leveled up. You are now level 2.")
            player1.addLevel()
            print("Your stats are, health:", player1.getHealth(), "strength:", player1.getStrength(), "and defense:", player.getDefense())       
        loot1 = int(input("Type ''1'' to loot the enemy chest."))
        if loot1 == 1:
            print("You recieved two potions!")
            player1.__addPotion(2)
        enemy.setHealth(10)
        battle(enemy, player1, name1)

main()

现在的问题是,当我运行游戏时,我到达了一个点“1”来攻击敌人的点,但是由于某种原因它说,在攻击敌人之后,敌人的健康状况是“无” ”。这是敌人攻击球员1的情况,它说球员1的生命值是“无”。我假设“无”是python 3.4.1中的默认值,所以我的想法是来自def main()的player1的对象没有转移到def battle(),我看不出发生这种情况的原因。我很可能在这里遗漏了一些东西,或者我不知道Python导致了这个问题。有人知道我能做些什么来解决这个问题,为什么会这样做呢?

BTW我使用的一些术语可能有误,所以请纠正我,如果它们是......我只编写了2周的代码:p。

感谢!!!

1 个答案:

答案 0 :(得分:-1)

首先,收到没收到的

第二个是,如果您的Python函数没有返回值,则结果为None

# dummy will return "Positive" or None
def dummy(x):
    if X > 0:
        return "Positive"

所以,你可能想要改变

def subHealth(self, num):
        self.__health -= num

def subHealth(self, num):
        self.__health -= num
        return self.__health

你的问题是:&#34;播放器&#34;来自def main()的类没有转移到def battle()对我来说没有意义。

但是,我看到在前2行战斗中,你正在用局部变量替换正式参数player1和敌人,这在你的程序中似乎很奇怪。