-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
340 lines (290 loc) · 13.9 KB
/
model.py
File metadata and controls
340 lines (290 loc) · 13.9 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import view
import game_map as gm
import controller as c
import pygame
import visual
import game_map
import game_object
import pyttsx3
class Model:
"""
The class used to display the game on to a screen.
Attributes:
map -> GameMap object with all required objects to play the game
view -> View object with required objects to display visuals and play audio
controller -> Controller object with functions for interpreting key presses
ping_delay -> integer value for milliseconds to wait before checking for a new ping
footstep_delay -> integer value for milliseconds to wait before playing next footstep sound
current_time -> current time in milliseconds
previous_time -> time in milliseconds for previous ping
footstep_previous_time -> time in milliseconds for previous footstep
footstep_count -> counter to track which audio file to play for footsteps
animation_step -> counter to track animation of moving feet
turned_on_wall ->
turned_on_NPC ->
wall_transparency ->
NPC_transparency ->
audio_loop -> boolean for reading text on the screen
show_home_screen -> boolean for displaying the home screen
show_instructions -> boolean for displaying the instructions
show_credits -> boolean for displaying the credits
game_on -> boolean to determine whether to display the game
run -> boolean for running the game
"""
def __init__(self):
"""Initalize the Model class with default variable values"""
self.map = gm.GameMap()
self.view = view.View(self.map)
self.controller = c.Controller()
self.ping_delay = 600
self.footstep_delay = 100
self.say_delay = 2000
self.current_time = 0
self.previous_time = 0
self.footstep_previous_time = 0
self.NPC_previous_say = 0
self.footstep_count = 0
self.animation_step = 0
self.turned_on_wall = None
self.turned_on_NPC = None
self.wall_transparency = 0
self.NPC_transparency = 0
self.NPC_interact = False
self.audio_loop = True
self.show_home_screen = True
self.show_instructions = False
self.show_credits = False
self.game_on = False
self.run = True
self.init_audio_engine()
def footstep_audio(self):
"""Call function to play footstep sound
"""
self.view.play_footsteps(self.current_time,self.footstep_previous_time,self.footstep_delay,self.footstep_count)
def init_audio_engine(self):
"""Initializes audio engine for text to speech
"""
self.audio_engine = pyttsx3.init()
self.audio_engine.setProperty('rate',200)
self.audio_engine.setProperty('rate',0.9)
self.audio_engine.setProperty('voice', 'english+f1')
def wall_collision_ping(self,direction):
"""Pings wall when a player runs into it
This function takes a direction of ping and sight range and displays the
wall at the correct distance and plays the sound at the correct volume
Key Arguments:
direction -> string indicating direction of ping
Return values: None
"""
ping_check = self.map.ping_from_player(direction,1)
dist = ping_check[1]
if self.current_time - self.previous_time >= self.ping_delay:
self.previous_time = self.current_time
if dist != None:
self.turned_on_wall = ping_check[0]
self.wall_transparency = 30
self.view.play_echo(round(dist)+2,direction, self.view.audio.hollow_sound)
def NPC_collision_ping(self,direction):
"""Pings NPC when a player runs into it
Key Arguments:
direction -> string indicating direction of ping
Return values: None
"""
ping_check = self.map.ping_from_player(direction,1)
dist = ping_check[1]
if self.current_time - self.previous_time >= self.ping_delay+2000:
self.previous_time = self.current_time
if dist != None:
self.turned_on_NPC = ping_check[0]
self.NPC_transparency = 30
self.view.play_echo(round(dist)+2,direction, self.view.audio.hollow_sound)
self.NPC_interact = True
def process_ping(self,direction,sight_range):
"""Processes ping
This function takes a direction of ping and sight range and displays the
wall at the correct distance and plays the sound at the correct volume
Key Arguments:
direction -> string indicating direction of ping
sight_range -> integer value for number of blocks visible
Return values: None
"""
if self.controller.ping_keys[direction]:
ping_check = self.map.ping_from_player(direction,sight_range)
dist = ping_check[1]
if self.current_time - self.previous_time >= self.ping_delay:
self.previous_time = self.current_time
if dist != None:
self.view.play_echo(round(dist),direction, self.view.audio.ping_sound)
self.turned_on_wall = ping_check[0]
self.wall_transparency = 30
self.turned_on_NPC = ping_check[0]
self.NPC_transparency = 30
def show_screen(self,screen):
"""Updates boolean to display the correct screen
The function sets all screen display booleans to false and takes the
referenced display screen and sets that boolean to be true. It also
resets the audio loop to read the screen when switched.
Key Arguments:
screen -> string indicating which screen to display
Return values: None
"""
self.audio_loop = True
self.show_home_screen = False
self.show_instructions = False
self.show_credits = False
self.game_on = False
if screen == 'home_screen':
self.show_home_screen = True
elif screen == 'instructions':
self.show_instructions = True
elif screen == 'credits':
self.show_credits = True
elif screen == 'game':
self.game_on = True
def check_for_change_screens(self,instruct,home,cred,game):
"""Sets which key presses to look for and switches screen if keypress detected
Key Arguments:
instruct-> boolean for looking for key press for instruction page
home -> boolean for looking for key press for home screen
cred -> boolean for looking for key press for credits page
game -> boolean for looking for key press for game
Return values: None
"""
self.controller.read_input()
if instruct == True:
if self.controller.hs_keys['I']:
self.show_screen('instructions')
if home == True:
if self.controller.hs_keys['H']:
self.show_screen('home_screen')
if cred == True:
if self.controller.hs_keys['C']:
self.show_screen('credits')
if game == True:
if self.controller.hs_keys['space']:
self.show_screen('game')
def run_game(self):
"""Run game
"""
#Create clock object
clock = pygame.time.Clock()
while self.run:
#Navigates home, instruction and credit pages
if self.show_home_screen == True:
self.view.update_screen('home_screen')
if self.audio_loop == True:
#self.view.audio.home_screen_audio(self.audio_engine)
self.audio_loop = False
self.check_for_change_screens(instruct=True, home=False, cred=True, game=True)
elif self.show_instructions == True:
self.view.update_screen('instructions')
if self.audio_loop == True:
#self.view.audio.instruction_page_audio(self.audio_engine)
self.audio_loop = False
self.check_for_change_screens(instruct=False, home=True, cred=True, game=True)
elif self.show_credits == True:
self.view.update_screen('credits')
if self.audio_loop == True:
#self.view.audio.credits_page_audio(self.audio_engine)
self.audio_loop = False
self.check_for_change_screens(instruct=False, home=True, cred=False, game=False)
#if self.view.visual.game_on == False:
#self.run = False
while self.game_on == True:
self.current_time = pygame.time.get_ticks()
#printself.current_time)
#forces the frames per second (fps) of the game to be equal to 60 fps
clock.tick(60)
if self.animation_step == 16:
self.animation_step = 0
#prevents the user from providing input while they are moving
if self.map.at_block() == True:
#updates the controller-
self.controller.read_input()
#process controller inputs for pings
self.process_ping('front',3)
self.process_ping('back',3)
self.process_ping('right',3)
self.process_ping('left',3)
#process controller inputs for moving and interacting
speed = self.map.player.speed
if self.controller.move_keys['north']:
#TODO: Extract to function in player
#player.move("direction")
self.map.player.rect.move_ip(0,-speed)
if self.map.player_wall_collision() != None:
self.map.player.rect.move_ip(0,speed)
self.wall_collision_ping('front')
elif self.map.player_NPC_collision() != None:
self.map.player.rect.move_ip(0,speed)
self.NPC_collision_ping('front')
else:
self.animation_step += 1
if self.animation_step % 2 == 0:
self.map.player.set_image('north',int(self.animation_step/2))
self.footstep_audio()
"""#if the player runs into an npc cause the player to interact
if self.map.player_NPC_collision() != None:
self.map.player.rect.move_ip(0,speed)
self.NPC_collision_ping('front')"""
elif self.controller.move_keys['south']:
self.map.player.rect.move_ip(0,speed)
if self.map.player_wall_collision() != None:
self.map.player.rect.move_ip(0,-speed)
self.wall_collision_ping('back')
elif self.map.player_NPC_collision() != None:
self.map.player.rect.move_ip(0,speed)
self.NPC_collision_ping('back')
else:
self.animation_step += 1
if self.animation_step % 2 == 0:
self.map.player.set_image('south',int(self.animation_step/2))
self.footstep_audio()
elif self.controller.move_keys['west']:
self.map.player.rect.move_ip(-speed,0)
if self.map.player_wall_collision() != None:
self.map.player.rect.move_ip(speed,0)
self.wall_collision_ping('left')
elif self.map.player_NPC_collision() != None:
self.map.player.rect.move_ip(0,speed)
self.NPC_collision_ping('left')
else:
self.animation_step += 1
if self.animation_step % 2 == 0:
self.map.player.set_image('west',int(self.animation_step/2))
self.footstep_audio()
elif self.controller.move_keys['east']:
self.map.player.rect.move_ip(speed,0)
if self.map.player_wall_collision() != None:
self.map.player.rect.move_ip(-speed,0)
self.wall_collision_ping('right')
elif self.map.player_NPC_collision() != None:
self.map.player.rect.move_ip(0,speed)
self.NPC_collision_ping('right')
else:
self.animation_step += 1
if self.animation_step % 2 == 0:
self.map.player.set_image('east',int(self.animation_step/2))
self.footstep_audio()
if self.turned_on_wall != None and self.wall_transparency >=0:
self.turned_on_wall.set_transparency(1 + self.wall_transparency * 8)
self.wall_transparency -= 1
if self.turned_on_NPC != None and self.NPC_transparency >=0:
self.turned_on_NPC.set_transparency(1 + self.NPC_transparency * 8)
self.NPC_transparency -= 1
#update visuals
if self.NPC_interact == False:
self.view.update_screen('game')
if self.NPC_interact == True:
self.NPC_previous_say = self.current_time
self.turned_on_NPC.update_dialogue(self.audio_engine, self.view)
#self.view.say_and_display('string to say and display',self.audio_engine)
self.NPC_interact = False
if self.view.visual.game_on == False:
self.game_on = False
self.run = False
if __name__ == "__main__":
pygame.init()
pygame.mixer.init()
m = Model()
m.run_game()