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

それはそれについて行くための驚くほどひねくれた方法です。なぜ関数を使ってデータを保存しているのですか?

インベントリ配列を持つプレーヤーオブジェクトを用意するだけです。

オブジェクトを使用してアイテムをモデル化することもお勧めします。クラス階層に適しています。COuldには、サブクラス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 に答える