制作一个游戏,但仍停留在调用函数

2024-10-04 05:23:26 发布

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

我正在做一个基于文本的游戏,玩家从一个房间移动到另一个房间。每个房间是一个班级,每个房间有4个位置。我为每个位置使用一个函数,并使用函数来输入房间中要去的地方。问题是我想调用一个位置函数,然后让这个函数再次调用一个输入函数,但是因为我的函数都是在输入函数上面定义的,所以它不起作用

这是这个类的代码。输入函数在底部

2教室:

room = 2

firstAid_location = []

if firstAid_room == room:
    firstAid_location = firstAid_spawn


def starting_position(self):

    global room2_minion
    global playerHealth
    global firstAid_room

    position_start = 1
    minion_hit = random.randrange(1, 3)

    print('You Are Now In The North Room')

    if position_start == self.firstAid_location:

        print('You Found The First Aid Kit')
        print('Your Health Has Been Increased By 2HP!')
        playerHealth += 2
        firstAid_room += 10

        if position_start == room2_minion:
            print('A Minion Has Attacked!')

            if minion_hit == 1:
                print('The Minion Hit You And Then Fainted From Exhaustion')
                print('You Have Lost 2HP')
                playerHealth -= 2
                room2_minion += 10

                if playerHealth <= 0:
                    print('You Have Died')
                    quit()

                else:
                    print('You Have', playerHealth, 'HP Left')


            else:
                print('The Minion Missed The Attack!')
                room2_minion += 10


    elif position_start == room2_minion:
        print('A Minion Has Attacked!')

        if minion_hit == 1:
            print('The Minion Hit You Then Fainted From Exhaustion')
            print('You Have Lost 2HP')
            playerHealth -= 2
            room2_minion += 10

            if playerHealth <= 0:
                print('You Have Died')
                quit()

            else:
                print('You Have', playerHealth, 'HP Left')


        else:
            print('The Minion Missed The Attack!')


    else:
        pass


def pos1(self):

    global room2_minion
    global playerHealth
    global firstAid_room

    position1 = 1
    minion_hit = random.randrange(1, 3)

    if position1 == self.firstAid_location:

        print('You Found The First Aid Kit')
        print('Your Health Has Been Increased By 2HP!')
        playerHealth += 2
        firstAid_room += 10

        if position1 == room2_minion:
            print('A Minion Has Attacked!')

            if minion_hit == 1:
                print('The Minion Hit You And Then Fainted From Exhaustion')
                print('You Have Lost 2HP')
                playerHealth -= 2
                room2_minion += 10

                if playerHealth <= 0:
                    print('You Have Died')
                    quit()

                else:
                    print('You Have', playerHealth, 'HP Left')

            else:
                print('The Minion Missed The Attack!')
                room2_minion += 10

    elif position1 == room2_minion:
        print('A Minion Has Attacked!')

        if minion_hit == 1:
            print('The Minion Hit You Then Fainted From Exhaustion')
            print('You Have Lost 2HP')
            playerHealth -= 2
            room2_minion += 10

            if playerHealth <= 0:
                print('You Have Died')
                quit()

            else:
                print('You Have', playerHealth, 'HP Left')

        else:
            print('The Minion Missed The Attack!')

    else:
        pass


def pos2(self):

    global room2_minion
    global playerHealth
    global firstAid_room

    position2 = 2
    minion_hit = random.randrange(1, 3)

    if position2 == self.firstAid_location:

        print('You Found The First Aid Kit')
        print('Your Health Has Been Increased By 2HP!')
        playerHealth += 2
        firstAid_room += 10

        if position2 == room2_minion:
            print('A Minion Has Attacked!')

            if minion_hit == 1:
                print('The Minion Hit You And Then Fainted From Exhaustion')
                print('You Have Lost 2HP')
                playerHealth -= 2
                room2_minion += 10

                if playerHealth <= 0:
                    print('You Have Died')
                    quit()

                else:
                    print('You Have', playerHealth, 'HP Left')

            else:
                print('The Minion Missed The Attack!')
                room2_minion += 10

    elif position2 == room2_minion:
        print('A Minion Has Attacked!')

        if minion_hit == 1:
            print('The Minion Hit You Then Fainted From Exhaustion')
            print('You Have Lost 2HP')
            playerHealth -= 2
            room2_minion += 10

            if playerHealth <= 0:
                print('You Have Died')
                quit()

            else:
                print('You Have', playerHealth, 'HP Left')

        else:
            print('The Minion Missed The Attack!')

    else:
        pass




def pos3(self):

    global room2_minion
    global playerHealth
    global firstAid_room

    position3 = 3
    minion_hit = random.randrange(1, 3)

    if position3 == self.firstAid_location:

        print('You Found The First Aid Kit')
        print('Your Health Has Been Increased By 2HP!')
        playerHealth += 2
        firstAid_room += 10

        if position3 == room2_minion:
            print('A Minion Has Attacked!')

            if minion_hit == 1:
                print('The Minion Hit You And Then Fainted From Exhaustion')
                print('You Have Lost 2HP')
                playerHealth -= 2
                room2_minion += 10

                if playerHealth <= 0:
                    print('You Have Died')
                    quit()

                else:
                    print('You Have', playerHealth, 'HP Left')

            else:
                print('The Minion Missed The Attack!')
                room2_minion += 10

    elif position3 == room2_minion:
        print('A Minion Has Attacked!')

        if minion_hit == 1:
            print('The Minion Hit You Then Fainted From Exhaustion')
            print('You Have Lost 2HP')
            playerHealth -= 2
            room2_minion += 10

            if playerHealth <= 0:
                print('You Have Died')
                quit()

            else:
                print('You Have', playerHealth, 'HP Left')

        else:
            print('The Minion Missed The Attack!')

    else:
        pass


def pos4(self):

    global room2_minion
    global playerHealth
    global firstAid_room

    position4 = 4
    minion_hit = random.randrange(1, 3)

    if position4 == self.firstAid_location:

        print('You Found The First Aid Kit')
        print('Your Health Has Been Increased By 2HP!')
        playerHealth += 2
        firstAid_room += 10

        if position4 == room2_minion:
            print('A Minion Has Attacked!')

            if minion_hit == 1:
                print('The Minion Hit You And Then Fainted From Exhaustion')
                print('You Have Lost 2HP')
                playerHealth -= 2
                room2_minion += 10

                if playerHealth <= 0:
                    print('You Have Died')
                    quit()

                else:
                    print('You Have', playerHealth, 'HP Left')

            else:
                print('The Minion Missed The Attack!')
                room2_minion += 10

    elif position4 == room2_minion:
        print('A Minion Has Attacked!')

        if minion_hit == 1:
            print('The Minion Hit You Then Fainted From Exhaustion')
            print('You Have Lost 2HP')
            playerHealth -= 2
            room2_minion += 10

            if playerHealth <= 0:
                print('You Have Died')
                quit()

            else:
                print('You Have', playerHealth, 'HP Left')

        else:
            print('The Minion Missed The Attack!')

    else:
        pass


def pos1_input(self):
    pass

def pos2_input(self):
    pass

def pos3_input(self):
    pass

def pos4_input(self):
    pass

Tags: theselfyouifhaveglobalelsehas
1条回答
网友
1楼 · 发布于 2024-10-04 05:23:26

将房间表示为函数不太可能是一种可行的方法。正如你所发现的,你最终会重复很多代码

这种应用程序是Python objects and classes的典型用例(看起来您可能还在学习Python,所以您可能还没有遇到这些问题。)将房间、仆从和物品建模为一个类将允许您以更简单、更直接的方式表达您的游戏

相关问题 更多 >