32df4ccb43caeb03db6822d6e28fcaebd9d547fe
[butaba-adventures.git] / maingame.py
1 import pygame
2 import sys
3 import random
4 import os.path
5 import cPickle
6
7 import level
8 import butaba
9 import utility
10 import gameobjects
11 import constants
12 import npcs
13
14 class MainGame:
15
16 # initialize the game
17 def __init__ (self):
18 pygame.init ()
19 self.screen = pygame.display.set_mode ((720, 512))
20 pygame.display.set_caption ("The Adventures of Butaba")
21
22 # initalize background graphics
23 self.img_tileset = pygame.image.load (os.path.join ("background", "tileset.png")).convert ()
24
25 self.img_menu = pygame.image.load (os.path.join ("background", "menu_screen.png")).convert ()
26
27 self.img_inventory = pygame.image.load (os.path.join ("background", "inventory.png")).convert ()
28
29 self.img_chestbg = pygame.image.load (os.path.join ("background", "chestcontent.png")).convert ()
30
31 self.img_dialogue = pygame.image.load (os.path.join ("background", "dialog_screen.png")).convert ()
32 self.img_dialogue.set_colorkey (pygame.Color (0, 255, 0))
33
34
35 # initialize object graphics
36 self.img_redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert ()
37 self.img_redpotion.set_colorkey (pygame.Color (0, 255, 0))
38 self.img_goldcoins = pygame.image.load (os.path.join ("objects", "gold-coins.png")).convert ()
39 self.img_goldcoins.set_colorkey (pygame.Color (0, 255, 0))
40 self.img_wand = pygame.image.load (os.path.join ("objects", "wand.png")).convert ()
41 self.img_wand.set_colorkey (pygame.Color (0, 255, 0))
42 self.img_bulb = pygame.image.load (os.path.join ("objects", "bulb.png")).convert ()
43 self.img_bulb.set_colorkey (pygame.Color (0, 255, 0))
44 self.img_lightning = pygame.image.load (os.path.join ("objects", "lightning.png")).convert ()
45 self.img_lightning.set_colorkey (pygame.Color (0, 255, 0))
46 self.img_key = pygame.image.load (os.path.join ("objects", "key.png")).convert ()
47 self.img_key.set_colorkey (pygame.Color (0, 255, 0))
48 self.img_key2 = pygame.image.load (os.path.join ("objects", "key2.png")).convert ()
49 self.img_key2.set_colorkey (pygame.Color (0, 255, 0))
50 self.img_chest = pygame.image.load (os.path.join ("objects", "chest.png")).convert ()
51 self.img_chest.set_colorkey (pygame.Color (0, 255, 0))
52
53 # initialize player graphics
54 self.img_butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert ()
55 self.img_butabafront.set_colorkey (pygame.Color (0, 255, 0))
56 self.img_butababack = pygame.image.load (os.path.join ("sprite", "butaba-back.png")).convert ()
57 self.img_butababack.set_colorkey (pygame.Color (0, 255, 0))
58 self.img_butabaleft = pygame.image.load (os.path.join ("sprite", "butaba-left.png")).convert ()
59 self.img_butabaleft.set_colorkey (pygame.Color (0, 255, 0))
60 self.img_butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert ()
61 self.img_butabaright.set_colorkey (pygame.Color (0, 255, 0))
62
63 # initialize NPC graphics
64 self.img_bulisa = pygame.image.load (os.path.join ("sprite", "bulisa.png")).convert ()
65 self.img_bulisa.set_colorkey (pygame.Color (0, 255, 0))
66
67 # initialize NPC portraits
68 self.img_bulisa_portrait = pygame.image.load (os.path.join ("portraits", "bulisa.png")).convert ()
69
70 # set level data
71 self.setup_levels ()
72 # set current level and position of our character
73 self.currentlevel = self.level1
74
75 # set the status message
76 self.status_message = "Game started"
77
78 self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT)
79
80 # set up the levels and their interactions
81 def setup_levels (self):
82 # set up the objects first
83 chest1 = gameobjects.Chest (2, 6, "chest", self.img_chest, constants.KEY_CHEST1, True)
84 chest2 = gameobjects.Chest (6, 6, "chest", self.img_chest, constants.KEY_CHEST2, True)
85 key1 = gameobjects.Key (5, 3, "a chest key", self.img_key2, constants.KEY_CHEST1)
86 key2 = gameobjects.Key (5, 3, "a chest key", self.img_key, constants.KEY_CHEST2)
87 potion = gameobjects.HealthPotion (5, 2, self.img_redpotion)
88 gold50 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 50)
89 gold25 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 25)
90 gold10 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 10)
91 potion2 = gameobjects.HealthPotion (5, 2, self.img_redpotion)
92 potion3 = gameobjects.HealthPotion (5, 2, self.img_redpotion)
93
94 npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisa, self.img_bulisa_portrait,
95 [ os.path.join ("dialogues", "bulisa1.dlg") ])
96
97 chest1.objects = [ gold50, gold25, potion2, potion3, key2, gold10 ]
98
99 # create the levels
100 self.level1 = level.Level (cPickle.load (file ("levels/level1.dat")),
101 objects = [ chest1 ] )
102
103 self.level1w = level.Level (cPickle.load (file ("levels/level1w.dat")))
104
105 self.level1e = level.Level (cPickle.load (file ("levels/level1e.dat")),
106 objects = [ key1, potion, chest2 ], npcs = [ npc_bulisa ])
107
108 # set up the interaction between levels
109 self.level1.levelright = self.level1e
110 self.level1.levelleft = self.level1w
111 self.level1e.levelleft = self.level1
112 self.level1w.levelright = self.level1
113
114 def main_loop (self):
115 # main game loop
116 while 1:
117 # clear screen
118 self.screen.fill (pygame.Color (0,0,0))
119 # draw the level
120 self.draw_level_background (self.currentlevel)
121 # draw level objects
122 self.draw_level_objects (self.currentlevel)
123 # draw the NPCs in the level
124 self.draw_level_npcs (self.currentlevel)
125 # draw our character
126 self.draw_butaba ()
127 # display the character's inventory
128 self.draw_inventory ()
129 # draw the status info
130 self.draw_status ()
131 # update the display
132 pygame.display.update ()
133
134 # get keyboard events
135 for event in pygame.event.get ():
136 if event.type == pygame.QUIT:
137 sys.exit (0)
138 # if keyboard event
139 if event.type == pygame.KEYDOWN:
140 if event.key == pygame.K_UP:
141 self.move_butaba_up ()
142 elif event.key == pygame.K_DOWN:
143 self.move_butaba_down ()
144 elif event.key == pygame.K_LEFT:
145 self.move_butaba_left ()
146 elif event.key == pygame.K_RIGHT:
147 self.move_butaba_right ()
148 # drinking health potion in inventory
149 elif event.key == ord ("h") or event.key == ord ("H"):
150 self.inventory_drink_health_potion ()
151 # quit the game
152 elif event.key == ord ("q") or event.key == ord ("Q"):
153 sys.exit (0)
154
155 # drink a health potion if it is in the player's inventory
156 def inventory_drink_health_potion (self):
157 # look for a health potion
158 for item in self.butaba.objects:
159 if isinstance (item, gameobjects.HealthPotion) is True:
160 self.use_object (self.butaba, item)
161 break
162
163 def move_butaba_up (self):
164 # clear any status messages
165 self.status_message = None
166 # first if butaba is not facing up, make him face up
167 if self.butaba.position <> butaba.Butaba.BACK:
168 self.butaba.position = butaba.Butaba.BACK
169 return
170
171 # if butaba is trying to move off the top of the screen
172 if self.butaba.row <= 0:
173 # if there is a level above set current level to that one
174 if self.currentlevel.leveltop is not None:
175 lastrow = len (self.currentlevel.leveltop.background) - 1
176 # interact with objects
177 if self.level_interact (self.currentlevel.leveltop, lastrow, self.butaba.col) is False:
178 return
179
180 # make sure there is no obstacle
181 if self.check_background_obstacle (self.currentlevel.leveltop, lastrow, self.butaba.col) is False:
182 self.currentlevel = self.currentlevel.leveltop
183 self.butaba.row = lastrow
184 # normal upward movement
185 else:
186 # if there is any object in that place interact with it
187 # if any object is a blocking object then avoid movement
188 if self.level_interact (self.currentlevel, self.butaba.row-1, self.butaba.col) is False:
189 return
190
191 if self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False:
192 self.butaba.row -= 1
193
194 def move_butaba_down (self):
195 # clear any status messages
196 self.status_message = None
197 # first if butaba is not facing forward, make him face forward/down
198 if self.butaba.position <> butaba.Butaba.FRONT:
199 self.butaba.position = butaba.Butaba.FRONT
200 return
201
202 # if butaba is trying to move off the bottom of the screen
203 if self.butaba.row >= len (self.currentlevel.background)-1:
204 # if there is a level below set current level to that one
205 if self.currentlevel.levelbottom is not None:
206 # interact with objects if any
207 # if any object is a blocking object then avoid movement
208 if self.level_interact (self.currentlevel.levelbottom, 0, self.butaba.col) is False:
209 return
210 # make sure there is no obstacle at that position
211 if self.check_background_obstacle (self.currentlevel.levelbottom, 0, self.butaba.col) is False:
212 self.currentlevel = self.currentlevel.levelbottom
213 self.butaba.row = 0
214 # normal downward movement
215 else:
216 # interact with objects if any
217 # if any object is a blocking object then avoid movement
218 if self.level_interact (self.currentlevel, self.butaba.row+1, self.butaba.col) is False:
219 return
220 if self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False:
221 self.butaba.row += 1
222
223 # check if a background tile is an obstacle
224 def check_background_obstacle (self, level, row, col):
225 if (level.background[row][col][2] == 1):
226 return True
227 else:
228 return False
229
230 # get and interact with objects and characters if present in a particular row/col
231 def level_interact (self, level, row, col):
232 objs = []
233 # get list of objects at current location
234 for obj in level.objects:
235 if obj.row == row and obj.col == col:
236 objs.append (obj)
237
238 notblock = self.interact_objects (level, objs)
239
240 # get npc at current location
241 current_npc = None
242 for npc in level.npcs:
243 if npc.row == row and npc.col == col:
244 current_npc = npc
245 break
246
247 # npcs always block the tile. So return false if there is an NPC
248 # at the location
249 if current_npc is not None:
250 self.interact_npc (current_npc)
251 return False
252
253 return notblock
254
255 # interaction with npcs
256 def interact_npc (self, npc):
257 # interact with NPC and get the response ID
258 resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba, 0, 90)
259
260 # if none
261 if resp_id is None:
262 self.status_message = "You cannot initiate a conversation with %s" % npc.charname
263
264 # interaction with objects
265 def interact_objects (self, container, objs):
266 # overall flag for blocking/non-blocking objects
267 notblock = True
268
269 # now perform interaction
270 for obj in objs:
271 # run the object interact function
272 if obj.interact () is False:
273 notblock = False
274 # if object can be picked up ask
275 if obj.can_pickup is True:
276 ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", obj.use_str, "Ignore"], self.img_menu)
277 # if the answer is "pick up"
278 if ans == 1:
279 self.pickup_object (container, obj)
280 elif ans == 2:
281 # use the object according to its type
282 self.use_object (container, obj)
283 # if it cannot be picked up, try to use it anyway
284 else:
285 ans = utility.ask_question (self.screen, "Found %s." % obj.text, [obj.use_str, "Ignore"], self.img_menu)
286 if ans == 1:
287 self.use_object (container, obj)
288
289 return notblock
290
291 # transfer an object from one container to another
292 # container must have an objects list
293 def transfer_object (self, source, obj, dest):
294 # remove object from source
295 source.objects.remove (obj)
296 # add object to destination
297 dest.objects.append (obj)
298
299 # picking up an object
300 def pickup_object (self, container, obj):
301 # only if object can be picked up, pick it up or use it
302 if obj.can_pickup is True:
303 # check if the inventory is full
304 if len (self.butaba.objects) >= butaba.Butaba.MAXITEMS:
305 self.status_message = "Cannot pick up item. Inventory full"
306 else:
307 # add item to inventory
308 self.transfer_object (container, obj, self.butaba)
309
310 self.status_message = "You picked up %s" % obj.text
311
312 # this method uses the object first by calling the object use () method
313 # and then performing specific actions as necessary
314 def use_object (self, container, obj):
315 # if the object is a health potion
316 if isinstance (obj, gameobjects.HealthPotion) is True:
317 if self.butaba.health < butaba.Butaba.MAXHEALTH:
318 obj.use (self.butaba)
319 container.objects.remove (obj)
320 self.status_message = "You gained health"
321 else:
322 self.status_message = "You already have maximum health!"
323 # if the object is a chest
324 elif isinstance (obj, gameobjects.Chest) is True:
325 # if chest is locked, try to open it
326 if obj.locked is True:
327 # try opening the chest with every item 9the use () function
328 # of the chest determines if item is a key anyway
329 fittedkey = None
330 for invobj in self.butaba.objects:
331 fittedkey = obj.use (invobj)
332 # if a key fits
333 if fittedkey is not None:
334 break
335 # if no key found
336 if fittedkey is None:
337 self.status_message = "No key found to open %s" % obj.text
338 # chest successfully unlocked
339 else:
340 self.status_message = "You unlocked the %s" % obj.text
341 # remove the key from Butaba
342 self.butaba.objects.remove (fittedkey)
343 # add an experience point for unlocking chest subject
344 # to a limit of KNOWLEDGEMAX_CHEST_UNLOCK
345 if self.butaba.experience < constants.KNOWLEDGEMAX_CHEST_UNLOCK:
346 self.butaba.experience += 1
347 self.status_message += " and gained experience!"
348 # display the contents of the chest
349 else:
350 item = utility.get_container_object (self.screen, obj, self.img_chestbg, 30)
351 if item is not None:
352 self.interact_objects (obj, [ item, ])
353
354 # if the object is gold coins
355 elif isinstance (obj, gameobjects.GoldCoins) is True:
356 obj.use (self.butaba)
357 self.status_message = "You picked up %d gold." % obj.value
358 # remove the gold coins after adding it to Butaba's gold
359 container.objects.remove (obj)
360
361 def move_butaba_left (self):
362 # clear any status messages
363 self.status_message = None
364
365 # first if Butaba is not facing left, make him face left
366 if self.butaba.position <> butaba.Butaba.LEFT:
367 self.butaba.position = butaba.Butaba.LEFT
368 return
369
370 # if butaba is trying to move off the left edge
371 if self.butaba.col <= 0:
372 # if there is a level to the right set current level to that one
373 if self.currentlevel.levelleft is not None:
374 # get the last column of the previous level
375 lastcol = len (self.currentlevel.levelleft.background[0]) - 1
376 # interact with objects if any
377 # if any object is a blocking object then avoid movement
378 if self.level_interact (self.currentlevel.levelleft, self.butaba.row, lastcol) is False:
379 return
380 # make sure there is no obstacle at that position of movement
381 if self.check_background_obstacle (self.currentlevel.levelleft, self.butaba.row, lastcol) is False:
382 self.currentlevel = self.currentlevel.levelleft
383 self.butaba.col = lastcol
384 # normal left movement
385 else:
386 # interact with objects if any
387 # if any object is a blocking object then avoid movement
388 if self.level_interact (self.currentlevel, self.butaba.row, self.butaba.col-1) is False:
389 return
390 if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False:
391 self.butaba.col -= 1
392
393 def move_butaba_right (self):
394 # clear any status messages
395 self.status_message = None
396
397 # First if Butaba is not facing right make him face right
398 if self.butaba.position <> butaba.Butaba.RIGHT:
399 self.butaba.position = butaba.Butaba.RIGHT
400 return
401
402 # if butaba is trying to move off the right edge
403 if self.butaba.col >= len (self.currentlevel.background[0])-1:
404 # if there is a level to the right swap current level with that one
405 if self.currentlevel.levelright is not None:
406 # interact with objects if any
407 # if any object is a blocking object then avoid movement
408 if self.level_interact (self.currentlevel.levelright, self.butaba.row, 0) is False:
409 return
410
411 # make sure there is no obstacle at that position of movement
412 # get the last column of the previous level
413 if self.check_background_obstacle (self.currentlevel.levelright, self.butaba.row, 0) is False:
414 self.currentlevel = self.currentlevel.levelright
415 self.butaba.col = 0
416 # normal right movement
417 else:
418 # interact with objects if any
419 # if any object is a blocking object then avoid moving
420 if self.level_interact (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False:
421 return
422 if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False:
423 self.butaba.col += 1
424
425 def draw_butaba (self):
426 if self.butaba.position == butaba.Butaba.FRONT:
427 self.screen.blit (self.img_butabafront, (self.butaba.col*48, self.butaba.row*48))
428 elif self.butaba.position == butaba.Butaba.BACK:
429 self.screen.blit (self.img_butababack, (self.butaba.col*48, self.butaba.row*48))
430 elif self.butaba.position == butaba.Butaba.LEFT:
431 self.screen.blit (self.img_butabaleft, (self.butaba.col*48, self.butaba.row*48))
432 elif self.butaba.position == butaba.Butaba.RIGHT:
433 self.screen.blit (self.img_butabaright, (self.butaba.col*48, self.butaba.row*48))
434
435
436 # Draw the status infodisplay
437 def draw_status (self):
438 self.screen.blit (self.img_redpotion, (485, 10))
439 utility.put_text (self.screen, 550, 25, 20, (255, 0, 0), "%d" % self.butaba.health)
440
441 self.screen.blit (self.img_lightning, (620, 10))
442 utility.put_text (self.screen, 660, 25, 20, (255,255,255), "%d" % self.butaba.strength)
443
444 self.screen.blit (self.img_wand, (485, 65))
445 utility.put_text (self.screen, 550, 75, 20, (0, 0, 255), "%d" % self.butaba.magic)
446
447 self.screen.blit (self.img_bulb, (620, 65))
448 utility.put_text (self.screen, 660, 75, 20, (0, 255, 0), "%d" % self.butaba.experience)
449
450 self.screen.blit (self.img_goldcoins, (485, 110))
451 utility.put_text (self.screen, 550, 130, 20, (255, 255, 0), "%d" % self.butaba.gold)
452
453 if self.status_message is not None:
454 utility.put_text (self.screen, 10, 485, 10, (255,255, 0), "%s" % self.status_message)
455
456 # display the inventory of the player
457 def draw_inventory (self):
458 # draw the inventory slots
459 r = 1
460 c = 1
461 utility.put_text (self.screen, 490, 170, 16, (255,255 , 0), "Inventory")
462 for i in range (butaba.Butaba.MAXITEMS):
463 self.screen.blit (self.img_inventory, (440+c*54, 150+r*54))
464 if c % 4 == 0:
465 r += 1
466 c = 1
467 else:
468 c += 1
469
470 r = 1
471 c = 1
472 for obj in self.butaba.objects:
473 self.screen.blit (obj.image, (440+c*54+2, 150+r*54+2))
474 if c % 4 == 0:
475 r += 1
476 c = 1
477 else:
478 c += 1
479
480 # Draw the level background tiles on surface
481 def draw_level_background (self, level):
482 i = 0
483 for row in level.background:
484 j = 0
485 for tilerow, tilecol, is_solid in row:
486 tilex = tilecol * 48
487 tiley = tilerow * 48
488 self.screen.blit (self.img_tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48))
489
490 j += 1
491 i += 1
492
493 # Draw the level objects
494 def draw_level_objects (self, level):
495 for obj in level.objects:
496 if obj.image is not None:
497 self.screen.blit (obj.image, (obj.col*48, obj.row*48))
498
499
500 # Draw the NPCs in the level
501 def draw_level_npcs (self, level):
502 for npc in level.npcs:
503 if npc.image is not None:
504 self.screen.blit (npc.image, (npc.col*48, npc.row*48))