简体   繁体   English

python 文字曲棍球游戏,向库存添加物品

[英]python text hockey game, issue adding items to inventory

I'm trying to do a text game for school and having issues adding the items to inventory, I have the moving between rooms working but the inventory is what's getting me now.我正在尝试为学校做一个文字游戏,但在将物品添加到库存时遇到问题,我可以在房间之间移动,但库存是我现在的动力。 I just keep getting errors no matter how I set up the get items, I have issues.无论我如何设置获取项目,我都会不断收到错误,我有问题。 Is there an easy way to add this functionality that I am missing?有没有一种简单的方法可以添加我缺少的这个功能?

# Game Description
# The main character is an ice hockey goalie for the local home team. 
# Their cities rival is playing them tonight and the opposing team’s goalie broke in to the locker room
# and hid the home teams goalie equipment all over the building to try and rattle him causing him to lose time warming up. 
# This includes his helmet, chest armor, blocker, glove, and each leg pad.  
# Time is clicking off and the home team goalie needs to find his stuff so he can get dressed and warm up.


rooms = {
    'Front Lobby': {'East': 'Ice Rink', 'Item': 'Glove'},
    'Ice Rink': {'North': 'Away Locker Room', 'West': 'Front Lobby', 'East': 'Mens Locker Room', 'South': 'Lounge', 'Item': 'Helmet'},
    'Away Locker Room': {'East': 'Pro Shop', 'South': 'Ice Rink'},
    'Pro Shop': {'West': 'Away Locker Room', 'Item': 'Villian'},
    'Mens Locker Room': {'West': 'Ice Rink', 'Item': 'Leg Pad 2'},
    'Lounge': {'West': 'Womens Locker Room', 'East': 'Skate Rentals', 'North': 'Ice Rink', 'Item': 'Blocker'},
    'Womens Locker Room': {'East': 'Lounge', 'Item': 'Leg Pad 1'},
    'Skate Rentals': {'West': 'Lounge', 'Item': 'Chest Armor'}
}
gameOn = True
inventory = []

#moves player from room to room
def move(player, direction):
    global rooms
    current_room = player 

    # check if there is a room in the specified direction
    if direction in rooms[current_room]:
        current_room = rooms[current_room][direction]

    # error handling
    else:
        print("There is nothing in that direction!")
        return player # Returning the same room if nothing Exists in the direction

    # return the player state
    return current_room # Indent Error

#displays rules at start of game 
def showRules():

    print("- Collect 6 items to win the game, or have to get clowned by the other goalie.\n" "Move commands: go South, go North, go East, go West.\n" "Add to Inventory: get 'item name'. Once all items collected type: ‘Finish’")


def main():
    showRules()
    player = "Front Lobby" 

    while gameOn:

        current_room = player

        # output
        print(f"\nYou are in the {current_room}")
        print('Inventory: ',  inventory)

        # Goalie got you
        if player == 'Pro Shop':
            print('You make more excuses than saves. GAME OVER')
            break

        # input validation and parsing
        print("----------------")
        player_move = input("Enter your move:\n") 

        # invalid move syntax
        if 'go' in player_move or 'Finish' in player_move:
                
    
            # split string 
            
            action = player_move.split(' ')
            print(action)        # move 
            if action[0] == 'go':
                player = move(player, action[1]) # Assigning the value to player 
                                                 
            elif action == 'Finish':
                if len(inventory) == 6:
                    print('Awesome! Now get on the ice! You Win.')
                else:
                    print('Keep collecting items')
    
            # invalid action
            else:
                print("Invalid command!")
        
        elif 'get' in player_move:
            action = player_move.split(' ')
            getItem(current_room, action[1])
            
        else:
            print('Invalid Command')
            continue


def getItem(player, item):
    current_room = player
    if item in rooms[current_room]:
        inventory.append(item)
        print(inventory)
    
    

main()

updated the code 11:05 EST更新代码 11:05 EST

Try to write your getItem function more like this:尝试像这样编写您的 getItem function :

def getItem(current_room, item):
    if item == rooms[current_room]["Item"]:
        inventory.append(item)
        print(inventory)

Explanation: in you original expression item in rooms[current_room] , rooms[current_room] points to a dictionary, so the x in dict expression will check if x is part of the key of that dictionary.解释:在你原来的表达式item in rooms[current_room]中,rooms[current_room] 指向一个字典,所以x in dict表达式将检查x是否是该字典的键的一部分。 But if I understand well, you are only interested by the "Item" part of that dict, and you have to make the comparison with the value and not with the key.但如果我理解得很好,你只对该字典的“Item”部分感兴趣,你必须与值而不是键进行比较。

I ran it through my Python compiler and came up with a syntax error.我通过我的 Python 编译器运行它并提出语法错误。 You missed the second condition and action in your block in one of the last if/elif/else blocks in main() .您错过了main()中最后一个 if/elif/else 块中块中的第二个条件和操作。 I don't know much but I think the code might be this:我不太了解,但我认为代码可能是这样的:

elif 'get' in player_move:
            action = player_move.split(' ')
            if action[1] == <second condition here>:  #The second condition was missed here
                <action> #The action was missed here

IDK about the code you want there. IDK 关于你想要的代码。
I'm not completely sure but I think there is something wrong with the move() function. Try the other answer, adding this snippet to getItem() :我不完全确定,但我认为move() function 有问题。尝试其他答案,将此片段添加到getItem()

    if item == rooms[current_room]["Item"]:
        inventory.append(item)
        print(inventory)

If that doesn't work, then send me an email: shivernscorch@gmail.com and we can take it from there.如果这不起作用,请给我发送一个 email:shivernscorch@gmail.com,我们可以从那里获取。

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

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