Programming Finals

My Cumulative Program: Part 3

This program focuses on a criminal’s journey through the cosmos, all while trying to escape the people he stole a diamond from. If you would like a more detailed overview of this program, my first post covers it. If you want some technicalities on the program or some highs and lows, my second post addresses that. If you are more into the outputs of the program and what it looks like when it’s running, my fourth post covers that. If you would like to see and download the files needed to run this program on a mac, I would suggest checking out this file.

"""
Program: Choose your Own Adventure
Author: Easton Cooper
First Made: 12/5/19
Last Updated: 12/5/19

I will give the user instructions. I will give the user the
option to stick with the given name of John, or switch it up
to a name of their choosing. I will then start asking them questions 
and will play sad/frightening music if their make a fatal decision. 
Assuming they haven’t died, at around 6-8 questions I will tell
them their end result and the game will end. If they have made a
fatal decision, I will tell them right away and the game will end.
I will then ask them if they would like to play again. If they
choose to, I will not show them the instructions again. This program
will work with whole number answers, and the answers must be one of
the numbers to the answers provided. This program will not work with
any non-number input from the user with one exception, the name.
Though some answers with this program will not work, they will not
break the program.

Degree Of Difficulty:
[x] Allow the user to play again (+1pt)

[x] If the user wants to play again, don’t show the instructions (+2pts)

[x] Add more questions (origionally was 3, now asking 5) (+2pts)

[x] No input can break the program (+3pts)

[x] Allow the user to name the characters (could be risky depending on how evil the user is) (+1pt)

[x] Put music into your program (+1pts)
"""

import random
import sys
import time
import pyglet

def main():
    give_instructions()
    doAnother = 'yes'
    while doAnother.lower().strip() == 'yes':
        name = get_name()
        crossroads(name)
        doAnother = do_again()

def get_name():
    """gets a new name for the user if they want it."""
    sprint('')
    sprint("Hold on a second, you don't remember your own name, do you. ")
    sprint("Your name was John, but I'll let you change it since you do not remember it.")
    name = input('New name: ')
    while name == ' ' or name == '':
        name = input("I can't pronounce that, give me another name to call you by: ")
    sprint("Nice, I like it!")
    return name

This is the first of three parts of my source code. I chose to split up my source code into three parts because there are three main sections that make up my source code. This first one sets up the rest of the program with the main, and also holds the get_name procedure. I wanted to include the get_name procedure in this section because it would not fit in well with the next section. In short, this section gives you the program notes at the very top, which gives credit to the author of the program (me), and when it was first made. Next we have some notes on what the program will do and then we move onto some extra features I added into the program. I then include what items you will need to import into this file to play it (notice the “import pyglet” will need to be imported into the terminal as well, see the README  in the zipped file attached). Then we have our main, which shows what big procedures will be running in what order. Lastly, we come to the get_name procedure, which allows the user to pick a new name for their character.

def crossroads(name):
    """gets the first senerio and leads to all others."""
    death = senerio1()
    checkDeath(death)
    death = senerio2()
    checkDeath(death)
    hungry()
    death, dagger, banana = senerio3()
    checkDeath(death)
    senerio4(dagger, banana)
    senerio5()

def senerio1():
    """Puts the user into the first senerio."""
    sprintLine = '***You look down and see a small dagger hanging from your belt.***'
    optionA = 'Sneak around and try to attack to group of cops, hoping to get their weapons.'
    optionB = 'Try to escape with the dagger from the oncoming cops.'
    sprint('')
    sprint(sprintLine)
    orString = 'or'
    sprint(f'\nDo you:\n A. {optionA}\n {orString:^80}\n B. {optionB}')
    senerio1Choice = input(f'\nWill you choose A or B: ')
    if senerio1Choice.upper() != 'A' and senerio1Choice.upper() != 'B':
        #
        # You must punish the user if they don't answer with a valid response 
        #
        sprint("Uh-oh, you didn't do either of the options I gave you and decided to run at the group of cops.")
        #
        # This plays the sound of 5 shots being fired.
        #
        play_sound("./sounds/Shots 5x.wav")
        sprint("The cops shot you and you died.")
        death = True
    elif senerio1Choice.upper() == 'A':
        sprint('Although an effort was made to get new weapons and loot, you happend to fall and cause a loud noise to')
        sprint('be made because you are clumsy, attracting the attnetion of the cops.')
        play_sound("./sounds/Shots 5x.wav")
        sprint('The cops shot you and you died.')
        death = True
    elif senerio1Choice.upper() == 'B':
        sprint('Though it might seem like the coward move, running away saved your life. You also happened to keep')
        sprint('your dagger, which might help in the future...')
        death = False
    return death

