如何让“魔药”限制在5?

时间:2016-06-08 16:39:02

标签: python

我正在做一个战斗模拟器的事情,我希望玩家能够使用药水,这会增加健康,最多5次。我还希望玩家能够选择不同类型的物品,但那些不同的物品不能超过五件。这是我到目前为止所做的:

import random, sys, time
pokedex = {"Bulbasaur": [200,32,],
       "Ivysaur"  : [230,40],
       "Venusaur" : [270,50],
       "Charmander": [188,30],
       "Charmeleon": [226,40],
       "Charizard": [266,84],
       "Squirtle": [198,48],
       "Wartortle": [228,31],
       "Blatoise": [268,41],
       "Caterpie": [200,15],
       "Metapod": [210,10],
       "Butterfree": [230,22],
       "Weedle": [190,17],
       "Kakuna": [200,12],
       "Beedrill": [240,45],
       "Pidgey": [190,22],
       "Pidgeotto": [236,30],
       "Pidgeot": [276,40],
       "Rattata": [170,28],
       "Raticate": [220,40],
       "Spearow": [190,30],
       "Fearow": [240,45],
       "Ekans": [180,30],
       "Arbok": [230,42],
       "Pikachu": [180,27],
       "Raichu": [230,45],
       "Sandshrew": [210,37],
       "Sandslash": [260,50],
       "Nidoran♀": [220,23],
       "Nidorina": [250,32],
       "Nidoqueen": [290,46],
       "Nidoran♂": [202,28],
       "Nidorino": [232,36],
       "Nidoking": [272,51],
       "Clefairy": [250,22],
       "Clefable": [300,35],
       "Vulpix": [186,20],
       "Ninetales": [256,38],
       "Jigglypuff": [340,22],
       "Wigglytuff": [390,35],
       "Zubat": [190,22],
       "Golbat": [260,40],


       }
   enemy = random.choice(list(pokedex.keys()))
   enemy = [enemy, pokedex[enemy][0]]          
   player = random.choice(list(pokedex.keys()))
   player = [player, pokedex[player][0]]
   print("Your pokemon is: %s" % (player[0]))
   print("The enemy's pokemon is: %s" % (enemy[0]))

   while(player[1] > 0 and enemy[1] > 0):
       if pokedex[player[0]][1] < enemy[1]:
           choice = input("What would you like to do?")
           if choice in ["Attack","attack","Kill","kill"]:
               print("The player has attacked the enemy's %s." % (enemy[0]))
               enemy[1] -= pokedex[player[0]][1]
               print("The enemy's %s has %d HP remaining!" % (enemy[0], enemy[1]))
           if choice in ["potion","Potion"]:
                player[1] = player[1]+20
       else:
           enemy[1] = 0
           print("The enemy's %s has %d HP remaining!" % (enemy[0], enemy[1]))
           print("The player's %s has won!" % (player[0]))
           break
    if pokedex[enemy[0]][1] < player[1]:
        print("The enemy has attacked the player's %s." % (player[0]))
        player[1] -= pokedex[enemy[0]][1]
        print("The player's %s has %d HP remaining!" % (player[0], player[1]))
    else:
        player[1] = 0
        print("The player's %s has %d HP remaining!" % (player[0], player[1]))
        print("The enemy's %s has won!" % (enemy[0]))
        break

格式化有点奇怪,但我向你保证,它可以在我的最后工作,唯一需要的是上面的药水问题。任何帮助是极大的赞赏!

3 个答案:

答案 0 :(得分:2)

简单的解决方案是跟踪已使用了多少药剂,并使用某种条件(if / while)围绕将该值与5进行比较。例如:

     potions_used = 0

     if choice in ["potion","Potion"]:
         if (potions_used <= 5):
            player[1] = player[1]+20
            potions_used += 1

答案 1 :(得分:2)

创建一个玩家对象,其中包含玩家拥有的项目列表。 创建一个将项添加到该列表的方法。在此方法中,您现在可以在将项目添加到列表之前检查项目数。

class Player(object):
    __slots__ = ['items', 'potions']

    MAX_POTIONS = 5

    def __init__(self):
        self.items = []
        self.potions = Player.MAX_POTIONS

    def use_potion():
        # Here you can check if the number of potions you have
        # has reached zero
        pass

    def add_item(item):
        # Here you can check the number of items the player has
        pass

答案 2 :(得分:1)

跟踪变量,“potionsUsed”或类似的东西,每次使用药水时增加它,当它达到5时,不允许玩家使用药水。

if (choice in ["potion","Potion"]) && (potionsUsed < 5):
            player[1] = player[1]+20

像这样的东西

相关问题