这是一个基于文本的游戏,但无法正常工作

时间:2019-07-02 23:40:37

标签: python python-3.x

这应该成为基于文本的游戏,但是由于某些原因,我的代码会在第一个问题之后重复出现

import sys
import random
from time import sleep


class Person:
    def __init__(self, name, health, power, age):
        self.name = name
        self.health = health
        self.power = power
        self.age = age


main_character = Person(name=None, health=250, power=None, age=random.randint(12, 100))
Enemy = Person(name="Thanos", health=200, power=random.randint(60, 80), age=30)


def power_getter():
    if main_character.age >= 40:
        main_character.power = random.randint(20, 40)
    elif main_character.age < 40:
        main_character.power = random.randint(60, 80)


def select_name():
    human_name = input("please write your name")
    main_character.name = human_name


def start_game():
    print("Your name is ", main_character.name, " and your goal is to  find the hidden treasure of eternal life")
    sleep(2)
    print("You have been looking for your treasure for ", main_character.age/2, "years")
    sleep(2)
    print("And now after all of this search")
    sleep(2)
    print("You located your treasure's but your enemy")
    sleep(1.5)
    print("His name is Thanos")
    sleep(1)


def old_main_character():
    print("Also located the treasure")
    sleep(.5)
    print("Because of your long search you became weak and old you are(", main_character.age, ")so you'd better pick your fights")
    sleep(2)
    print("You are in  an island you have two places to go for your treasure")
    sleep(2)
    print('put in mind that your enemy is in the island to and looking for the treasure')
    try:
        location_old = int(input("Where do want to go?\n1-Through the jungle\n2-To Around the island\nplease\
                choose (1/2)"))
    except Exception:
        pass
    while location_old != 1 and location_old != 2:
        if location_old != 1 and location_old != 2:
            print("please enter (1/2)")
            try:
                location_old = int(input("Where do want to go?\n1-Through the jungle\n2-To Around the island\nplease\
                    choose (1/2)"))
            except Exception:
                pass
        if location_old == 1:
            print("Your enemy found the treasure before you")
            sleep(1)
            print("You lost")
        elif location_old == 2:
            print("You met your enemy!!")
            sleep(1)
            try:
                old_fight = int(input("Do you want to:\n1-Get runaway through the jungle\n2-Fight him"))
            except Exception:
                pass
            while old_fight != 1 and old_fight != 2:
                if old_fight != 1 and old_fight != 2:
                    print("please enter 1/2")
                    try:
                        old_fight = int(input("Do you want to:\n1-Get runaway through the jungle\n2-Fight him"))
                    except Exception:
                        pass
                if old_fight == 1:
                    print("Your enemy went  after you")
                    sleep(1)
                    print("You running away from ", Enemy.name, " you found a man who is drowning in quicksand")
                    sleep(2)
                    old_fight_help = input("help him? please choose(y/n)")
                    while old_fight_help != "y" and old_fight_help != "n":
                        if old_fight_help != "y" and old_fight_help != "n":
                            old_fight_help = input("please choose(y/n)")
                        if old_fight_help == "y":
                            print("Because you helped him he became your helper and helped you to defeat your enemy")
                            sleep(3)
                            print("The jungle wasn't the path because it was closed so he helped\
                                    you go around the island")
                            sleep(2)
                            print("You won!!")
                        elif old_fight_help == "n":
                            print('By the time your enemy keep up with you and defeats you')
                elif old_fight == 2:
                    print("because of your overwhelming courage")
                    sleep(4)
                    print("You lost")
        elif location_old == 2:
            print("You found an end to the jungle")
            sleep(2)
            print("It wasn't the right path")
            print("Your enemy gets to the treasure before you")
            sleep(1)
            print("You lost")


def young_main_character():
    print("He also located the treasure")
    print("You are now in the island")
    sleep(2)
    print("keep in mind that your enemy is in the island too")
    sleep(1)
    print("You have a jungle and a way around the island where do you want to go?")
    try:
        location_young = int(input("1-To the jungle\n2-Go around the island"))
    except Exception:
        pass
    while location_young != 1 and location_young != 2:
        if location_young != 1 and location_young != 2:
            print("please choose 1/2")
            location_young = int(input("1-To the jungle\n2-Go around the island"))
        if location_young == 1:
            print("You found an end to the jungle")
            sleep(1)
            print(Enemy.name and "found the treasure")
            sleep(2)
            print("You lost")
            sleep(2)
        elif location_young == 2:
            print("You met your enemy")
            young_figh = int(input("Do you want to:\n 1-Fight him \n2-Run away "))
            while young_figh != 1 and young_figh != 2:
                if young_figh != 1 and young_figh != 2:
                    young_figh = int(input("Do you want to:\n 1-Fight him \n2-Run away "))
                if young_figh == 1:
                    while main_character.health > 0 and Enemy.health > 0:
                        Enemy.health = Enemy.health - main_character.power
                        main_character.health = main_character.health - Enemy.power
                elif young_figh == 2:
                    print("your enemy gets the treasure")
                    sleep(1)
                    print("You lost")


def game_reptition():
    print("Do you want to play again")
    again = input("please choose(y/n)")
    while again != "y" and again != "n":
        if again != "y" and again != "n":
            again = input("please choose(y/n)")
        if again == "y":
            print("Oh great here we go  again")
        elif again == "n":
            print("See you next time")
            sys.exit()
        else:
            print("please enter (y/n)")


def story_selector():
    if main_character.age >= 40:
        old_main_character()
    else:
        young_main_character()


while True:
    main_character = Person(name=None, health=250, power=None, age=random.randint(12, 100))
    Enemy = Person(name="Thanos", health=200, power=random.randint(60, 80), age=30)
    power_getter()
    select_name()
    start_game()
    story_selector()
    game_reptition()

请记住,您的敌人在岛上寻找宝藏 想去哪里? 1-穿越丛林 2至环岛 请选择(1/2)1 你想再玩一次吗 请选择(是/否)

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是:

std::unique_ptr<MyType> SomeClass::getAt1(int i) {
  return std::move(cache[i]);
}
MyType* SomeClass::getAt2(int i) {
  return cache[i].get();
}

如果输入1或2,则不会输入循环,因此您的方法仅返回而游戏结束。

如果输入无效值,例如3,则将输入循环。然后,系统会再次提示您输入答案,并且从那里开始会更好。

一种快速的技巧是只需先进行 try: location_old = int(input("Where do want to go?\n1-Through the jungle\n2-To Around the island\nplease\ choose (1/2)")) except Exception: pass while location_old != 1 and location_old != 2: # Game continues 而不是询问用户。

更好的解决方案是编写一种方法,询问您一个问题并再次提示,直到您提供有效的答复为止。这样一来,您不必在各处添加循环,从而使代码更易于编写(和读取)。