在python中修改另一个函数

时间:2012-05-31 14:42:45

标签: python function boolean

对于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()

2 个答案:

答案 0 :(得分:3)

这是一种非常不正常的方式。为什么使用函数存储数据?

只需拥有一个带库存数组的玩家对象。

我建议使用对象来建模项目。适用于类层次结构。 COuld有一个基础Item,Subclasses SingleItem和StackableItem等。

答案 1 :(得分: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

但就个人而言,我不是将每个项目用作布尔值,而是使用项目列表:

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