def senerio2():
    """Puts the user into the second senerio."""
    sprint('You made it passed your first group of cops. Unfortunately, the game is far from over.')
    sprint("Now let's look around for any other useful tools, but hurry, we don't have much time!")
    sprintLine = f'\n***You hear the footsteps made by boots, the boots of a cop!***'
    optionA = "Hide behind a moldy peice of cardboard leaned up against the wall."
    optionB = "Duck into the boiler room, which currently has a room temperature of 120 degrees"
    orString = 'or'
    sprint(sprintLine)
    sprint(f'\nDo you:\n A. {optionA}\n {orString:^80}\n B. {optionB}')
    senerio2Choice = input(f'\nWill you choose A or B: ')
    if senerio2Choice.upper() != 'A' and senerio2Choice.upper() != 'B':
        sprint('***You decided to just straight up runs at the cops.***')
        sprint('Why??? Why would you do that?? Did you think that would be a good idea? Well...so long partner.')
        play_sound("./sounds/Shots 5x.wav")
        sprint('***The cops shot you and you died.')
        death = True
    elif  senerio2Choice.upper() == 'A':
        sprint("They know you're smart enough to steal a diamond, so they wouldn't stop to check behind a stupid")
        sprint('cardboard box. Nice work!')
        death = False
    elif senerio2Choice.upper() == 'B':
        sprint('Good idea, but you forgot to check to the degree sign. You walked into a 120 degree CELCIUS room, which')
        sprint('is equal to a 248 degree Farenheit room.')
        sprint("***You don't last past 5 minutes in there and you die***")
        death = True
    return death

def hungry():
    """The user's character gets hungry and is given the choice of dropping the dagger for a banana."""
    sprint("Now I don't hear anymore cops nearby, and I don't see them on my radar, so let's take a break.")
    sprint("Alright, good break, now grab your dagger and let's keep going")
    sprint(f'\n***You start to get hungry and see a banana sitting on a shelf a couple yards away***')

def senerio3():
    """Puts the user in the third senerio."""
    sprintLine = f'\n***Your hunger becomes almost unbearable but you know there are cops nearby***'
    optionA = 'Keep the dagger and run away.'
    optionB = 'Grab the delicious banana and run away.'
    orString = 'or'
    sprint("Alright I don't see cops nearby, you can let your gaurd down for a second.")
    sprint(f'\n***Just then, you see a cop burst into the building. He starts shooting at you.***')
    sprint(f'\nDo you:\n A. {optionA}\n {orString:^30}\n B. {optionB}')
    senerio3Choice = input(f'\nWill you choose A or B: ')
    
    if senerio3Choice.upper() != 'A' and senerio3Choice.upper() != 'B':
        sprint("***Your brain freezes and you stare down the cop with an embarassing look on your face***")
        sprint('He just straight up shoots you.')
        play_sound("./sounds/Shots 5x.wav")
        death = True
    elif senerio3Choice.upper() == 'A':
        sprint("Well done! Your quick reflexes help you out here and you don't let the hunger take over.")
        sprint("You manage to make it out of the building in one piece, but you are still hungry.")
        sprint("That's alright, though. If we make it out of this place, we can eat back at home.")
        death = False
        dagger = True
        banana = False
    elif senerio3Choice.upper() == 'B':
        sprint('Alright, we dodged the bullets and made it out of the building alive.')
        sprint("I'm glad you got that banana, I'm not hungry anymore. I just hope that we")
        srpint("don't need that dagger anymore.")
        death = False
        dagger = False
        banana = True
        
    return death, dagger, banana

def senerio4(dagger, banana):
    """Puts the user in the fourth situation."""
    sprint("Oh! I see a camera watching us right now! We should cut the wires so they can't track us!")
    sprint("That will ensure that we can escape! I wonder if we still have that dagger we could cut the")
    sprint('wires with?')
    if dagger == False and banana == True:
        sprint("Gosh dang it! We took the banana (which was a very tasty banana), but left the")
        sprint("dagger behind! You know what, let's just go outside and try to find a way out there.")
    elif dagger == True and banana == False:
        sprint("Yes! Yes! Yes! We have a dagger that can cut the wires!")
        sprint(f'\n***You cut the wires to the recording device with the dagger***')
        sprint("Nice! Now let's head outside since they can't see us anymore.")

