制作游戏但坚持调用功能

时间:2018-06-05 04:00:52

标签: python

我正在制作一个基于文本的游戏,玩家可以在一个房间之间移动。每个房间都是一个班级,每个房间有4个职位。我正在为每个位置和函数使用一个函数来输入房间的哪个位置。问题是我想调用一个位置函数然后让函数再次调用一个输入函数,但因为我的函数都在输入函数上面定义它不起作用

以下是Class中的代码。输入函数位于底部。

课堂Room2:

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

1 个答案:

答案 0 :(得分:2)

将房间表示为功能不太可能是一种可行的方法。正如您所发现的那样,您最终会重复大量代码。

这种应用程序是Python objects and classes的经典用例。 (看起来你可能还在学习Python,所以你可能还没有遇到过这些。)将每个房间,小兵和物品作为一个类进行建模将使你能够以更简单,更直接的方式表达你的游戏。 / p>

相关问题