0

我一直在开发一款基于恐怖短信的假牙游戏,但我遇到了一些库存问题。库存是我可以在任何函数中调用的数组。我有点到了那里,但我只是每次都用新的库存重新填充我的阵列。我可以使用一些帮助,这些是我的库存功能:

#Populating an aray with items to be used throughout the game.
def createItems():
   items = range(0, 11)
   if items[9] != "full":
      items[1] = ("Axe")
      items[2] = ("Gas")
      items[3] = ("keys")
      items[4] = ("gun")
      items[5] = ("note")
      items[9] = ("full")
      return items
   else:
      return items 
# this function is going to check if the item passed to it is still in the array
def checkItems(item):
  list = createItems()
  itemC = item
  for i in range (0, 11):
    if list[i] == itemC:
      return ("no")
      break

def createInventory():
   inv = range(0 , 11)
   inv[10] = ("made")
   if inv[10] != ("made"):
      for i in range (0, 11):
         inv[i] = 0
   return inv

def stockInventory(item):
  inv = createInventory()
  for i in range (0, 11):
     if inv[i] == 0:
       inv[i] = item 
       break
       return inv

def checkInventory(item):
    itemC = item
    inv = createInventory()
    for i in range(0, 11):
       if itemC == inv[i]:
          return ("yes")
4

2 回答 2

1

这可能不是答案,但从我可以从代码和问题中得出的结论,这应该会有所帮助。请注意您的代码和我的代码中的差异并相应地进行更改。

# Main Inventory
Inventory = createInventory()

# Populating given inventory aray with items to be used throughout the game.
def createItems(inv):
    items = inv
    items[1] = "Axe"
    items[2] = "Gas"
    items[3] = "keys"
    items[4] = "gun"
    items[5] = "note"
    items[9] = "full"

# Check if the item passed to it is still in the inventory array
def checkItems(item):
    items = Inventory
    for i in range(len(items)):
        if items[i] == item:
            return "yes"
    return "no"

def createInventory():
    inv = range(11)
    inv[10] = "made"
    return inv

def stockInventory(item):
    inv = Inventory
    for i in range (11):
        if inv[i] == 0:
            inv[i] = item
            break
    return inv

def checkInventory(item):
    inv = Inventory
    for i in range(0, 11):
        if item == inv[i]:
            return "yes"
    return "no"
于 2013-11-04T00:30:47.380 回答
0

您可以将库存设为布尔值。

inventory = {"sword":True, "axe":True}

它还有助于为您的库存制作课程。

class Weapon():

    def attack(self, monster):
        pass

class Axe(Weapon):
    def __init__(self):
        self.name = "axe"
        self.damage = 1

    def attack(self, monster):
        monster.hp -= self.damage
于 2017-01-15T04:27:03.897 回答