波奇不断与所选的神奇宝贝一起显示

问题描述 投票:0回答:1

波奇不断与选定的神奇宝贝一起显示。我生成了一个随机数,当它等于 random_pokemon_counter 时,它会显示神奇宝贝及其属性。但函数中的第一个神奇宝贝(Pidgey)总是与所选的神奇宝贝一起显示。

def random_pokemon_for_battle():
variables.random_pokemon_counter = random.randint(1, 6)
if variables.random_pokemon_counter == 1:
    variables.random_pokemon_encounter = "Pidgey"
    from sounds import pidgey_sound
    winsound.PlaySound(pidgey_sound, winsound.SND_FILENAME)
    variables.pidgey_level = random.randint(1, 10)
    variables.pidgey_hp = 100
    variables.pidgey_attack = random.randint(10, 25)
    variables.pidgey_defense = random.randint(15, 35)
    print("A wild Pidgey appeared!")
    print(f"Level: {variables.pidgey_level}")
    print(f"HP: {variables.pidgey_hp}")
    print(f"Attack: {variables.pidgey_attack}")
    print(f"Defense: {variables.pidgey_defense}")
    variables.random_pokemon_counter = 0

elif variables.random_pokemon_counter == 2:
    variables.random_pokemon_encounter = "Weedle"
    from sounds import weedle_sound
    winsound.PlaySound(weedle_sound, winsound.SND_FILENAME)
    variables.weedle_level = random.randint(1, 10)
    variables.weedle_hp = 100
    variables.weedle_attack = random.randint(10, 25)
    variables.weedle_defense = random.randint(15, 35)
    print("A wild Weedle appeared!")
    print("A wild Pidgey appeared!")
    print(f"Level: {variables.weedle_level}")
    print(f"HP: {variables.weedle_hp}")
    print(f"Attack: {variables.weedle_attack}")
    print(f"Defense: {variables.weedle_defense}")
    variables.random_pokemon_counter = 0

elif variables.random_pokemon_counter == 3:
    variables.random_pokemon_encounter = "Pikachu"
    from sounds import pikachu_sound
    winsound.PlaySound(pikachu_sound, winsound.SND_FILENAME)
    print("A wild Pikachu appeared!")
    variables.pikachu_level = random.randint(1, 10)
    variables.pikachu_hp = 100
    variables.pikachu_attack = random.randint(10, 25)
    variables.pikachu_defense = random.randint(15, 35)
    print("A wild Pidgey appeared!")
    print(f"Level: {variables.pikachu_level}")
    print(f"HP: {variables.pikachu_hp}")
    print(f"Attack: {variables.pikachu_attack}")
    print(f"Defense: {variables.pikachu_defense}")
    variables.random_pokemon_counter = 0

elif variables.random_pokemon_counter == 4:
    variables.random_pokemon_encounter = "Nidoran_M"
    from sounds import nidoran_male_sound
    winsound.PlaySound(nidoran_male_sound, winsound.SND_FILENAME)
    print("A wild Nidoran appeared!")
    variables.nidoran_M_level = random.randint(1, 10)
    variables.nidoran_M_hp = 100
    variables.nidoran_M_attack = random.randint(10, 25)
    variables.nidoran_M_defense = random.randint(15, 35)
    print("A wild Pidgey appeared!")
    print(f"Level: {variables.nidoran_M_level}")
    print(f"HP: {variables.nidoran_M_hp}")
    print(f"Attack: {variables.nidoran_M_attack}")
    print(f"Defense: {variables.nidoran_M_defense}")
    variables.random_pokemon_counter = 0

elif variables.random_pokemon_counter == 5:
    variables.random_pokemon_encounter = "Nidoran_F"
    from sounds import nidoran_female_sound
    winsound.PlaySound(nidoran_female_sound, winsound.SND_FILENAME)
    print("A wild Nidoran^ appeared!")
    variables.nidoran_F_level = random.randint(1, 10)
    variables.nidoran_F_hp = 100
    variables.nidoran_F_attack = random.randint(10, 25)
    variables.nidoran_F_defense = random.randint(15, 35)
    print("A wild Pidgey appeared!")
    print(f"Level: {variables.nidoran_F_level}")
    print(f"HP: {variables.nidoran_F_hp}")
    print(f"Attack: {variables.nidoran_F_attack}")
    print(f"Defense: {variables.nidoran_F_defense}")
    variables.random_pokemon_counter = 0

elif variables.random_pokemon_counter == 6:
    variables.random_pokemon_encounter = "Caterpie"
    from sounds import caterpie_sound
    winsound.PlaySound(caterpie_sound, winsound.SND_FILENAME)
    print("A wild Caterpie appeared!")
    variables.caterpie_level = random.randint(1, 10)
    variables.caterpie_hp = 100
    variables.caterpie_attack = random.randint(10, 25)
    variables.caterpie_defense = random.randint(15, 35)
    print("A wild Pidgey appeared!")
    print(f"Level: {variables.caterpie_level}")
    print(f"HP: {variables.caterpie_hp}")
    print(f"Attack: {variables.caterpie_attack}")
    print(f"Defense: {variables.caterpie_defense}")
    variables.random_pokemon_counter = 0

所以我基本上想做的事情看起来很简单。生成一个随机数,当它等于特定值时,它应该显示该神奇宝贝及其属性,而不是函数内的第一个神奇宝贝,即 pidgey。

python pycharm
1个回答
0
投票

这是一种以数据驱动的方式组织代码的更好方法。最后,您可能希望图鉴信息来自文件而不是嵌入代码中。

import random
import sounds

class Pokemon:
    def __init__(self,name,sound):
        self.name = name
        self.level = random.randint(1,10)
        self.hp = 100
        self.attack = random.randint(10,25)
        self.defense = random.randint(15,35)
        self.sound = sound

pokedex = [
    ( "Pidgey", sounds.pidgey_sound ),
    ( "Weedle", sounds.weedle_sound ),
    ( "Pikachu", sounds.pikachu_sound ),
    ( "Nidoran_M", sounds.nidoran_male_sound ),
    ( "Nidoran_F", sounds.nidoran_female_sound ),
    ( "Caterpie", sounds.caterpie_sound )
]


def random_pokemon_for_battle():
    poke = Pokemon( *random.choice( pokedex ) )
    winsound.PlaySound(poke.sound, winsound.SND_FILENAME)
    print(f"A wild {poke.name} appeared!")
    print(f"Level: {poke.level}")
    print(f"HP: {poke.hp}")
    print(f"Attack: {poke.attack}")
    print(f"Defense: {poke.defense}")
    return poke

random_pokemon_for_battle()
random_pokemon_for_battle()

输出:

A wild Pidgey appeared!
Level: 10
HP: 100
Attack: 10
Defense: 19
A wild Caterpie appeared!
Level: 9
HP: 100
Attack: 13
Defense: 31
© www.soinside.com 2019 - 2024. All rights reserved.