简体   繁体   English

我想在 Python V 3.4.3 中为我的游戏制作一个库存系统

[英]I want to make an inventory system for my game in Python V 3.4.3

I want to make an inventory system in my game and it does add items to inventory but when I want it to print out the names of items in inventory it returns this:我想在我的游戏中创建一个库存系统,它确实将物品添加到库存中,但是当我希望它打印出库存中物品的名称时,它会返回:

This is your inventory: [<'class main .Item'>]这是您的库存:[<'class main .Item'>]

It looks like it is copying the Item class into the inventory list but I don't know how to fix it can somebody please tell me how to print the name of the items instead of the copy of the class?看起来它正在将 Item 类复制到库存列表中,但我不知道如何修复它,有人可以告诉我如何打印项目的名称而不是类的副本吗?

This is my code:这是我的代码:

import sys     #Imports the system commands which makes it possible to terminate the program
import time    #Imports the time module to allow delays in script
import os      #Imports os to make it possible to play music/sound
import random

print('Welcome to the Text Adventure login screen') #Login screen welcome message

username1 = input ('Please set a username: ') #Setting the username
password1 = input ('Please set a password: ') #Setting the password


username2 = input ('Please enter your username: ') #Entering the saved username
if username2 == username1 :
    print('Please enter password to prove it\'s ' + username1) #Prints confirmation messages

if username2 != username1:          #Checks if password's incorrect if so, Terminates the program
    print ('Username Incorrect!!!')
    sys.exit()


password2 = input ('Please enter your password: ') #Entering the saved password
if password2 == password1 :
    print ('Password Correct!')
if password2 != password1 :                    #Checks if password is incorrect, if so terminates the program
    print ('Password incorrect you hacker!!!')
    sys.exit


print ('WELCOME ' + username1 + ' TO TEXT ADVENTURE V 1.0')  #Prints welcome message to text adventure
os.system("start F:\Python\Adventure.mp3")

    #http://codereview.stackexchange.com/questions/57438/game-inventory-system   Website
class Item(object):
    def __init__(self, name, value, quantity = 1):
        self.name = name
        self.value = value
        self.quantity = quantity
def itemadd(self):
    inventory.append(Item)

class Weapon(Item):
    def __init__(self, name, value, damage, quantity = 1):
        Item.__init__(name, value, quantity)

        self.damage = damage

def weaponadd(self):
    weapons.append(Weapon)

def Wooden_Sword(Weapon):
    name = "Wooden Sword"
    value = 10
    damage = 25
def Lighter(Item):
    name = "Lighter"
    value = 5
def Sharp_Stick(Weapon):
    name = "Sharp Stick"
    value = 5
    damage = 15
def startRoom():
    global inventory 
    global character_Cash
    global character_Health
    global character_Damage_No_Weapon
    global weapons
    global weapon_Choice
    global weapon_Add
    global item_Add
character_Health = 1000
inventory = []
character_Cash = 200.00
character_Damage_No_Weapon = random.randint(1, 15)
weapons = []
goblin_Damage = random.randint(1, 10)
wizard_Damage = random.randint(1, 50)

time.sleep(5)
print ('You are in a cottage in Loch Scyze in Scotland where there is a large wooden chest in the room')
time.sleep(5)
print ('Your character\'s HP is %d' % character_Health)
time.sleep(5)
print ('Your character has $%d' % character_Cash)
time.sleep(5)
print ('Press [o] to open chest or press [e] to exit cottage')
choice1 = input ('What is your choice? ')

if choice1 == "e" :
    print ('You have exited the cottage')
    time.sleep(5)
    print ('To go North press [n], East [e] or West [w]')
    choice2 = input ('What is your choice? ')

    '''if choice2 == "e" :
        print ('''







    if choice2 == "n" :
        print ('You have entered the woods')
        time.sleep(5)

        print ('Press [w] to equip weapon or [e] to fight barefist')
        choice6 = input()
        if choice6 == "w" :
            if weapons == [] :
                print ('You have no weapons yet!')
                time.sleep(2.5)
            else :
                print ('These are your weapons: ')
                print (weapons)
                weapon_Choice = input ('Which weapon do you want to use? Type in weapons[Numberinlist] ')
                del weapon_Choice


        goblin_Health = 50
        print ('You have discovered a goblin press [a] to attack or [p] to pay $10 to escape ')
        print ('The goblin\'s health is %d' % goblin_Health)
        if choice6 == "e" :
            print ('You have no weapon equipped')
        choice3 = input ('What is your choice? ')

        if choice3 == "p" :
            character_Cash -= 10
            print ('You have successfully escaped')
            print ('You now have $%d' % character_Cash)
            print ('To go North press [n]')
            choice4 = input ()



            if choice4 == "n" :
                print('You have moved North')
                time.sleep(2.5)
                print ('You hear a loud noise and start running')
                os.system("start F:\Python\Forest.mp3")
                time.sleep(16.5)
                print ('...')
                time.sleep(16.5)
                os.system("start F:\Python\Shotgun.mp3")
                time.sleep(1)
                os.system("start F:\Python\Shell_Falling.mp3")
                print ('You hear someone shoot at you, you keep running')
                time.sleep(1)
                os.system("start F:\Python\Forest.mp3")
                time.sleep(16.5)
                print ('...')
                time.sleep(16.5)
                print ('OH NO!!! You have run over a death trap set up by a hunter')          
                time.sleep(5)
                os.system("start F:\Python\gameover.wav")
                print('GAME OVER')
                time.sleep(5)
                sys.exit()


        if choice3 == "a" :
            while goblin_Health | character_Health >= 0 :
                if goblin_Health | character_Health <= 0 :
                    print ('The Goblin is dead!')
                    time.sleep(2.5)
                else:
                    print ('You take a hit at the goblin')
                    goblin_Health -= character_Damage_No_Weapon
                    os.system("start F:\Python\Punch_1.mp3")
                    if goblin_Health | character_Health <= 5 :
                        print ('The Goblin is dead!')
                        time.sleep(2.5)
                    else :
                        print ('The goblin\'s health is %d' % goblin_Health)
                        time.sleep(2)
                        print ('The goblin attacks you!')
                        os.system("start F:\Python\Punch_2.mp3")
                        character_Health -= goblin_Damage
                        print ('Your health is %d' % character_Health)
                        time.sleep(2)


if choice1 == "o" :
    print ('You have opened a chest')
    time.sleep(2.5)
    print ('In the chest there is a wooden sword, $27, a lighter and a sharp stick')
    print ('Type y to take the items or n to leave them')
    pickup1 = input()
    if pickup1 == "y":
        weaponadd(Wooden_Sword) 
        itemadd(Lighter)           
        weaponadd(Sharp_Stick)
        os.system("start F:\Python\Coin.wav")
        character_Cash += 27
        print ('This is your inventory:')
        print (inventory)
        print ('These are your weapons:')
        print(weapons)
        print ('This is your cash balance:')
        print ('$%d' % character_Cash)
        time.sleep(15)

else:
    print ('You leave the items in the chest')
print ('In your inventory there is:')
print (inventory)
print ('You have $%d' % character_Cash)
print ('You are still in the cottage')
time.sleep(5)
print ('Press [e] to exit cottage')
choice5 = input ()
if choice5 == "e" :
    print ('You have exited the cottage')
for item in inventory
  print item.name

that's it.就是这样。

I don't know Python, but based on my knowledge of other languages, it looks to me like you may be printing the array itself rather than its contents.我不知道 Python,但根据我对其他语言的了解,在我看来,您可能正在打印数组本身而不是其内容。

To print the contents of the inventory array, you'll need to loop through it.要打印库存数组的内容,您需要遍历它。

Here's some information on how to loop through an array in Python:以下是有关如何在 Python 中循环遍历数组的一些信息:

6.3. 6.3. Iterating with for Loops 使用 for 循环进行迭代

There are many issues with your code at the moment.目前您的代码有很多问题。

def Wooden_Sword(Weapon):

creates a function called Wooden_Sword, within which you can refer to a variable called Weapon.创建一个名为 Wooden_Sword 的函数,您可以在其中引用名为 Weapon 的变量。 This is entirely unrelated to your Weapon class.这与你的武器类完全无关。 I suspect you actually want to have is lines我怀疑你实际上想要的是线条

Wooden_Sword = Weapon(name = "Wooden Sword", value = 10, damage = 25)

and so forth等等

Similarly your functions weaponadd and itemadd ignore self, and the class Weapon and the class Item to the list.类似地,你的函数 Weaponadd 和 itemadd 忽略了 self 和类 Weapon 和类 Item 到列表中。 This is why you see <'class main.Item'> when you try to print the inventory.这就是当您尝试打印库存时会看到 <'class main.Item'> 的原因。 Instead you can try相反,你可以尝试

def weaponadd(weapontoadd):
    weapons.append(weapontoadd)

Combined with the changes to your item declarations, you will have named items in your lists结合对您的项目声明的更改,您将在列表中命名项目

As a further note it looks like pasting into SO has messed up your indentation, which will change the meaning of a python program进一步说明,粘贴到 SO 中似乎弄乱了您的缩进,这将改变 python 程序的含义

In addition to the problems noted by @Caleth, you need to be explicit about how the programme should output the weapon information.除了@Caleth 指出的问题外,您还需要明确程序应该如何输出武器信息。 A simple way to do this is to change print(weapons) to something like this:一个简单的方法是将print(weapons)更改为如下所示:

for weapon in weapons:
    print("Name: {name}; value: {value}; damage: {damage}".format(name=weapon.name, value=weapon.value, damage=weapon.damage))

An alternative way is to implement the __repr__ method of the Weapon class:另一种方法是实现Weapon类的__repr__方法:

class Weapon(Item):
    def __init__(self, name, value, damage, quantity = 1):
        Item.__init__(name, value, quantity)    
        self.damage = damage

    def __repr__(self):
        return "Name: {name}; value: {value}; damage: {damage}".format(name=self.name, value=self.value, damage=self.damage)

Then later you can simply print(weapons) to print the list in the format specified.然后,您可以简单地print(weapons)以指定的格式打印列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM