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

吸血鬼文字冒险游戏
EN

Code Review用户
提问于 2020-07-20 11:32:02
回答 1查看 227关注 0票数 6

这是我基于吸血鬼的文字冒险游戏。我最近开始学习Python,并在谷歌上搜索一些好的项目来练习。在互联网上查看了一些例子之后,我自己编写了所有这些代码。

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

#A text based dracula survival game

#possible answers
answer_A = ['A', 'a']
answer_B = ['B', 'b']
answer_C = ['C', 'c']
yes = ['yes', 'Yes', 'y', 'Y']
no = ['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:')
    time.sleep(1)
    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')
    elif choice in answer_B:
        option_castle()
    elif choice in answer_C:
        option_cave()
    else:
        print("That's not an option idiot")
        intro()


def option_castle():
    print('You ran inside the castle and see a glass front cubboard with garlic inside.You hear the Vampire coming,''You will: ')
    time.sleep(1)
    print('''    A. Take the garllic to scare the vampire.
    B. Hide
    C. Escape from backdoor.''')
    choice = input('>>>  ')
    if choice in answer_A:
        print('This is not a story book, what are you doing? Making salad? \n\n RIP')
        option_death()
    elif choice in answer_B:
        print("This is not hide n'seek \n\n RIP" )
        option_death()
    elif choice in answer_C:
        option_abdvilllage()
    else:
        print('Not an option idiot')
        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: ')
    time.sleep(1)
    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.')
    elif choice in answer_B:
        print("Cowards don't get to fight and live. \n\n RIP")
        option_death()
    elif choice in answer_C:
        option_abdvilllage()
    else:
        print('not an option idiot')
        option_cave()


def option_abdvilllage():
    print('You ran towards an abandoned village in the open. The bats are coming faster than before, you will: ')
    time.sleep(1)
    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.')
    elif choice in answer_B:
        print("For real? How can a piece of wood kill an immortal blood sucking human size bat? \n\n RIP")
        option_death()
    elif choice in answer_C:
        option_cave()
    else:
        print('not an option idiot')
        option_abdvilllage()


def option_death():
    choice = input('Do you want to play again? Yes or No  ')
    if choice in yes:
        intro()
    else:
        print('very well')


play = input('Do you want to play? Y or N  ')
if play == 'Y'.lower():
    intro()
else:
    print('very well')
EN

回答 1

Code Review用户

回答已采纳

发布于 2020-07-20 13:43:10

不要问用户是否想第一次播放

如果用户开始你的游戏,那么他们当然想玩它,否则他们就不会一开始就启动它。所以,不要问,并立即去介绍。如果他们开始你的游戏错误,他们总是可以退出它关闭窗口,按下控制-C或类似的东西。

,您将溢出堆栈,最终导致

溢出。

对于没有结束游戏的每一个动作,您只需调用另一个函数,但不会从一个函数返回。这意味着调用堆栈将无限期地增长。现在我们的电脑里有千兆字节的内存,你可能不会注意到这个错误,但是在上个世纪的八位内存中,你的游戏会因为这个而很快耗尽内存。

通常,您希望有一个主循环来处理输入,并根据您的输入来提升状态。例如,您可以这样编写它:

代码语言:javascript
复制
def intro():
    print('You wake up...')
    ...
    choice = input('>>> ')
    if choice in answer_A:
        print("...");
        return "game_over"
    elif choice in answer_B:
        return "castle"
    ...

def main_loop():
    state = "intro"

    while state != "end":
        if state == "intro":
            state = intro()
        elif state == "castle":
            state = option_castle()
        ...
        elif state == "game_over":
            again = input('Do you want to play again? Y or N  ')
            if again in yes:
                state = "intro"

main_loop()

以上只是这个想法的一个例子。要正确地执行此操作,您可能会使用一个enum作为状态,并且可能有一个状态映射到函数,这样就可以简化整个循环。例如:

代码语言:javascript
复制
from enum import Enum

class State(Enum):
    INTRO = 1
    CASTLE = 2
    ...
    GAME_OVER = -1
    END = -2

def intro():
    ...

def option_castle():
    ...

def game_over():
    print('Game over.')
    again = input('Do you want to play again? Y or N  ')
    if again in yes:
        return State.INTRO
    else
        return State.QUIT

scenarios = {
    State.INTRO: intro,
    State.CASTLE: option_castle,
    ...
    State.GAME_OVER: game_over,
}

def main_loop():
    state = State.INTRO

    while state != State.END:
        state = scenarios[state]()

main_loop();

在显示可能的选择之前考虑不睡觉,

time.sleep(1)的调用并不是游戏所需要的,所以玩家必须等待才能读到选择。我只想避免这种事。

游戏很粗鲁,

这对你来说可能很有趣,但是如果我是一个玩家,不小心输入了错误的角色,然后被告知我是个白痴,我对这个游戏的欣赏就会大大下降。另外,被告知大蒜不起作用,因为这只是一个故事书的事情是非常烦人的,因为吸血鬼的整个概念是从故事书开始。

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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