2013-11-03 5 views
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") 
+2

코드를 올바르게 들여 씁니다. 따르기가 어렵습니다. –

+0

@DanielRoseman – Blank1268

+0

이 코드에 4 ~ 5 가지의 근본적인 오해가 있기 때문에 대답하기가 어려울 것입니다. 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

감사합니다, 이것은 내가 찾고있는 것입니다. 나는 파이썬이 지원하는 전역 변수를 몰랐다. 나쁜 교사 ... – 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 
관련 문제