0

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

2 回答 2

3

这是一种非常反常的做法。为什么要使用函数来存储数据?

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

我建议也使用对象来建模项目。很好地用于类层次结构。可能有一个基础项目,具有子类 SingleItem 和 StackableItem 等。

于 2012-05-31T14:45:43.263 回答
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
于 2012-05-31T16:48:12.540 回答