2012-05-31 59 views
0

對於python中的在線課程,我正在python中製作一個基本的基於文​​本的冒險遊戲。在python中修改另一個函數

現在,我有一個基本的庫存系統通過對布爾如果用戶有一個對象與否,以及有限的物品,如彈藥什麼的整數工作。 下面是庫存系統

def Inventory(self): #The inventory for the game. I don't know how to program it properly, so this is my testing ground. 
#This will hold the boolean values to if the player has the items or not. Another will be used to show the user the items 

    street_clothes = False 
    pistol = False 
    ammo = 0 
    phone = False 

的代碼,這就是我想修改上述

#Eric's apartment 
def Eric_Apartment(self): 

    print "type in grab your gun" 

    action = raw_input("> ") 

    if action == "grab": 
     self.Inventory(CR97) = True 
    # self.CR97_ammo += 15 
    # print CR97_ammo 
    # print self.CR97_ammo 

    exit(1) 

試圖運行這個程序讓我這個錯誤的庫存函數的代碼:

python ex43.py 
File "ex43.py", line 78 
self.Inventory(CR97) = True 
SyntaxError: can't assign to function call 

有什麼我應該做的嗎?我對python很陌生,這是我自己的第一個項目。

下面是完整的代碼,以供參考

from sys import exit #allows the program to use the exit(1) code 
from random import randint #allows the program to use a random number 

class Game(object): 

#quotes that pop up if the person dies, and also defines the start and self variables 
def __init__(self, start): 
    self.quips = [ 
     "You lose!" 
    ] 
    self.start = start 

def Inventory(self): #The inventory for the game. 
#This will hold the boolean values to if the player has the items or not. 

    street_clothes = False 
    pistol = False 
    ammo = 0 
    phone = False 


#this function launches the game, and helps with the room transfer 
def play(self): 
    next = self.start 

    while True: 
     print "\n---------" 
     room = getattr(self, next) 
     next = room() 

#if the user dies, or fails at the game, this is the function that is ran 
def death(self): 
    print self.quips[randint(0, len(self.quips)-1)] 
    exit(1) 

#Welcome screen to the game 
def welcome_screen(self): 
    print " place holder" 
    return 'intro_screen' 

#Intro screen to the game 
def intro_screen(self): 

    print "place holder" 

    action = raw_input("> Press any key to continue ") 

    return 'Eric_Apartment' 

#Eric's apartment 
def Eric_Apartment(self): 

    print "type in grab your gun" 

    action = raw_input("> ") 

    if action == "grab": 
     self.Inventory(CR97) = True 
    # self.CR97_ammo += 15 
    # print CR97_ammo 
    # print self.CR97_ammo 

    exit(1) 

a_game = Game("welcome_screen") 
a_game.play() 

回答

3

這是一個令人驚訝的不正當的方式去了解它。你爲什麼使用函數來存儲數據?

只需要一個播放器對象和一個庫存數組。

我建議使用對象來模擬物品。很好用於類層次結構。可能有一個基地項目,與子類SingleItem和StackableItem等

+0

而且我建議使用'set'來表示庫存 –

+0

我對OOP很陌生。我會用另一種方式修改其他課程中的東西,我會讓玩家抓住一把槍:假設我仍然使用布爾值來存儲玩家是否擁有它,那麼我會讓玩家抓住一把槍: 'code' Inventory.gun = True' code' ? – user1428649

+0

沒關係,我測試的那部分。有用。 我做過的唯一OOP編程是Java的。我仍然是一個新手。 – user1428649

0

除了使用功能,請嘗試使用類 -

class Player: 
    def __init__(self): 
     self.street_clothes = False 
     self.pistol = False 
     self.ammo = 0 
     self.phone = False 
    def give_street_clothes(self): 
     self.street_clothes = True 
    # etc 

不過個人,而不是使用每個項目作爲一個布爾值,我d使用項目清單:

class Player: 
    def __init__(self): 
     self.inventory = [] 
     # add code for ammo/pistol 
    def has_item(self, item): 
     return item in self.inventory 
    def give_item(self, item): 
     self.inventory.add(item) 
    def remove_item(self, item): 
     self.inventory.remove(item) 
    # etc 
相關問題