-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
229 lines (165 loc) · 8.29 KB
/
Model.py
File metadata and controls
229 lines (165 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""
MetaGame: Model Code
This part of the code contains the classes that
provide the major information for the other two documents.
Authors: Cynthia Yong, Sabrina Pereira, Sophie Schaffer
"""
import pygame
import time
pygame.init()
pygame.mixer.init()
#sets the sound that plays when the player gains an item
item = pygame.mixer.Sound('./sound/item.wav')
item.set_volume(.3)
class State():
"""
Stores the information of what has already happened, so the game will
know when to move forward in the game.
level: Keeps track of the level the user is on to advance the game.
decisions: Decisions are made when the user chooses certain answers when prompted.
Their answer will be stored as a string in a dictionary in this class. The dictionary is also
used to replace certain words in the displayed text with the user's choices.
inventory: Stores the .png files for the icons related to the options the user selects.
music: the name of the song file that is currently playing, is changed in levelConditions when level changes
noise: the name of the noise file that plays when a level is changed, default is None. noise is changed in levelConditions when the level changes
"""
def __init__(self, level, decisions = None, inventory = None,music = 'happy.mp3',noise = None):
self.level = level
if decisions == None:
self.decisions = {}
if inventory == None:
self.inventory = []
self.music = music
self.noise = noise
def __str__(self):
return "Level: " + str(self.level)+", Inventory: "+ str(self.inventory) + ", Location: "+str(self.location)
class Stage():
"""
Stores all the information that is needed to create the game screens
name: Refers to the stage
description: Dictionary containing strings of text, where the level the player is on is the key, and the text
to be displayed is the value. The "0" key indicates the text should remain the same regardless of level. Since
the game is mainly textbased, the description is what the users read to understand the game plot. It is displayed
as a textbox onto the screens.
picture: File name for screen background, either a jpg. or png.
buttonMapping: This is a list that contains objects of the MappingObject class. Will be used to
generate buttons that lead the user to the next screen.
backStep: Signals the number of screens back the back button should take you. Default is 1.
clicked: Will check if stage attached to the button has been clicked on. Used to keep track of which
stages/screens have already been viewed.
backStage: This is used to generate the back button and direct the user from the current
screen to the previous screen.
inventorypic: Stores the image corresponding the option the user selects.
"""
def __init__(self, name, description, picture, buttonMapping = None, backStep = 1, inventoryPic = None, clicked = False, backStage = None):
self.name = name
self.description = description
self.picture = picture
self.buttonMapping = buttonMapping
self.backStep = backStep
self.clicked = clicked
self.backStage = backStage
self.inventoryPic = inventoryPic
def __str__(self):
return self.name
def __repr__(self):
return self.name
class MappingObject():
"""
This class connects Stages to each other - each contains the text that the screen buttons display and what stage it leads to.
Each object corresponds to a button generated. For example, if the user is in the HALLWAY (stage),
the MappingObjects will correspond to buttons leading to the KITCHEN and BEDROOM stages.
stageMapTo: What stage the button leads to (KITCHEN, BEDROOM)
levels: A list specifying which levels the button should appear in. Buttons shown will vary depending on level. A 0 indicates the
Button will display in the appropriate screen regardless of level.
backButton: Indicates whether the Button object created from the MappingObject will be a backButton or not.
responseButton: Indicates whether the Button object created from the MappingObject will be a responseButton or not.
"""
def __init__(self, stageMapTo, buttontext,levels,backButton = False, responseButton = False):
self.stageMapTo = stageMapTo
self.buttontext = buttontext
self.levels = levels
self.backButton = backButton
self.responseButton = responseButton
def __str__(self):
return str(self.stageMapTo)
def __repr__(self):
return str(self.stageMapTo)
def goBack(stage):
"""
This function returns the stage that the user will go back to, depending on the backStep number associated with the input stage.
For example:
1 = returns previous stage (DIARY -> BEDROOM)
2 = returns the stage two stages prior (LASAGNA -> KITCHEN, skips COOKBOOK)
"""
if stage.backStep == 1:
return stage.backStage
else:
return goBack(stage.backStage)
def storeDecision(stage,state):
"""
This function takes the input selected by the user and stores it in
the decisions dictionary, using the stage.name of the .
For example, after the user chooses a FLOWER, a new screen will pop up
with a description of the flower they chose. This will add to the state.decisions
dictionary to allow any occurrences of the previous screen's name ("FLOWER") in text
descriptions with the selected user input ("Daisy")
For example:
{"FLOWER" : "Daisy"} will allow any text descriptions to have the word FLOWER replaced with their input, "Daisy".
"""
state.decisions[stage.backStage.name] = stage.name
def choiceSelection(stage,state):
"""
Once the user selects a choice, the screen where they selected the choice will be replaced with the description
of the choice they selected. The mappings between the stages then change such that the intermediary stage no longer exists.
For example:
Beginning mapping: (KITCHEN -> COOKBOOK -> LASAGNA or PIZZA or SALAD)
Choice selection: (LASAGNA)
After choice selection: (KITCHEN -> LASAGNA)
"""
storeDecision(stage,state) #stores choice made by user
#Store the MappingObject from previous stage associated with the button linked to current stage
stagemappingobj = None
oldBackStage = stage.backStage
for mappingobj in oldBackStage.buttonMapping:
if mappingobj.stageMapTo == stage:
stagemappingobj = mappingobj
#Look to see which stage the back button in current stage belongs to
newBackStage = goBack(stage)
#Changes the target stage mapping with the stage related to the user's selected option
if stagemappingobj != None:
for mappingobj in newBackStage.buttonMapping:
if mappingobj.stageMapTo == oldBackStage:
for index, newBackStageMappingObj in enumerate(newBackStage.buttonMapping):
if newBackStageMappingObj == mappingobj:
newBackStage.buttonMapping[index].stageMapTo = stagemappingobj.stageMapTo
def checkStageConditions(stage,state):
"""
This function marks current stage as having been seen, calls the choiceSelection function if the stage is
indicated to be a choice selction stage. Choice selection stages are marked by having a stage.backStep value > 1.
"""
stage.clicked = True
if stage.backStep > 1 :
choiceSelection(stage, state)
def playMusic(song):
"""
Plays a song when given the file name of the song file
"""
pygame.mixer.music.load('./sound/'+song)
pygame.mixer.music.play(-1)
def checkInventory(stage,state):
"""
Once the user selects an option, it will add the photo of the option to state.inventory (if applicable).
"""
if stage.inventoryPic != None and stage.inventoryPic not in state.inventory:
pygame.mixer.Channel(2).play(item) #plays sound
time.sleep(.2)
state.inventory.append(stage.inventoryPic) #item image appears
def checkAllConditions(screen):
"""
Check if there is an item to add to the inventory, and if an item is added to the inventory it draws it and checks if a stage needs to be updates
"""
checkStageConditions(screen.stage, screen.state)
checkInventory(screen.stage,screen.state)
screen.inventoryDecide()
#end