對於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()
而且我建議使用'set'來表示庫存 –
我對OOP很陌生。我會用另一種方式修改其他課程中的東西,我會讓玩家抓住一把槍:假設我仍然使用布爾值來存儲玩家是否擁有它,那麼我會讓玩家抓住一把槍: 'code' Inventory.gun = True' code' ? – user1428649
沒關係,我測試的那部分。有用。 我做過的唯一OOP編程是Java的。我仍然是一個新手。 – user1428649