首页 > 解决方案 > Making a game but stuck on calling functions

问题描述

I am making a text based game where a player moves around from room to room. Each room is a Class with 4 positions in each room. I am using a Function for each position and Functions to take an input on where in the room to go. The problem is I want to Call a position Function then have that Function call an input Function again but because my Functions are all defined above the input Functions it doesn't work

Here is the code from the Class. The input Functions are at the bottom.

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

标签: python

解决方案


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

这种应用程序是Python 对象和类的经典用例。(看起来您可能仍在学习 Python,所以您可能还没有遇到过这些。)将房间、小兵和物品建模为一个类将使您能够以更简单、更直接的方式表达您的游戏。


推荐阅读