TransWikia.com

Vampiric text-based adventure game

Code Review Asked on October 27, 2021

Here is my vampire based text adventure game.
I recently started learning Python and Googled for some good projects to practice with. I wrote all this code myself after checking out some examples on the internet.

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. nnRip')
    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? nn RIP')
        option_death()
    elif choice in answer_B:
        print("This is not hide n'seek nn 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. nn 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? nn 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')

One Answer

Don't ask if the user wants to play the first time

If the user started your game, then of course they want to play it, otherwise they wouldn't have started it to begin with. So don't ask and go to the intro immediately. If they started your game in error, they can always quit it by closing the window, pressing control-C or something like that.

You will overflow the stack, eventually

For every action that does not end the game, you just call another function, but you don't return from a function. This means your call stack will grow indefinitely. With the gigabytes of RAM we have in our computers nowadays, you might not notice this mistake, but on the eight-bitters of the previous century, your game would run out of memory very quickly because of this.

Generally, you want to have a main loop that handles the input, and that advances the state based on your input. For example, you could write it like so:

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

The above is just an illustration of the idea. To do it properly, you would probably use an enum for the state, and possibly have a map of states to functions so you can simplify the whole loop. For example:

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

Consider not sleeping before showing possible choices

The calls to time.sleep(1) are not needed for the game, and just make it so the player has to wait unncessarily before being able to read the choices. I would just avoid this.

The game is quite rude

It might seem funny to you, but if I was a player, and accidentally typed in the wrong character, and got told that I was an idiot, my appreciation of the game would drop significantly. Also, being told that garlic doesn't work because that's just a story book thing is very annoying, since the whole concept of vampires comes from a story book to begin with.

Answered by G. Sliepen on October 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP