首页 > 解决方案 > 访问方法变量

问题描述

我创建了一个有恶魔的房子逃脱游戏。当你进入一个有恶魔的房间时,我称之为战斗方法。一切都很好,除了我在理解如何在战斗中被杀时减少生命时遇到问题。战斗是基于骰子的。我尝试使用布尔标志来标记玩家何时被杀死以减少但我似乎无法访问该结果来减少玩家的生命。任何帮助表示赞赏。谢谢堆栈社区。

我也尝试过处理方法内部的生命减量,但是当我在战斗方法之外调用变量时,这对我也不起作用。

import random


def showInstructions():
    # Display main menu and the commands to use in the game
    print('''
Hill House
----------
Commands:
go [direction]
get [item] 
    ''')


def showStatus():
    # Display the current player status
    print('-----------------------------')
    print('You are in the ' + currentRoom + ' with ' + str(currentLives) + ' lives')
    # Display the items currently in players inventory
    print('Inventory : ' + str(inventory))
    # This if statement displays items in the room should there be any
    if "item" in rooms[currentRoom]:
        print('You see a ' + rooms[currentRoom]['item'])
        print("--------------------------------------")


def player():
    currentLives = 5
    return currentLives


def combat():
    # combat function
    print('DEFEND YOURSELF OR ATTEMPT TO FLEE!')
    print('''   
-----------------------------------------
Commands:
To fight the Demon - roll 
To attempt to escape the Demom - flee 
    ''')
    playersCommand = input('>')
    demonsRoll = random.randrange(10)
    if playersCommand == 'roll':
        playersRoll = random.randrange(10)
        if playersRoll >= demonsRoll:
            print('The Demon has rolled a ' + str(demonsRoll) + ' and you rolled a '
                  + str(playersRoll) + '. You have killed the demon!')
            del rooms[currentRoom]['demon']
            fightResult = True

        else:
            print('The Demon has rolled a ' + str(demonsRoll) + ' and you rolled a '
                  + str(playersRoll) + '. You have been killed!')
            fightResult = False

    else:
        playersCommand == 'flee'
        playersRoll = random.randrange(10)
        if playersRoll >= demonsRoll:
            print('The Demon has rolled a ' + str(demonsRoll) + ' and you rolled a '
                  + str(playersRoll) + '. YOU HAVE GOT AWAY!')
        else:
            print('The Demon has rolled a ' + str(demonsRoll) + ' and you rolled a '
                  + str(playersRoll) + '. You have been killed!')
        del inventory[:]
        fightResult = False


# inventory that is empty
inventory = []

# Starting lives
currentLives = 5

# Rooms
rooms = {

    'Master Bathroom': {
        'west': 'Master Bedroom',
        'item': 'Green Spirit Stone',
        'demon': 'demon'
    },
    'Master Bedroom': {
        'east': 'Master Bathroom',
        'west': 'Up Stairs',
        'item': 'Glowing Red Crystal'
    },
    'Guest Bedroom': {
        'west': 'Library'
    },
    'Library': {
        'north': 'Up Stairs',
        'east': 'Guest Bedroom',
        'item': 'Mirror Stone'
    },
    'Up Stairs': {
        'north': 'Ball Room',
        'east': 'Master Bedroom',
        'west': 'Hall',
        'south': 'Library'
    },
    'Ball Room': {
        'east': 'Dinning Room',
        'south': 'Up Stairs',
        'west': 'Dinning Room',
        'item': 'Pale Purple Crystal'
    },
    'Hall': {
        'north': 'Dinning Room',
        'east': 'Exit',
        'west': 'Up Stairs',
        'south': 'Kitchen'
    },
    'Dinning Room': {
        'east': 'Ball Room',
        'south': 'Hall',
        'item': 'Tall Tale Heart'
    },
    'Kitchen': {
        'north': 'Hall',
        'item': 'sacred water',
        'demon': 'demon'
    },
    'Exit': {
        'east': 'Hall'
    }
}
# The defined starting point
currentRoom = 'Hall'

# Display the instructions
showInstructions()

# game loop
while True:


    showStatus()

    # get the player's position
    move = ''
    while move == '':
        move = input('>')

    move = move.split()

    # if they type 'go' first
    if move[0] == 'go':
        # check that they are allowed wherever they want to go
        if move[1] in rooms[currentRoom]:
            # set the current room to the new room
            currentRoom = rooms[currentRoom][move[1]]
        # there is no door (link) to the new room
        else:
            print('You can\'t go that way!')

    # if they type 'get' first
    if move[0] == 'get':
        # if the room contains an item, and the item is the one they want to get
        if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
            # add the item to their inventory
            inventory += [move[1]]
            # display a helpful message
            print(move[1] + ' got!')
            # delete the item from the room
            del rooms[currentRoom]['item']
        # otherwise, if the item isn't there to get
        else:
            # tell them they can't get it
            print('Can\'t get ' + move[1] + '!')

    if 'demon' in rooms[currentRoom]:
        print("YOUR UNDER ATTACK BY A DEMON!")
        print('')
        combat()

标签: pythoninheritance

解决方案


推荐阅读