-
Notifications
You must be signed in to change notification settings - Fork 0
/
moana.py
executable file
·304 lines (258 loc) · 10.4 KB
/
moana.py
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
#!/usr/bin/python3
"""An extremely pointless game to learn pygame with."""
import pygame
import time
import drawer
import things
class AmazingMoanaGame(object):
"""OMG IT IS SO AMAZING."""
def __init__(self, square_size=64, max_x=15, max_y=7,
moana_image="images/babymoana.jpg",
moana_boat_image="images/moana_boat.jpg",
maui_image="images/maui_bird_by_biz.jpg",
sharkhead_image="images/maui.jpg",
crab_image="images/crab_by_biz.jpg",
dad_image="images/moana_dad.jpg",
lava_image="images/lava_monster_by_biz.jpg",
island_image="images/tifiti_by_biz.jpg",
hook_image="images/fishhook.jpg",
boat_image="images/boat.jpg",
heart_image="images/heart_by_biz.jpg",
shell_bin_image="images/shell_bin_by_biz.jpg",
shells_image="images/shell.png",
mud_image="images/lava_by_biz.jpg"):
"""Set up the game.
Add one Moana character, a bunch of shells and a bunch of obstacles. Beware:
this does nothing intelligent about making sure all shells are reachable or
checking that the character doesn't get trapped in a corner surrounded by
obstacles. No pathfinding algorithms were invoked in the writing of this
game.
Args:
square_size: (int) the size of each grid square. The images on the grid
should be square and should be this size or it'll look like a mess.
max_x, max_y: (int) how many squares on each side of the grid.
"""
pygame.init()
pygame.mixer.music.load("sounds/shells.wav")
pygame.mixer.music.play()
self.game_over = False
self.clock = pygame.time.Clock()
self.image_lib = {}
self.done = False
self.drawer = drawer.Drawer(square_size, max_x, max_y)
self.crab = things.SelfMovingThing(self.get_image(crab_image), self.drawer, x=int(max_x / 2))
self.dad = things.SelfMovingThing(self.get_image(dad_image), self.drawer, y=int(max_y / 2))
self.mud = things.StationaryThings(self.get_image(mud_image), self.drawer, obstacle=True)
self.mud.place_randomly(15)
self.shells = things.StationaryThings(self.get_image(shells_image), self.drawer)
self.shells.place_randomly(50)
self.hook = things.StationaryThings(self.get_image(hook_image), self.drawer)
self.hook.place_randomly(1)
self.boat = things.StationaryThings(self.get_image(boat_image), self.drawer)
self.boat.place_randomly(1)
self.heart = things.StationaryThings(self.get_image(heart_image), self.drawer)
self.shell_bin = things.StationaryThings(self.get_image(shell_bin_image), self.drawer)
self.shell_bin.place_randomly(1)
self.moana = things.MovingThing(self.get_image(moana_image), self.drawer, x=0)
self.moana.add_replacement_image(self.get_image(moana_boat_image))
self.maui = things.MovingThing(self.get_image(sharkhead_image), self.drawer, x=max_x - 1)
self.maui.add_replacement_image(self.get_image(maui_image))
self.island = things.MovingThing(self.get_image(lava_image), self.drawer, x=0, y=0)
self.island.add_replacement_image(self.get_image(island_image))
self.drawer.set_background((255, 64, 0)) # orange
self.update_score_text("Get the hook!")
self.has_hook = False
self.has_boat = False
def run(self):
"""The main game loop. Draw stuff and look for events."""
while not self.done:
self.check_events()
self.drawer.fill()
self.heart.draw()
self.island.draw()
self.shells.draw()
self.mud.draw()
self.crab.move_up_and_down()
self.dad.move_over_and_back()
self.hook.draw()
self.boat.draw()
self.moana.draw()
self.maui.draw()
self.crab.draw()
self.dad.draw()
self.shell_bin.draw()
if self.has_hook:
if self.crab.is_at(self.maui.pos()):
self.drawer.set_background((255, 64, 0)) # orange
self.has_hook = False
self.hook.place_randomly(1)
self.maui.set_default_image()
self.update_score_text("You LOST the hook!")
if self.shells.is_at(self.maui.pos()):
self.shells.delete(self.maui.pos())
self.maui.carrying += 1
if self.maui.carrying >= self.maui.capacity:
self.shells.place_randomly(1)
self.update_score_text("OH NO! MAUI DROPPED A SHELL! Put the shells in the shell bin!")
else:
self.maui.score += 1
self.update_score_text()
if self.has_boat:
if self.dad.is_at(self.moana.pos()):
self.drawer.set_background((255, 64, 0)) # orange
self.has_boat = False
self.boat.place_randomly(1)
self.moana.set_default_image()
self.update_score_text("You LOST the boat!")
if self.shells.is_at(self.moana.pos()):
self.shells.delete(self.moana.pos())
self.moana.carrying += 1
if self.moana.carrying >= self.moana.capacity:
self.shells.place_randomly(1)
self.update_score_text("OH NO! MOANA DROPPED A SHELL! Put the shells in the shell bin!")
else:
self.moana.score += 1
self.update_score_text()
if self.shell_bin.is_at(self.maui.pos()):
self.maui.carrying = 0
self.update_score_text("HURRAY! Now Maui can't lose shells any more!")
if self.shell_bin.is_at(self.moana.pos()):
self.moana.carrying = 0
self.update_score_text("HURRAY! Now Moana can't lose shells any more!")
# Only Moana can get the heart.
if self.heart.is_at(self.moana.pos()):
self.island.set_replacement_image()
score_str = "TEFITI HAS HER HEART BACK! Moana: %d, Maui: %d" % (self.moana.score, self.maui.score)
self.win(score_str)
# Only Maui can get the hook.
if self.hook.is_at(self.maui.pos()):
self.hook.delete(self.maui.pos())
self.maui.set_replacement_image()
self.has_hook = True
self.update_score_text("You got the hook!")
if self.has_boat:
# we're ready to begin!
self.drawer.set_background((0, 0, 255)) # blue
# Only Moana can get the boat.
if self.boat.is_at(self.moana.pos()):
self.boat.delete(self.moana.pos())
self.moana.set_replacement_image()
self.has_boat = True
self.update_score_text("You got the boat!")
if self.has_hook:
# we're ready to begin!
self.drawer.set_background((0, 0, 255)) # blue
# If they walk into lava, they get frozen and lose their things.
if self.mud.is_at(self.moana.pos()):
self.moana.freeze()
self.update_score_text("Moana is STUCK IN LAVA! Get her boat to save her!")
self.mud.delete(self.moana.pos())
if self.has_boat:
self.boat.place_randomly(1)
self.has_boat = False
if self.mud.is_at(self.maui.pos()):
self.maui.freeze()
self.update_score_text("Maui is STUCK IN LAVA! Get his hook to save him!")
self.mud.delete(self.maui.pos())
if self.has_hook:
self.hook.place_randomly(1)
self.has_hook = False
# If both are frozen, the game is over.
if self.moana.frozen and self.maui.frozen:
score_str = "Moana: %d, Maui: %d" % (self.moana.score, self.maui.score)
self.lose(score_str)
# They can unfreeze each other with each other's things.
if self.hook.is_at(self.moana.pos()):
self.maui.unfreeze()
self.hook.delete(self.moana.pos())
self.has_hook = True
if self.boat.is_at(self.maui.pos()):
self.moana.unfreeze()
self.boat.delete(self.maui.pos())
self.has_boat = True
self.drawer.show_messages()
pygame.display.flip()
self.clock.tick(60)
def win(self, score_str):
"""Set a winning message for winners."""
self.game_over = True
pygame.mixer.music.load("sounds/we_did_it.wav")
pygame.mixer.music.play()
self.drawer.update_score_text(
"You did it!! %s GREAT TEAM WORK! Press y to play again, q to quit." %
score_str)
self.drawer.set_background((0, 255, 0)) # green
self.crab.stop()
self.dad.stop()
def lose(self, score_str):
"""Set a losing message for losers."""
self.game_over = True
self.drawer.update_score_text(
"AWWW WE LOST. %s Press y to play again, q to quit." % score_str)
self.drawer.set_background((0, 0, 0)) # black
self.crab.stop()
self.dad.stop()
def update_score_text(self, prefix=""):
count = self.shells.count()
if prefix:
prefix = "[" + prefix + "] "
score_str = "Moana: %d (%d), Maui: %d (%d)" % (
self.moana.carrying, self.moana.score, self.maui.carrying, self.maui.score)
# TODO: this is a crappy place to have this game logic.
if count == 0 and not self.game_over:
print ("placing the heart")
self.heart.place_randomly(1)
return
if count == 1:
self.drawer.update_score_text("%sOnly 1 shell left! %s" % (prefix, score_str))
return
self.drawer.update_score_text("%s%d shells left! %s" % (prefix, count, score_str))
def check_events(self):
"""Check for keypresses and take actions based on them."""
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
self.done = True
if event.type != pygame.KEYDOWN:
continue
pressed = pygame.key.get_pressed()
# move maui
if pressed[pygame.K_UP]:
self.maui.move_up()
if pressed[pygame.K_DOWN]:
self.maui.move_down()
if pressed[pygame.K_LEFT]:
self.maui.move_left()
if pressed[pygame.K_RIGHT]:
self.maui.move_right()
# move moana
if pressed[pygame.K_w]:
self.moana.move_up()
if pressed[pygame.K_s]:
self.moana.move_down()
if pressed[pygame.K_a]:
self.moana.move_left()
if pressed[pygame.K_d]:
self.moana.move_right()
# quit/restart
if pressed[pygame.K_ESCAPE]:
self.done = True
if pressed[pygame.K_q]:
self.done = True
if pressed[pygame.K_y]:
# start the game again
self.__init__()
def get_image(self, filename):
"""Pull an image from disk and cache it.
Args:
filename: (str) local path to image file on disk.
Returns:
(pygame.Surface) blittable image.
"""
image = self.image_lib.get(filename)
if image is None:
image = pygame.image.load(filename)
return image
# Main.
game = AmazingMoanaGame()
game.run()