首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跟进:基于吸血鬼文字的冒险游戏

跟进:基于吸血鬼文字的冒险游戏
EN

Code Review用户
提问于 2020-08-01 08:30:45
回答 1查看 79关注 0票数 5

这是我第二次尝试这个游戏,我从这里那里学到了关于最后一个游戏的建议和建议。

我学了一些关于如何避免内存堆栈溢出和写一个更好的故事的技巧。建议我如何在总体上改进我的代码?谢谢。

代码语言:javascript
复制
import time #added to enable pause

#A text based vampire survival game

#possible answers
answer_A = ['A', 'a']
answer_B = ['B', 'b']
answer_C = ['C', 'c']
y = ['yes', 'Yes', 'y', 'Y']
n = ['no', 'No', 'n', 'N']


#story begins here
def intro():
    print('You wake up in front of a castle, you have no idea where you are or why you are here.'
    'You suddenly see a colony of bats fly away.' 'You turn back and see a vampire figure appear out of nowhere. You will:')
    print('''    A. Ask for direction.
    B. Run inside the castle.
    C. Run to the cave.''')
    choice = input('>>>  ')
    if choice in answer_A:
        print('\n Well you dont ask vampires for directions. \n\nRip')
        return "Gameover"
    elif choice in answer_B:
        return "castle"
    elif choice in answer_C:
        return "cave"
    else:
        print("Invalid input")
        time.sleep(1)
        intro()


def option_castle():
    print('You ran inside the castle and see a glass front cubbord with garlic inside.You hear the Vampire coming,''You will: ')
    print('''    A. Take the garllic to scare the vampire.
    B. Hide
    C. Escape from backdoor.''')
    choice = input('>>>  ')
    if choice in answer_A:
        print("The garlic did stopped the vampire but it did not stopped it's blood thirsty bats.\n\n RIP")
        return "Gameover"
    elif choice in answer_B:
        print("This is not hide n'seek \n\n RIP" )
        return "Gameover"
    elif choice in answer_C:
        return "village"
    else:
        print('Invalid input')
        time.sleep(1)
        option_castle()


def option_cave():
    print('You ran inside a dark cave, you were not sure if its a good idea or not but in there you see a shiny silver dagger.' 'Hurry bats are coming: ')
    print('''    A. You pick up the dagger and fight.
    B. You pick up the dagger and hide.
    C. You run.''')
    choice = input('>>>  ')
    if choice in answer_A:
        print('You picked the silver dagger and stood there like a fearsome warrior. The vampire attacked you but you were cunning and avoiding its attack stabbed the vampire right in its heart. Well done vampire slayer, you may live.')
        return "Gameover"
    elif choice in answer_B:
        print("Cowards don't get to fight and live. \n\n RIP")
        return "Gameover"
    elif choice in answer_C:
        return "village"
    else:
        print("Invalid input")
        time.sleep(1)
        option_cave()


def option_abdvilllage():
    print('You ran towards an abandoned village in the open. The bats are coming faster than before, you will: ')
    print('''    A. Hide 
    B. Pick a wood to stab the vampire
    C. Enter the cave''')
    choice = input('>>>  ')
    if choice in answer_A:
        print('You hid in a hut and well it worked, you were lucky and the sun rose killing the vampire. You were a coward but a lucky one.')
        return "Gameover"
    elif choice in answer_B:
        print("You were ready to stab the vampire with the wood but neither you were fast enough nor the wood was pointed enough. \n\n RIP")
        return "Gameover"
    elif choice in answer_C:
        return "cave"
    else:
        print('Invalid Input')
        time.sleep(1)
        option_abdvilllage()

def main_loop():
    state = "intro"

    while state != "end":
        if state == "intro":
            state = intro()
        elif state == "castle":
            state = option_castle()
        elif state == "village":
            state = option_abdvilllage()
        elif state == "cave":
            state = option_cave()
        elif state == "Gameover":
            again = input("\nDo you want to play again? (Y or n)").lower()
            if again in y:
                state = "intro"
            else:
                print('\nvery well')
                break

def play(): 
    start = input('Do you want to play? Y or N  ')
    if start in y:
        main_loop()
    elif start in n:
        print('very well')
    else:
        print('wrong input')
        time.sleep(1)
        play()

play()
EN

回答 1

Code Review用户

回答已采纳

发布于 2020-08-01 12:24:54

注:这些是我个人的意见,每当我写到你应该做什么的时候,就把它当作“我认为你应该做的”。虽然我通常不这样做,但我不会给你解决我将要指出的“问题”的方法,似乎你很擅长自己解决问题。不过,如果你不知道该怎么问的话。

阳性

  1. 我真的很喜欢像answer_A这样的变量。
  2. 函数的命名与我所期望的一样,当我读到名称时-我知道发生了什么。
  3. 变量名也是如此,大多数变量都有正确的名称。
  4. 您的代码是非常可读的,我甚至不需要有任何解释来理解它正在做什么-保持它!

Extract更多的函数

  1. 以下内容可以概括为一个单独的函数,因此,对于其他主要函数,您可以具有相同的逻辑。如果选择在answer_A: print(“大蒜确实阻止了吸血鬼,但它没有阻止它的嗜血蝙蝠。\n RIP")返回"Gameover”elif选择在answer_B: print(“这不是隐藏n‘寻求\n\n RIP”)返回"Gameover“elif选择在answer_C:返回”村庄“:打印(’无效输入‘)time.sleep(1) option_castle()
  2. 打印选项也可以泛化--不仅可以简化代码,而且可以轻松地扩展选项数量--阅读有关参数的内容。(‘A.拿着那个花环去吓唬吸血鬼。B.隐藏C.逃离后门。‘’)

变量应该将"Gameover""intro""cave"等提取到变量中,以便以后可以轻松地转换/重构它。

概括

您可能希望提取读取用户输入并将输出写入单独的类/函数(类更可取)。这将使您有机会将您的游戏移植到UI,而不是控制台。

nits

  1. 通常,当您想直接运行脚本( if __name__ == "__main__" )时,使用这里(参见C24)是一个很好的实践。
  2. 可以更好地命名变量yx,以指示它们包含什么。
  3. 1中的time.sleep(1)提取为变量。您可能希望稍后更改它,如果您有更多的功能,这将是烦人的。
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/247337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档