基于文本的口袋妖怪游戏:如何实现进化系统?

时间:2017-11-28 12:53:11

标签: python

我已经开始研究这个基于文本的口袋妖怪游戏了,我一直试图想出一个可以将一个类对象改为另一个类对象的进化系统,如果这是有道理的。

我试图通过简单地使类对象等于我想要更改的对象来尝试这样做:

# Trainer
new_trainer = Trainer('Trainer', 'Male')

# Test pokemon (the parameter is just a level value)
bulbasaur = Bulbasaur(10)
ivysaur = Ivysaur(20)

# Adding pokemon to party
new_trainer.add_pokemon(bulbasaur)
new_trainer.add_pokemon(ivysaur)

# Display pokemon lists
print(new_trainer.pokemon_list)
new_trainer.display_party()

# Attempting to 'evolve' bulbasuar
bulbasaur = Ivysaur(bulbasaur.lvl)

# Displaying results again to see if it worked
print(new_trainer.pokemon_list)
new_trainer.display_party()

但是通过查看输出,它表明没有发生任何事情:

Bulbasaur was added to your party!
Ivysaur was added to your party!

[<__main__.Bulbasaur object at 0x0000023587EDA080>, <__main__.Ivysaur object at 0x0000023587EDA0F0>]

1. Bulbasaur
2. Ivysaur

[<__main__.Bulbasaur object at 0x0000023587EDA080>, <__main__.Ivysaur object at 0x0000023587EDA0F0>]

1. Bulbasaur
2. Ivysaur

编辑: 它变成了变量值确实会改变,但我只需要更新训练师的小精灵列表。但是,我仍然不确定如何做到这一点。

Trainer类中的display_party函数如下所示:

def display_party(self):  # Displays pokemon party list
        if not self.pokemon_list:
            return print("There are no pokemon in your party.")
        else:
            for pokemon in range(len(self.pokemon_list)):
                print("{0}. {1}".format(pokemon + 1, self.pokemon_list[pokemon].name))

我只想添加一个update_party函数,该函数进入列表,删除旧值并插入新值,但它将如何知道要删除的值?

也许有更好的方法可以做到这一点?抱歉,如果我的问题没有包含那么多信息,我会添加更多代码,如果它已经没有多大意义的话。这是我第一次在这里问一个问题:D

编辑2: 对于那些不了解代码如何工作的人来说,这基本上是它到目前为止的工作方式:

# Formula used to calculate pokemon health
def health_stat_calculation(base_stat, lvl):
    return int(((base_stat * 3 * lvl) / 100) + lvl + 10)

# Formula used to calculate pokemon stats
def stat_calculation(base_stat, lvl):
    return int(((base_stat * 3 * lvl) / 100) + 5)


# Trainer class
class Trainer:
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender
        self.pokemon_list = []
        self.item_list = []


    def add_badge(self, badge):  # Gives player a badge
        self.player_badges.append(badge)


    def add_pokemon(self, pokemon):  # Adds a pokemon to party

        for poke in self.pokemon_list:
            if pokemon is poke:
                return print("You already have this pokemon in your party!")

        if len(self.pokemon_list) > 5:
            return print("You have too much pokemon in your party!")
        else:
            self.pokemon_list.append(pokemon)
            return print("{} was added to your party!".format(pokemon.name))


    def display_party(self):  # Displays pokemon party list
            if not self.pokemon_list:
                return print("There are no pokemon in your party.")
            else:
                for pokemon in range(len(self.pokemon_list)):
                    print("{0}. {1}".format(pokemon + 1, self.pokemon_list[pokemon].name))


    def add_item(self, item):  # Adds an item to player's inventory
        if len(self.item_list) > 50:
            print("You have too much items in your inventory!")
        else:
            self.item_list.append(item)
            print("{} was added to your inventory!")


# Generic pokemon class
class Pokemon:
    def __init__(self, name, lvl, exp, id_number, health, attack, defence, sp_attack, sp_defense, speed):
        self.name = name
        self.lvl = lvl
        self.exp = exp
        self.id_number = id_number
        self.health = health
        self.attack = attack
        self.defence = defence
        self.sp_attack = sp_attack
        self.sp_defense = sp_defense
        self.speed = speed


    def get_pokemon_name(self):
            return self.name


    # User friendly stat display
    def display_stats(self):
        print("\nStats for {}\n---------------".format(self.name))
        print("Level: {}".format(self.lvl))
        print("Index No.{}".format(self.id_number))
        print("Health: {}".format(self.health))
        print("Attack: {}".format(self.attack))
        print("Defense: {}".format(self.defense))
        print("Sp. Attack: {}".format(self.sp_attack))
        print("Sp. Defense: {}".format(self.sp_defense))
        print("Speed: {}\n".format(self.speed))


    # Level up pokemon and show change in stats
    def level_up(self):
        self.display_stats()
        self.lvl += 1
        print("\n{} has leveled up!\n".format(self.name))
        self.calculate_stats()
        self.display_stats()


    # Calculate generic pokemon stats
    def calculate_stats(self):
        self.health = health_stat_calculation(self.base_health, self.lvl)
        self.attack = stat_calculation(self.base_attack, self.lvl)
        self.defense = stat_calculation(self.base_defense, self.lvl)
        self.sp_attack = stat_calculation(self.base_sp_attack, self.lvl)
        self.sp_defense = stat_calculation(self.base_sp_defense, self.lvl)
        self.speed = stat_calculation(self.base_speed, self.lvl)


# Bulbasaur class
class Bulbasaur(Pokemon):
    def __init__(self, lvl, name="Bulbasaur"):
        self.name = name
        self.lvl = 16
        self.exp = 0
        self.id_number = 1

        # Bulbasaur Base stats
        self.base_health = 45 
        self.base_attack = 49
        self.base_defense = 49
        self.base_sp_attack = 65
        self.base_sp_defense = 65
        self.base_speed = 45

        self.calculate_stats()


# Ivysaur class
class Ivysaur(Pokemon):
    def __init__(self, lvl, name="Ivysaur"):
        self.name = name
        self.lvl = 16
        self.exp = 0
        self.id_number = 2

        # Bulbasaur Base stats
        self.base_health = 60
        self.base_attack = 62
        self.base_defense = 63
        self.base_sp_attack = 80
        self.base_sp_defense = 80
        self.base_speed = 60

        self.calculate_stats()

3 个答案:

答案 0 :(得分:0)

从一个类转换到另一个类在你的情况下毫无意义。

请继续

@Transaction

答案 1 :(得分:0)

您可以将种类$ clang++ matmul.cpp -std=c++17 -Ofast -march=native -Wall -Wextra $ ./a.out A 1.0367049464391398e-01 7.4917987082978588e-03 -2.7966084757805687e-01 -7.2325095373639048e-01 2.2478938813996119e-01 8.4194832286446353e-01 5.3602376615184033e-01 7.1132727553003439e-01 1.9608747339865196e-01 -6.4829263198209253e-01 -2.7477471919710350e-01 1.2721104074473044e-01 B -8.5938605801284385e-01 -6.2981285198013204e-01 -6.0333085647033191e-01 -6.8234173530317577e-01 -1.2614486249714407e-01 -3.3875904433100934e-01 -6.9618174970366520e-01 6.6785401241316045e-01 A * B 4.4517888255515814e-01 -4.5869338680118737e-01 -1.2639839804611623e+00 -4.2259184895688506e-01 1.6871952235091500e-01 4.9689953389829533e-01 Bulbasaur等转换为Ivysaur类的组件。 Pokemon将其下一个进化阶段作为属性,在口袋妖怪的Bulbasaur方法中,您可以将evolve设置为下一个进化阶段。

kind

答案 2 :(得分:0)

我不知道这是否是最干净的方式,但是当我将此函数添加到Pokemon类并使用dict dunder将旧对象的内容更新为新的时,它会以某种方式起作用一。 temp_pos被用作当前对象名称的临时占位符,因此在将其更新为new_pokemon对象时不会更改。

# Evolve function
def evolve(self, evln, trainer):
    temp_name = self.name
    new_pokemon = evln(self.lvl)
    self.__dict__.update(new_pokemon.__dict__)
    self.name = temp_name