def senerio5():
    """Pretty much ends the program becasue it kills the user no matter what."""
    sprint(f'\n***You see 10 cops surround you the minute you step outside***')
    sprint('PUT YOUR HANDS UP NOW!!!!')
    sprint(f'Thank you Agent Brain for your work in this case.\n')
    sprint("""
From Brain to you: Listen bud, It's been nice working with you this long, and your're one
                   of the best criminals in the business, but there's something I have to
                   tell you...I work for the I-CPD, or the Inter-Cosmos Police Division.
                   So basically, I work for the people we've been running from. When the
                   blast from the bank security system hit you, it knocked you out, which
                   is how we found you in the first place. instead of killing you then, we
                   decided to punish you a little harder, so I went ahead and injected myself
                   into your brain. I led you into all these traps and have been continually
                   sending information back to bank headquarters as to where you, or should I
                   say, we, have been.""")
    sprint("It's been fun riding along for the journey, but so long partner...COPS, PUT HIM TO SLEEP!")
    play_sound("./sounds/Shots 5x.wav")
    time.sleep(1)
    play_sound("./sounds/Scream.wav")

This is the second section of my code and also the biggest. It also contains the bulk of my program. This is where most of what is printed to the user is at. In here, I give the user five different scenarios in which they can answer questions that might end their journey! All of these scenarios are are run in the crossroads procedure. This crossroads procedure is run in main, the main that I spoke to in the previous paragraph.

def play_sound(nameOfSoundFile):
    """plays a .wav file."""
    sound = pyglet.media.load(nameOfSoundFile, streaming=False)
    sound.play()

def checkDeath(death):
    """Checks to see if the player died, if so, the program ends."""
    if death == True:
        sys.exit()
    else:
        pass
    
def give_instructions():
    """Starts out our journey through the cosmos."""
    sprint('Hey!')
    sprint('Psst!')
    sprint('You there!')
    sprint('What are you doing?!?')
    sprint("Uh-Oh. The blast must've hit your other head, I mean, me because I am your brain.")
    sprint("Look, we don't have a whole lot of time right now. Ummmmmmmmmm, I'll give you")
    sprint('a brief summary of the past three days: You and I stole the biggest diamond')
    sprint('from the largest security company in the cosmos. They are now hunting us down.')
    sprint('The cops are coming and they have one purpose: disinigrating your body')
    sprint("and erasing you from the universe. I am on your side and will help you through")
    sprint("this journey, but you must make your own decisions. I can give you two options for each decision")
    sprint("but I can't see the future. Make the right move, and you'll escape alive, make a wrong move, and")
    sprint('it could be your last.')
    sprint('')
    sprint('')
    time.sleep(2)
    sprint('Wait! Before you go, I think I see a group of cops just around the corner!')
    
def do_again():
    """Asks the user if they want to play agian."""
    doAnother = input('Do you want to play again?(yes/no) ')
    while doAnother.strip().lower() != 'yes' and doAnother.strip().lower() != 'no':
        doAnother = input('I do not know what that means. Try saying that a different way for me to understand(yes/no): ')
    if doAnother.strip().lower() == 'yes':
        doAnother = 'yes'
    if doAnother.strip().lower() == 'no':
        pass
    return doAnother

#
# sprint makes the lines print in a form that makes them look like someone is printing them at the same time.
#
def sprint(str):
   for c in str + '\n':
     sys.stdout.write(c)
     sys.stdout.flush()
     time.sleep(3./90)

#
# Credits to soundbible.com for the shooting sound effect. http://soundbible.com/tags-death.html
# No changes were made to the sound
#


main()

This is the third and final section of my code. This is all of the “utility-like” procedures that add to or help the program run smoothly. To start things off, we have the play_sound and check_death procedures. The play_sound procedure plays a sound given to it when it is run. The check_death procedure checks to see if the user’s character has died, and if so, ends the program. Next we have the procedure that gives the instructions. You may be thinking, “Oh, why is do you give instructions towards the end? Won’t you need to give them when the program starts?” and in which case, you’d be right. But, thanks to the coding language of Python, you can make a procedure wherever you want and call it and it will work. So all I have to do is call it where I want it and it will run right then. After this, I ask the user if they want to play again, if so, the program starts over. The last procedure in my program is a sprint procedure. Though it may look complicated, it basically just makes the text print out in a way that looks like it is being typed to the user at that very moment.

If you need a better reference for what the check_death procedure does, this image gives a good representation of what it asks the running program: