2013-11-03 61 views
0

我一直在发短信的恐怖基于adenture游戏&我有一些库存问题。库存是一个可以在任何函数中调用的数组。我有种感觉,但是我每次都用新的库存重新填充我的阵列。我可以利用一些帮助,这些都是我的库存功能:基于文本的冒险游戏的库存问题蟒蛇

#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") 
+2

请正确缩进您的代码,这是不可能的。 –

+0

只是做比较遗憾的是@DanielRoseman – Blank1268

+0

这将是很难回答,因为在这段代码四五真的根本误解。我建议你看看部分http://docs.python.org/2/tutorial/ 1-5,然后回来这个... – YXD

回答

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" 
+0

谢谢,这是我正在寻找。我不知道Python支持全局变量。坏老师... – Blank1268

+0

检查了这一点 - http://docs.python.org/2/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python –

0

你可以让使库存为boolen值。

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