Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
262 views9 pages

Finalproject

This document contains code for a text-based carnival game. It defines functions for initializing the game including rooms, items, and sounds. It also contains the main game loop that directs the user through different rooms based on their input and allows them to grab items or read clues. The objective is to make it to the secret rollercoaster room by finding coins, visiting the fortune teller, and discovering the secret entrance.

Uploaded by

api-355553261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views9 pages

Finalproject

This document contains code for a text-based carnival game. It defines functions for initializing the game including rooms, items, and sounds. It also contains the main game loop that directs the user through different rooms based on their input and allows them to grab items or read clues. The objective is to make it to the secret rollercoaster room by finding coins, visiting the fortune teller, and discovering the secret entrance.

Uploaded by

api-355553261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FinalProject.

py 4/23/17, 4:41 AM

# Lilia Chakarian
# Alan Ayoub
# Pedro Ramos

# Final Project

# Carnival Game
inventory =[]
items ={}
usersName = ""

# Sound declaration
localPath = ""
beginning = ()

#SOUND function definition


def picSound(localPath):

# global variables for each sound


global beginning
global youlose
global CongratsYouWin
global Chaching2
global FortuneTeller
global RollerCoaster2

# Carnival music
beginning = os.path.abspath(localPath + "/Final.wav")
beginning = makeSound(beginning)

# When the user lose


youlose = os.path.abspath(localPath + "/YouLoseWithClown.wav")
youlose = makeSound(youlose)

# Sound plays when the user wins


CongratsYouWin = os.path.abspath(localPath + "/CongratsYouWin.wav")
CongratsYouWin = makeSound(CongratsYouWin)

# Sound plays when the user adds an item to inventory


Chaching2 = os.path.abspath(localPath + "/Chaching2.wav")
Chaching2 = makeSound(Chaching2)

# Sound plays when the user has items in inventory and enters the fortune
teller room
FortuneTeller = os.path.abspath(localPath + "/FortuneTeller.wav")
FortuneTeller = makeSound(FortuneTeller)

# Sound plays when the user (wins) entering the secret room
RollerCoaster2 = os.path.abspath(localPath + "/RollerCoaster2.wav")
RollerCoaster2 = makeSound(RollerCoaster2)

Page 1 of 9
FinalProject.py 4/23/17, 4:41 AM

# The game can be played by typing in the game function


def game():
# Constant Variables help anyone who edits the code see the constant
variables easier
DIRECTION = 0
ROOM_NAME = 1

# We had to use global variables because inventory and items need to be


available in all rooms
global inventory
global items
global usersName

userInput = ""
currentLocation = "entrance" # The user begins here before entering the
carnival
secretOpened = false
inventory = []
items = initializeItems()

# Initialize rooms contains the definition for every room used


rooms = initializeRooms()

# Initialize Sound
picSound(localPath)

# Prints at the very beginning when the user starts the game
printNow("---- Welcome to the Carnival!!! ----")
printNow("There's lots of fun and games at the Carnival, but don't let the
clowns scare you away.")
printNow("To win the game, your'e objective is to ride the roller coaster."
)
printNow("But first, you must find the gold coins, visit the fortune
teller, and then find the secret entrance!")
printNow("Depnding on where you are within the carnival, you will given
options for which direction you can go.")
printNow("You can go north, south, east or west by typing that direction
(n,s,e,w).")
printNow("Type 'help' to see the game's instructions again.")
printNow("Type 'exit' to escape the carnival (Uh oh, Do clowns scare you?)"
)
printNow("")

# Help Menu shows up as a pop up menu


showHelp()

# The user is promnpted to enter their name or character name


usersName = requestString("Enter your name (or name your character): ")
showInformation("OK " + usersName + ", Let's enter the Carnival!")
printNow("")

# Carnival music sound plays


play(beginning)

# While loop continues until the user enters 'exit'

Page 2 of 9
FinalProject.py 4/23/17, 4:41 AM

while userInput!="exit":
roomDescription(currentLocation)
# specific directional options are allowed depending on the users
current room
roomOptions = rooms[currentLocation]
# this loop makes decisions based on the users input
userInput = requestString("Choose a direction (n,s,e,w)")
userInput = parse(userInput)
if len(userInput)>0:
if(userInput[:4]=='open'):
if currentLocation!="fortuneteller":
printNow("cannot open anything")
else:
userInput = openSecretDoor()
rooms["fortuneteller"] += [["e","secret room"]]
if(userInput=="n" or userInput=="s" or userInput== "e" or userInput
=="w"):
validDirection = false
for i in range(0,len(roomOptions)):
# handles validation for user input
if userInput == roomOptions[i][DIRECTION]:
validDirection = true
currentLocation = roomOptions[i][ROOM_NAME]
if not validDirection:
printNow("Sorry, can't go in that direction")
elif(userInput[:4]=='grab'):
if grabItem(currentLocation,userInput[5:]):
play(Chaching2)
showInformation(userInput[5:] + " have been added to the
inventory")
else:
printNow("can't grab item")
elif(userInput[:4]=='read'):
if(userInput[5:]== "nothing"):
printNow("can't read item")
if(currentLocation == "secret room"):
userInput = winGame()

else:
printNow("Sorry " + usersName + ", That's not a valid input. Please
try again. (Use help if you need to)")
printNow("")
# Stops the Carnival Music when the user exits the game
stopPlaying(beginning)
# Stops the Fortune Teller sound when the user exits the game
stopPlaying(FortuneTeller)
printNow("Buuuhh Byyyyyyyeeeee!!!!")

# Initializes rooms and location options


def initializeRooms():
entrance = [["n","circustent"]] #can only go north
circustent = [["n","ferriswheel"],["s","entrance"],["e","magicshow"]] #can
go north, south, and east
carnivalgames = [["e","ferriswheel"]] #can only go east
ferriswheel = [["s","circustent"],["w","carnivalgames"]] #can go south and
west

Page 3 of 9
FinalProject.py 4/23/17, 4:41 AM

magicshow = [["n","ticketbooth"],["w","circustent"],["e","fortuneteller"]]
#can go north, west, and east
ticketbooth = [["s","magicshow"]] #can only go south
fortuneteller = [["w","magicshow"]] #can only go west
secretroom=[["w","fortuneteller"]] #can only go west
rooms = {"ferriswheel":ferriswheel, "entrance":entrance, "circustent":
circustent, "carnivalgames":carnivalgames,
"magicshow":magicshow,"ticketbooth":ticketbooth,"fortuneteller":
fortuneteller,"secret room":secretroom}
return rooms

def initializeItems():
items = {"coins":"ferriswheel","tickets":"ticketbooth",
"cheatbook":"magicshow"}
return items

# Validates the users inputs


def parse(userInput):
if userInput is not None and userInput!="" :
userInput = userInput.lower()
userInputTokens = userInput.split()

if (userInput == "n" or userInput == "s" or userInput == "e" or userInput


== "w"):
return userInput
elif (userInput == "help"):
showHelp()
return "help"
elif (userInput == "exit"):
return userInput
elif (userInput=="i"):
showInventory( inventory)
return "i"
elif (userInputTokens[0] == "grab"):
if len(userInputTokens)<2:
return "grab nothing"
elif userInputTokens[1]=="up":
if len(userInputTokens)<3:
return "grab nothing"
else:
return "grab " + userInputTokens[2]
else:
return "grab " + userInputTokens[1]
elif (userInputTokens[0] == "read"):
if len(userInputTokens)<2:
return "read nothing"
else:
if readItem(userInput[5:]):
return "read something"
else:
return "read nothing"
elif (userInputTokens[0] == "open"):
if len(userInputTokens)<2:
return "open nothing"
else:
if(userInput[5:]=="secret room"):

Page 4 of 9
FinalProject.py 4/23/17, 4:41 AM

return "open secret door"


else:
return "open nothing"
else:
return ""
else:
return ""

# Help menu to guide the user with instructions and input information
def showHelp():
printNow("")
showInformation("You are viewing the Help Menu\n"
"\n"
"---- Welcome to the Carnival Game!!! ----\n"
"You will have different options depending on which part of
the carnival you are in.\n"
"You can to go north(n), south(s), east(e) or west(w) by
typing that direction.\n"
"Your goal is to get to the secret door so that you can
ride the roller coaster"
"You must follow steps in sequence or else the a creepy
clown will kill you if you don't do the following in
this exact order:"
"First step, find the gold coins and grab them by typing in
'grab coins'. \n"
"Second step, you will need to visit the fortune teller for
instructions"
"There is a room that has a secret door that can be opened
by typing 'open secret door'. \n"
"Type inventory or 'i' to display your inventory.\n"
"There is a cheatbook that can be read by typing in 'open
cheatbook', but only after you 'grab cheatbook'. \n"
"You can type 'help' to re-display this introduction.\n"
"You can type 'exit' to quit the program at any time.\n")

# Shows the user what is in their inventory


def showInventory(inventory):
printNow("Inventory:")
printNow(inventory)

# Each room has a description and it is defined here


def roomDescription(room):
if room == "entrance":
printNow("CARNIVAL ENTRANCE")
insertBackground(room)
printNow("It's a creepy night, full moon, and your'e standing outside
of the Carnival staring at the red and white tent.")
printNow("You can go north (into the circus tent) ... if you're not
scared of creepy clowns!")
if room == "circustent":
printNow("CIRCUS TENT")
insertBackground(room)
printNow("You are inside of the circus tent.")
printNow("It's scary in here and nobody is inside to watch the circus
acts.")

Page 5 of 9
FinalProject.py 4/23/17, 4:41 AM

printNow("This is straight out of a horror film, is this real?")


printNow("You can go through the north opening (to the Ferris Wheel) or
enter the opening to the east (to see the Magic Show).")
printNow("You can also exit south where you began (at the Carnival
Entrance).")
if room == "carnivalgames":
printNow("CARNIVAL GAMES")
insertBackground(room)
printNow("You are in the Carnival Games.")
printNow("Maybe nows a good time to use your tickets to play a game?
Why not! Go for it!")
printNow("Oh! There's only an entry to the east (that takes you back to
the Ferris Wheel).")
if room == "ferriswheel":
printNow("FERRIS WHEEL")
insertBackground(room)
printNow("You are near the Ferris Wheel.")
printNow("But, you'd rather use your tickets on the Roller Coaster.
That sounds funner than using your tickets on the Ferris Wheel,
right?")
if "coins" in items:
printNow("The Ferris Wheel goes round and round! Round and round!
Round and round....")
else:
printNow("Maybe it's a good idea to put those gold coins to use, what
do you say?")
printNow("Try and find the fortune teller and insert your gold coins
there.")
printNow("There's no use for your golden coin here.")
printNow("Maybe the fortune teller has some advice for you?")
if room == "magicshow":
printNow("MAGIC SHOW")
insertBackground(room)
printNow("You are watching the Magic Show.")
printNow("Why is everything so creepy in this Carnival? Clows, dark
performers....kinda scary isn't it?")
if "cheatbook" in items:
printNow("A cheatbook is on the floor.")
printNow("You can go back to where you started (into the Circus Tent),
You can go north (into the Ticket Booth), and you can go east (to
visit the Fortune Teller).")
if room == "ticketbooth":
printNow("TICKET BOOTH")
insertBackground(room)
printNow("You are in a neglected Ticket Booth that hasn't been
maintenced in years.")
printNow("The mirror is shattered and the smell inside is aweful.")
printNow("There is blood near the sink.")
printNow("There is an entry to the south (into the magicshow).")
if room == "fortuneteller":
printNow("FORTUNE TELLER")
insertBackground(room)
if "coins" in inventory:
stopPlaying(beginning)
stopPlaying(FortuneTeller)
play(FortuneTeller)

Page 6 of 9
FinalProject.py 4/23/17, 4:41 AM

printNow("I see you have some golden coins for me....")


printNow("I have something for you too.")
printNow("I have a feeling that you want to ride the roller coaster,
but you don't know where the secret door is...")
printNow("Advice isn't the only thing I can posses, I can open doors
for people...")
else:
printNow("You walk into a dark and deserted Fortune Teller, does that
machine even work?")
printNow("There's nothing to do here.")
printNow("There is an entry into the west (back into the Magic
Show).")
if room == "secret room":
printNow("SECRET ROOM")
insertBackground(room)
printNow("You found the roller coaster entrance!")

# Function to allow the user to grab an item and also to remove the item from
availabilty.
def grabItem(room, item):
isItemTaken = false
global inventory
if item in items:
if items[item] == room:
inventory = inventory + [item]
isItemTaken= true
del items[item]
return isItemTaken

# This is how we read the item into inventory


def readItem(item):
global inventory
if item in inventory:
if item == "cheatbook":
printNow("How to win the game")
printNow("- 1. Type in 'grab coins' when you are at the ferris wheel.")
printNow("- 2. Go visit the Fortune Teller with your gold coins.")
printNow("- 3. Type in 'open secret door' when you are visiting the
Fortune Teller.")
printNow("- 4. After entering the Secret room, you must type e for
east.")
printNow("- 5. Congratulations, you are a winner and you get to ride
the roller coaster.")
return true
return false

# Secret Door function, you must have the gold coins and already visited the
Fortune Teller to enter the
# secret room and make the creepy clown evaporate
def openSecretDoor():
global inventory
if "coins" in inventory:
printNow("A creepy clown pops out and then evaporates....")
printNow("The fortune teller's powers saved you and now there's nothing
to worry about, creep free zone.")

Page 7 of 9
FinalProject.py 4/23/17, 4:41 AM

printNow("What would happen if you went east? I'd try that if I were
you...")
printNow("")
userInput = requestString("Choose a direction")
userInput = parse(userInput)
return userInput
else:
loseGame()
return "exit"

# Show the picture of the room you are in


def insertBackground(room):

# insert background image files for rooms

entrancepic = makePicture('C:/Picture/Carnival-Entrance.png')
circustentpic = makePicture('C:/Picture/CircusTent.png')
carnivalgamespic = makePicture('C:/Picture/Carnival-Games.png')
ferriswheelpic = makePicture('C:/Picture/Ferris-Wheel.png')
magicshowpic = makePicture('C:/Picture/Magic-Show.png')
ticketboothpic= makePicture('C:/Picture/TicketBooth.png')
fortunetellerpic= makePicture('C:/Picture/Fortune-Teller.png')
secretroompic = makePicture('C:/Picture/Roller-Coaster.png')

if room == "entrance":
background = entrancepic
repaint(background)
if room == "circustent":
background = circustentpic
repaint(background)
if room == "carnivalgames":
background = carnivalgamespic
repaint(background)
if room == "ferriswheel":
background = ferriswheelpic
repaint(background)
if room == "magicshow":
background = magicshowpic
repaint(background)
if room == "ticketbooth":
background = ticketboothpic
repaint(background)
if room == "fortuneteller":
background = fortunetellerpic
repaint(background)
if room == "secret room":
background = secretroompic
repaint(background)

# If the user enters the secret room without the golden coins, they will lose
def loseGame():
play(youlose)
showInformation("Uh oh! Sorry " + usersName + " The creepy clown got you!
Hahahahahahahahaha.\n"
"You should have won the carnival game, gotten the golden

Page 8 of 9
FinalProject.py 4/23/17, 4:41 AM

coin, and then visited the fortune teller! Try again!!"


)

# This is what happens when the user walks into the secret room with the golden
coins, they win!
def winGame():
stopPlaying(FortuneTeller)
play(CongratsYouWin)
play(RollerCoaster2)
roomDescription("secret room")
showInformation("Good Job! " + usersName + "!\n"
"You win and now you get to ride the secret roller coaster!
Mission accomplished!\n"
"The game is over!\n"
"No clown nightmeres for you!\n")
return "exit"

Page 9 of 9

You might also like