需要协助制作一个简单的游戏

时间:2018-06-06 21:31:56

标签: python

我只是想知道如何才能做到这一点elif player_position == 1将会奏效。我想在函数pos中检查参数(player_position())的值,并根据其值执行代码。我只学习了大约1个月的Python。

def player_position(pos):

    position = pos

    if position == goliath.spawn:
        print('The Goliath Has Attacked!')

        if goliath.accuracy == 1:
            print('You Have Been Killed!')

        else:
            print('You Killed The Goliath!')

    else:
        print('Nothing Happens...')


def starting_room():

    while True:

        position_update = input('Enter A Direction: ')

        if position_update == 'Forwards':
            player_position(1)

        elif position_update == 'Backwards':
            player_position(3)

        elif position_update == 'Left':
            player_position(4)

        elif position_update == 'Right':
            player_position(2)

        elif player_position == 1:

            if position_update == 'Forwards':
                print('Room 2')

            elif position_update == 'Backwards':
                player_position(0)

            elif position_update == 'Left':
                print('There Are Monsters In the Dark')

            elif position_update == 'Right':
                print('There Are Monsters In The Dark')

starting_room()

1 个答案:

答案 0 :(得分:0)

你正在调用player_position,这个函数不会返回任何内容。相反,假设您想要跟踪玩家在starting_room函数中的当前位置,您需要跟踪那里的位置。

这样的事情:(注意 - 你需要添加更多的代码来突破while循环 - 这段代码应该允许你跟踪pos虽然)

def player_position(pos):
        position = pos
        if position == goliath.spawn:

            print('The Goliath Has Attacked!')

            if goliath.accuracy == 1:

                print('You Have Been Killed!')

            else:
                print('You Killed The Goliath!')

        else:
            print('Nothing Happens...')

def starting_room():
    pos = 0 #initialize the pos to 0
    while True:
        position_update = input('Enter A Direction: ')
        if pos == 1: #check to see if current pos is 1
            if position_update == 'Forwards':
                print('Room 2')
                #you may want to add "break" here to stop this while loop

            elif position_update == 'Backwards':
                pos = 0

            elif position_update == 'Left':
                print('There Are Monsters In the Dark')

            elif position_update == 'Right':
                print('There Are Monsters In The Dark')

        elif position_update == 'Forwards':
            pos = 1

        elif position_update == 'Backwards':
            pos = 3

        elif position_update == 'Left':
            pos = 4

        elif position_update == 'Right':
            pos = 2
        player_position(pos) #call the player_position function with the current pos

starting_room()