需要帮助做一个简单的游戏吗

2024-09-27 22:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是想知道我怎样才能让它工作。我想检查函数player_position()中参数(pos)的值,并根据其值执行代码。我学习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()

Tags: theposyouifdefpositionupdateelse
1条回答
网友
1楼 · 发布于 2024-09-27 22:31:23

您正在调用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()

相关问题 更多 >

    热门问题