Beruflich Dokumente
Kultur Dokumente
TXT
#CST205 Lab 13_probblem1
#Team 1
#Brian Sheridan, Kevin Bentley, Craig Calvert, Samuel Pearce
import os
'''
Media resources:
Icons: https://icomoon.io/
Sounds: http://soundbible.com
Photos: https://www.flickr.com/photos/shantideva/2580811889/in/photostream/
'''
# The root path for all of the media files.
rootFilePathName = os.path.dirname(__file__)
# The main picture for the app. This will never be reassigned
mainWindow = makeEmptyPicture(480,560,makeColor(255,255,255))
show(mainWindow)
# Greenscreen rendering
def greenScreen(backGround, foreGround, targetX, targetY):
greenScreen = makeColor(0,255,0)
shadow = makeColor(33,29,22)
for x in range(0, getWidth(foreGround)-1):
for y in range(0, getHeight(foreGround)-1):
picPixel = getPixel(foreGround,x, y)
newPixel = getPixel(backGround,targetX + x ,targetY + y)
color = getColor(picPixel)
Page 1
team1_final .TXT
if distance(color,greenScreen) > 140:
setColor(newPixel,color)
return backGround
# All game states will be stored here
class GameState:
def __init__(self):
self.bottleOnScale = False
self.playerWon = False
self.playerDied = False
self.bottleOpen = False
self.drawerOpen = False
class Player:
def __init__(self,name):
self.inventory = []
self.name = name
def getItem(self, itemName):
for item in self.inventory:
if(item.name.lower()==itemName.lower()):
return item
return None
class InventoryItem:
def __init__(self,name,iconFile, description):
global rootFilePathName;
# The name to display
self.name = name
# This will show when the user picks it up
self.description = description
# The file name for the icon
self.iconFile = iconFile
if(iconFile != None):
# JES Picture object
self.iconPicture =
makePicture(os.path.join(rootFilePathName,"inventory_icons",iconFile))
else:
self.iconPicture = None
self.actionFunction = None
# The Room class keeps track of the state of each room as well as references to
nearby rooms
class Room:
#import console
def getGameInput(msg):
inputStr = requestString(msg)
#input = raw_input(msg)
#inputStr = input()
return inputStr
def displayGameText(msg):
#print(msg)
showInformation(msg)
def getName():
inputStr = requestString("What is your name?")
return inputStr
def clip(source,start,end):
newSound = makeEmptySound(end-start,int(getSamplingRate(source)))
newPosition = 0
for i in range(start,end):
sample = getSampleObjectAt(source,i)
newSample = getSampleObjectAt(newSound,newPosition)
sampleValue = getSampleValue(sample)
setSampleValue(newSample,sampleValue)
newPosition = newPosition + 1
return newSound
def getFoolSound():
theSound = makeSound(os.path.join(rootFilePathName,"sounds","LOTR Fool.wav"))
clippedSound = clip(theSound,2679256,2703500)
clippedSound.play()
if(len(commands)>1):
if((commands[0]=='examine' or commands[0]=='view') and commands[1]=='desk'):
showDeskDescription()
return True
if(commands[0]=='open' and (commands[1]=='desk' or commands[1]=='drawer') and
gameState.drawerOpen == False):
gun = InventoryItem("Gun","Gun.jpg","Locked and loaded!")
Page 4
team1_final .TXT
room.items.append(gun)
displayGameText("You open the desk drawer and find an old revolver. Upon
closer inspection you find that it only has two bullets.")
return True
if(len(commands)>1):
if(commands[0]=='look' and commands[1]=='at' and commands[2]=='desk'):
showDeskDescription()
return True
return None
item = room.getItem(itemName)
if(item==None):
displayGameText("I can't find that to pick up.")
return True
else:
# Remove item from the room and add it to the inventorylook
player.inventory.append(item)
room.items.remove(item)
inventoryIcons(player.inventory,mainWindow)
repaint(mainWindow)
if(item.description != None):
displayGameText(item.description)
else:
displayGameText("You now have the " + item.name + " in your inventory.")
return True
return None
# This function builds a single string from the previously parsed commands.
# This allows us to parse, for example, the first two words as commands, and
# then treat the rest of the text as a single 'object'. E.g. take Typewriter
# Keys vs pick up Typewriter Keys
def reassembleCommand(commands,index):
command = ""
for i in range(index,len(commands)):
if(i!=index):
command += " "
command += commands[i]
return command
# This is the main game entry point that loops while the game is being played.
def play():
global rootFilePathName;
validDirections =
["north","n","south","s","east","e","west","w","up","u","down","d","u","under"]
basement = Room("Basement",
"The room is long and narrow with a doorway located at the north
end. "
Page 7
team1_final .TXT
"To your right the wall looks like something from an old
mausoleum. "
"One of the doors has been pried off and you see a pair of feet
"
"sticking out of where the door once was.",
"basement.jpg")
basement.roomActionFunction = basementActions
lowerStairway = Room("Stairway",
"There is a stairway leading up. To the east is a doorway
leading to an outdoor courtyard.",
"stairway_courtyard.JPG")
courtyard = Room("Courtyard",
"As you step through the doorway to the courtyard you fail to
see the man step out of "
"the second courtyard doorway to your right. The ground rushes
up toward your face "
"shortly after the sharp pain in your back. The ground ends up
being the last thing you see...ever.",
"death_stairway_courtyard.png")
foyer = Room("Foyer",
"Straight ahead of you is what appears to be the front door to the
house. To the left and to "
"the right of you are doorways to other rooms.",
"foyer.JPG")
foyer.roomActionFunction = foyerActions
office = Room("Office",
"You are standing in what looks like it used to be a personal
office. Pushed against one of "
"the walls is a desk with an old typewriter on it.",
"Office.JPG")
office.roomActionFunction = officeActions
stairway = Room("Stairway",
"Before you is a stairway leading upstairs. You also notice a
little farther down, just "
"underneath the stairs, is another doorway.",
"stairway.JPG")
basement.addRoom("North",lowerStairway)
lowerStairway.addRoom("South",basement)
lowerStairway.addRoom("East",courtyard)# This is a 1 way journey
lowerStairway.addRoom("Up",foyer)
foyer.addRoom("Down",lowerStairway)
foyer.addRoom("West",office)
office.addRoom("East",foyer)
foyer.addRoom("East",stairway)
stairway.addRoom("West",foyer)
stairway.addRoom("Down",storage)
stairway.addRoom("Under",storage)
storage.addRoom("Up",stairway)
stairway.addRoom("Up", scale)
scale.addRoom("Down",stairway)
scale.addRoom("West",bedroom1)
bedroom1.addRoom("East",scale)
scale.addRoom("East",bedroom2)
bedroom2.addRoom("West",scale)
currentRoom = basement
displayGameText(getInstructions())
playerName = getName()
player = Player(playerName)
nameTag = InventoryItem("Name Tag","NameTag.jpg","This is a light blue fabric
with a name tag pinned to it. The name tag says 'Brandon.'")#"nametag.png")
player.inventory.append(nameTag)
if(currentRoom.picture!=None):
pyCopy(currentRoom.picture,mainWindow,0,0)
inventoryIcons(player.inventory,mainWindow)
repaint(mainWindow)
gameState = GameState()
storageDeathPicture =
makePicture(os.path.join(rootFilePathName,"room_pictures","death_storage.jpg"))
# Game loop, exits on the following conditions:
# Game over (lost or won)
# User typed exit
while(True):
if(currentRoom == storage and gameState.playerDied == True):
deathSound =
makeSound(os.path.join(rootFilePathName,"sounds","deathSound.wav"))
deathSound.play()
pyCopy(storageDeathPicture,mainWindow,0,0)
repaint(mainWindow)
displayGameText(courtyard.description + "\nYou have died.")
if(currentRoom == courtyard):
deathSound =
makeSound(os.path.join(rootFilePathName,"sounds","deathSound.wav"))
deathSound.play()
displayGameText(courtyard.description + "\nYou have died.")
break
if(gameState.playerDied):
displayGameText("You lost the game.")
break
# Check for win condition
if(gameState.playerWon):
displayGameText("You won the game!")
break
print('You are in the ' + currentRoom.name + ".")
input = getGameInput("\nWhat do you do? ")
print(input)
if(input==None):
continue
input = input.lower()
# All of the words will be split by space characters for parsing.
commands = input.split(" ")
if(len(commands)==0):
continue
if(len(currentRoom.items)):
lookString += "You see the following items in this room:\n"
for roomItem in currentRoom.items:
lookString += "\t" + roomItem.name + "\n"
lookString += "\n"
if(len(currentRoom.exits)>0):
lookString += "You can go in the following directions:\n"
for direction in currentRoom.exits.keys():
if(len(direction)>1):
lookString += ("\t" + direction) + "\n"
displayGameText(lookString)
continue
if(len(commands)>2 and commands[1]=="at"):
itemName = reassembleCommand(commands,2)
item = currentRoom.getItem(itemName)
if(item==None):
item = player.getItem(itemName)
if(item!=None):
displayGameText(item.description)
continue
else:
displayGameText("You don't see that.")
Page 12
team1_final .TXT
else:
displayGameText("You can't go that way!")
else:
displayGameText("I don't know how to do that. (Try 'look')")
splashscreen =
makePicture(os.path.join(rootFilePathName,"room_pictures","splashscreen.png"))
pyCopy(splashscreen,mainWindow,0,0)
repaint(mainWindow)
Page 15
team1_final .TXT
play()
Page 16