简体   繁体   English

这是一个基于文本的游戏,但它不起作用

[英]this is a textbased game but its not working

This is supposed to become a text based game but for some reason my code repeats itself after the first question这应该成为一个基于文本的游戏,但由于某种原因,我的代码在第一个问题之后重复了

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()

put in mind that your enemy is in the island to and looking for the treasure Where do want to go?记住,你的敌人是在岛上寻找宝藏想去哪里? 1-Through the jungle 2-To Around the island please choose (1/2)1 Do you want to play again please choose(y/n) 1-穿越丛林 2-到环岛请选择(1/2)1 想再玩请选择(y/n)

This happens because of this part:发生这种情况是因为这部分:

    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

If you enter 1 or 2, then the loop will not be entered, so your method just returns and the game ends.如果您输入 1 或 2,则不会进入循环,因此您的方法只会返回并且游戏结束。

If you enter an invalid value, such as 3, then the loop will be entered.如果输入无效值,例如 3,则将进入循环。 You'll then be re-prompted for your answer, and it'll work better from there.然后,系统会重新提示您提供答案,然后它会更好地工作。

A quick hack would be to simply do location_old=-1 first instead of asking the user.一个快速的技巧是先简单地做location_old=-1而不是询问用户。

A better solution would be to write a method that asks you a question and re-prompts until you give a valid reply.更好的解决方案是编写一种方法,该方法会向您提出问题并重新提示,直到您给出有效答复为止。 That way you don't have to add loops everywhere, making the code easier to write (and read).这样您就不必到处添加循环,从而使代码更易于编写(和阅读)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM