Implemented NPC movement (random)
[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 import gamestate
14
15 class MainGame:
16
17 # initialize the game
18 def __init__ (self):
19 random.seed ()
20 pygame.init ()
21
22 self.clock = pygame.time.Clock ()
23 self.screen = pygame.display.set_mode ((720, 512))
24 pygame.display.set_caption ("The Adventures of Butaba")
25
26 # initalize background graphics
27 self.img_tileset = pygame.image.load (os.path.join ("background", "tileset.png")).convert ()
28
29 self.img_menu = pygame.image.load (os.path.join ("background", "menu_screen.png")).convert ()
30
31 self.img_inventory = pygame.image.load (os.path.join ("background", "inventory.png")).convert ()
32
33 self.img_chestbg = pygame.image.load (os.path.join ("background", "chestcontent.png")).convert ()
34
35 self.img_dialogue = pygame.image.load (os.path.join ("background", "dialog_screen.png")).convert ()
36 self.img_dialogue.set_colorkey (pygame.Color (0, 255, 0))
37
38
39 # initialize object graphics
40 self.img_redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert ()
41 self.img_redpotion.set_colorkey (pygame.Color (0, 255, 0))
42 self.img_goldcoins = pygame.image.load (os.path.join ("objects", "gold-coins.png")).convert ()
43 self.img_goldcoins.set_colorkey (pygame.Color (0, 255, 0))
44 self.img_wand = pygame.image.load (os.path.join ("objects", "wand.png")).convert ()
45 self.img_wand.set_colorkey (pygame.Color (0, 255, 0))
46 self.img_bulb = pygame.image.load (os.path.join ("objects", "bulb.png")).convert ()
47 self.img_bulb.set_colorkey (pygame.Color (0, 255, 0))
48 self.img_lightning = pygame.image.load (os.path.join ("objects", "lightning.png")).convert ()
49 self.img_lightning.set_colorkey (pygame.Color (0, 255, 0))
50 self.img_key = pygame.image.load (os.path.join ("objects", "key.png")).convert ()
51 self.img_key.set_colorkey (pygame.Color (0, 255, 0))
52 self.img_key2 = pygame.image.load (os.path.join ("objects", "key2.png")).convert ()
53 self.img_key2.set_colorkey (pygame.Color (0, 255, 0))
54 self.img_chest = pygame.image.load (os.path.join ("objects", "chest.png")).convert ()
55 self.img_chest.set_colorkey (pygame.Color (0, 255, 0))
56 self.img_bucket = pygame.image.load (os.path.join ("objects", "bucket.png")).convert ()
57 self.img_bucket.set_colorkey (pygame.Color (0, 255, 0))
58
59 # initialize player graphics
60 self.img_butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert ()
61 self.img_butabafront.set_colorkey (pygame.Color (0, 255, 0))
62 self.img_butababack = pygame.image.load (os.path.join ("sprite", "butaba-back.png")).convert ()
63 self.img_butababack.set_colorkey (pygame.Color (0, 255, 0))
64 self.img_butabaleft = pygame.image.load (os.path.join ("sprite", "butaba-left.png")).convert ()
65 self.img_butabaleft.set_colorkey (pygame.Color (0, 255, 0))
66 self.img_butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert ()
67 self.img_butabaright.set_colorkey (pygame.Color (0, 255, 0))
68
69 # initialize NPC graphics
70 self.img_bulisafront = pygame.image.load (os.path.join ("sprite", "bulisa-front.png")).convert ()
71 self.img_bulisafront.set_colorkey (pygame.Color (0, 255, 0))
72
73 self.img_bulisaback = pygame.image.load (os.path.join ("sprite", "bulisa-back.png")).convert ()
74 self.img_bulisaback.set_colorkey (pygame.Color (0, 255, 0))
75
76 self.img_bulisaleft = pygame.image.load (os.path.join ("sprite", "bulisa-left.png")).convert ()
77 self.img_bulisaleft.set_colorkey (pygame.Color (0, 255, 0))
78
79 self.img_bulisaright = pygame.image.load (os.path.join ("sprite", "bulisa-right.png")).convert ()
80 self.img_bulisaright.set_colorkey (pygame.Color (0, 255, 0))
81
82 # initialize portraits
83 self.img_butaba_portrait = pygame.image.load (os.path.join ("portraits", "butaba.png")).convert ()
84 self.img_bulisa_portrait = pygame.image.load (os.path.join ("portraits", "bulisa.png")).convert ()
85
86 # set level data
87 self.setup_levels ()
88 # set current level and position of our character
89 self.currentlevel = self.level1
90
91 # set the status message
92 self.status_message = "Game started"
93
94 self.butaba = butaba.Butaba (5,0, self.img_butabaleft,
95 self.img_butabaright, self.img_butabafront,
96 self.img_butababack, self.img_butaba_portrait, constants.RIGHT)
97
98 # set up the levels and their interactions
99 def setup_levels (self):
100 # set up the objects first
101 chest1 = gameobjects.Chest (2, 6, "chest", self.img_chest, constants.KEY_CHEST1, True)
102 chest2 = gameobjects.Chest (6, 6, "chest", self.img_chest, constants.KEY_CHEST2, True)
103 key2 = gameobjects.Key (5, 3, "a chest key", self.img_key, constants.KEY_CHEST2)
104 potion = gameobjects.HealthPotion (5, 2, self.img_redpotion)
105 gold50 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 50)
106 gold25 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 25)
107 gold10 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 10)
108 bucket = gameobjects.Bucket (6, 3, self.img_bucket)
109
110 well1 = gameobjects.Well (4, 7)
111 well2 = gameobjects.Well (5, 7)
112 well3 = gameobjects.Well (4, 8)
113 well4 = gameobjects.Well (5, 8)
114
115 npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisaleft, self.img_bulisaright,
116 self.img_bulisafront, self.img_bulisaback,
117 self.img_bulisa_portrait, constants.FRONT,
118 (2, 2, 2, 2),
119 [ os.path.join ("dialogues", "bulisa1.dlg"),
120 os.path.join ("dialogues", "bulisa2.dlg"),
121 os.path.join ("dialogues", "bulisa3.dlg"),
122 os.path.join ("dialogues", "bulisa4.dlg") ] )
123
124 chest1.objects = [ gold50, gold25, key2, gold10 ]
125
126 # create the levels
127 self.level1 = level.Level (cPickle.load (file (os.path.join ("levels", "level1.dat"))),
128 objects = [ chest1 ] )
129
130 self.level1w = level.Level (cPickle.load (file (os.path.join ("levels", "level1w.dat"))))
131
132 self.level1e = level.Level (cPickle.load (file (os.path.join ("levels", "level1e.dat"))),
133 objects = [ potion, chest2 ], npcs = [ npc_bulisa ])
134
135 self.level1ee = level.Level (cPickle.load (file (os.path.join ("levels", "level1ee.dat"))),
136 objects = [ well1, well2, well3, well4 ])
137
138 self.level1n = level.Level (cPickle.load (file (os.path.join ("levels", "level1n.dat"))),
139 objects = [ bucket ])
140
141 # set up the interaction between levels
142 self.level1.levelright = self.level1e
143 self.level1.levelleft = self.level1w
144 self.level1e.levelleft = self.level1
145 self.level1e.levelright = self.level1ee
146 self.level1ee.levelleft = self.level1e
147 self.level1w.levelright = self.level1
148 self.level1.leveltop = self.level1n
149 self.level1n.levelbottom = self.level1
150
151 def main_loop (self):
152 # main game loop
153 while 1:
154 self.clock.tick (25)
155 # clear screen
156 self.screen.fill (pygame.Color (0,0,0))
157 # draw the level
158 self.draw_level_background (self.currentlevel)
159 # draw level objects
160 self.draw_level_objects (self.currentlevel)
161 # draw the NPCs in the level
162 self.draw_level_npcs (self.currentlevel)
163 # draw our character
164 self.draw_butaba ()
165 # display the character's inventory
166 self.draw_inventory ()
167 # draw the status info
168 self.draw_status ()
169 # update the display
170 pygame.display.update ()
171
172 # get keyboard events
173 for event in pygame.event.get ():
174 if event.type == pygame.QUIT:
175 sys.exit (0)
176 # if keyboard event
177 if event.type == pygame.KEYDOWN:
178 if event.key == pygame.K_UP:
179 self.move_butaba_up ()
180 elif event.key == pygame.K_DOWN:
181 self.move_butaba_down ()
182 elif event.key == pygame.K_LEFT:
183 self.move_butaba_left ()
184 elif event.key == pygame.K_RIGHT:
185 self.move_butaba_right ()
186 # drinking health potion in inventory
187 elif event.key == ord ("h") or event.key == ord ("H"):
188 self.inventory_drink_health_potion ()
189 # quit the game
190 elif event.key == ord ("q") or event.key == ord ("Q"):
191 sys.exit (0)
192
193 # drink a health potion if it is in the player's inventory
194 def inventory_drink_health_potion (self):
195 # look for a health potion
196 for item in self.butaba.objects:
197 if isinstance (item, gameobjects.HealthPotion) is True:
198 self.use_object (self.butaba, item)
199 break
200
201 def move_butaba_up (self):
202 # clear any status messages
203 self.status_message = None
204 # first if butaba is not facing up, make him face up
205 if self.butaba.position <> constants.BACK:
206 self.butaba.position = constants.BACK
207 return
208
209 # if butaba is trying to move off the top of the screen
210 if self.butaba.row <= 0:
211 # if there is a level above set current level to that one
212 if self.currentlevel.leveltop is not None:
213 lastrow = len (self.currentlevel.leveltop.background) - 1
214 # interact with objects
215 if self.level_interact (self.currentlevel.leveltop, lastrow, self.butaba.col) is False:
216 return
217
218 # make sure there is no obstacle
219 if self.check_background_obstacle (self.currentlevel.leveltop, lastrow, self.butaba.col) is False:
220 self.currentlevel = self.currentlevel.leveltop
221 self.butaba.row = lastrow
222 # normal upward movement
223 else:
224 # if there is any object in that place interact with it
225 # if any object is a blocking object then avoid movement
226 if self.level_interact (self.currentlevel, self.butaba.row-1, self.butaba.col) is False:
227 return
228
229 if self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False:
230 self.butaba.row -= 1
231
232 def move_butaba_down (self):
233 # clear any status messages
234 self.status_message = None
235 # first if butaba is not facing forward, make him face forward/down
236 if self.butaba.position <> constants.FRONT:
237 self.butaba.position = constants.FRONT
238 return
239
240 # if butaba is trying to move off the bottom of the screen
241 if self.butaba.row >= len (self.currentlevel.background)-1:
242 # if there is a level below set current level to that one
243 if self.currentlevel.levelbottom is not None:
244 # interact with objects if any
245 # if any object is a blocking object then avoid movement
246 if self.level_interact (self.currentlevel.levelbottom, 0, self.butaba.col) is False:
247 return
248 # make sure there is no obstacle at that position
249 if self.check_background_obstacle (self.currentlevel.levelbottom, 0, self.butaba.col) is False:
250 self.currentlevel = self.currentlevel.levelbottom
251 self.butaba.row = 0
252 # normal downward movement
253 else:
254 # interact with objects if any
255 # if any object is a blocking object then avoid movement
256 if self.level_interact (self.currentlevel, self.butaba.row+1, self.butaba.col) is False:
257 return
258 if self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False:
259 self.butaba.row += 1
260
261 # check if a background tile is an obstacle
262 def check_background_obstacle (self, level, row, col):
263 if (level.background[row][col][2] == 1):
264 return True
265 else:
266 return False
267
268 # get and interact with objects and characters if present in a particular row/col
269 def level_interact (self, level, row, col):
270 objs = []
271 # get list of objects at current location
272 for obj in level.objects:
273 if obj.row == row and obj.col == col:
274 objs.append (obj)
275
276 notblock = self.interact_objects (level, objs)
277
278 # get npc at current location
279 current_npc = None
280 for npc in level.npcs:
281 if npc.row == row and npc.col == col:
282 current_npc = npc
283 break
284
285 # npcs always block the tile. So return false if there is an NPC
286 # at the location
287 if current_npc is not None:
288 self.interact_npc (current_npc)
289 return False
290
291 return notblock
292
293 # interaction with npcs
294 def interact_npc (self, npc):
295 # interact with NPC and get the response ID
296 # if the NPC is Bulisa
297
298 # if the NPC is dead cannot talk with the NPC
299 if npc.is_dead is True:
300 self.status_message = "%s is dead! RIP..." % npc.charname
301 return
302
303 if isinstance (npc, npcs.Bulisa):
304 # interact
305 self.interact_npc_bulisa (npc)
306
307 # interact with NPC Bulisa
308 def interact_npc_bulisa (self, npc):
309 # set initial response ID to none
310 resp_id = None
311 # not yet started mission drawing water from well and not refused it
312 if (gamestate.mission_bulisa_water_from_well is False
313 and gamestate.mission_bulisa_water_from_well_refused is False):
314 # set the current dialogue
315 npc.currentdialog = 0
316 # get the response ID
317 resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90)
318 if (gamestate.mission_bulisa_water_from_well_refused is True and
319 gamestate.mission_bulisa_water_from_well is False):
320 # set the current dialog
321 npc.currentdialog = 2
322 resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90)
323 # mission accepted but not completed - check if completed and set value
324 # accordingly
325 elif (gamestate.mission_bulisa_water_from_well is True
326 and gamestate.mission_bulisa_water_from_well_complete is False):
327 for invobj in self.butaba.objects:
328 if isinstance (invobj, gameobjects.Bucket) is True:
329 if invobj.liquid == "water":
330 gamestate.mission_bulisa_water_from_well_complete = True
331 self.butaba.objects.remove (invobj)
332 key1 = gameobjects.Key (5, 3, "a chest key", self.img_key2, constants.KEY_CHEST1)
333 self.butaba.objects.append (key1)
334
335 break
336 # water mission is not completed yet
337 if gamestate.mission_bulisa_water_from_well_complete is False:
338 npc.currentdialog = 1
339 else:
340 npc.currentdialog = 3
341
342 # get the response ID
343 resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90)
344
345 # if response ID is 12, then drawing water from well mission is refused
346 if resp_id == "12" or resp_id == "18":
347 gamestate.mission_bulisa_water_from_well_refused = True
348 # if response ID is 13: that is accepted the drawing water from well mission begins
349 if resp_id == "13" or resp_id == "17":
350 gamestate.mission_bulisa_water_from_well = True
351 # if response ID is none
352 elif resp_id is None:
353 self.status_message = "You cannot initiate a conversation with %s" % npc.charname
354
355 # interaction with objects
356 def interact_objects (self, container, objs):
357 # overall flag for blocking/non-blocking objects
358 notblock = True
359
360 # now perform interaction
361 for obj in objs:
362 # run the object interact function
363 if obj.interact () is False:
364 notblock = False
365 # if object can be picked up ask
366 if obj.can_pickup is True:
367 ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", obj.use_str, "Ignore"], self.img_menu)
368 # if the answer is "pick up"
369 if ans == 1:
370 self.pickup_object (container, obj)
371 elif ans == 2:
372 # use the object according to its type
373 self.use_object (container, obj)
374 # if it cannot be picked up, try to use it anyway
375 else:
376 ans = utility.ask_question (self.screen, "Found %s." % obj.text, [obj.use_str, "Ignore"], self.img_menu)
377 if ans == 1:
378 self.use_object (container, obj)
379
380 return notblock
381
382 # transfer an object from one container to another
383 # container must have an objects list
384 def transfer_object (self, source, obj, dest):
385 # remove object from source
386 source.objects.remove (obj)
387 # add object to destination
388 dest.objects.append (obj)
389
390 # picking up an object
391 def pickup_object (self, container, obj):
392 # only if object can be picked up, pick it up or use it
393 if obj.can_pickup is True:
394 # check if the inventory is full
395 if len (self.butaba.objects) >= constants.MAXITEMS:
396 self.status_message = "Cannot pick up item. Inventory full"
397 else:
398 # add item to inventory
399 self.transfer_object (container, obj, self.butaba)
400
401 self.status_message = "You picked up %s" % obj.text
402
403 # this method uses the object first by calling the object use () method
404 # and then performing specific actions as necessary
405 def use_object (self, container, obj):
406 # if the object is a health potion
407 if isinstance (obj, gameobjects.HealthPotion) is True:
408 if self.butaba.health < constants.MAXHEALTH:
409 obj.use (self.butaba)
410 container.objects.remove (obj)
411 self.status_message = "You gained health"
412 else:
413 self.status_message = "You already have maximum health!"
414 # if the object is a chest
415 elif isinstance (obj, gameobjects.Chest) is True:
416 # if chest is locked, try to open it
417 if obj.locked is True:
418 # try opening the chest with every item 9the use () function
419 # of the chest determines if item is a key anyway
420 fittedkey = None
421 for invobj in self.butaba.objects:
422 fittedkey = obj.use (invobj)
423 # if a key fits
424 if fittedkey is not None:
425 break
426 # if no key found
427 if fittedkey is None:
428 self.status_message = "No key found to open %s" % obj.text
429 # chest successfully unlocked
430 else:
431 self.status_message = "You unlocked the %s" % obj.text
432 # remove the key from Butaba
433 self.butaba.objects.remove (fittedkey)
434 # add an experience point for unlocking chest subject
435 # to a limit of KNOWLEDGEMAX_CHEST_UNLOCK
436 if self.butaba.experience < constants.KNOWLEDGEMAX_CHEST_UNLOCK:
437 self.butaba.experience += 1
438 self.status_message += " and gained experience!"
439 # display the contents of the chest
440 else:
441 item = utility.get_container_object (self.screen, obj, self.img_chestbg, 30)
442 if item is not None:
443 self.interact_objects (obj, [ item, ])
444
445 # if the object is gold coins
446 elif isinstance (obj, gameobjects.GoldCoins) is True:
447 obj.use (self.butaba)
448 self.status_message = "You picked up %d gold." % obj.value
449 # remove the gold coins after adding it to Butaba's gold
450 container.objects.remove (obj)
451 # using a bucket means emptying it
452 elif isinstance (obj, gameobjects.Bucket) is True:
453 if obj.liquid is not None:
454 self.status_message = "You emptied the bucket of %s" % obj.liquid
455 obj.use (self.butaba)
456 else:
457 self.status_message = "Bucket is already empty."
458 # using a well
459 elif isinstance (obj, gameobjects.Well) is True:
460 # if the well is not dry, i.e. it has some liquid
461 if obj.liquid is not None:
462 # search butaba inventory for an empty bucket
463 for invobj in self.butaba.objects:
464 # bucket found, now check if it is empty
465 if isinstance (invobj, gameobjects.Bucket) is True:
466 # if empty fill it
467 if invobj.liquid is None:
468 obj.use (invobj)
469 self.status_message = "You successfully filled the %s with %s" % (invobj.text, obj.liquid)
470 if self.butaba.strength < constants.STRENGTHMAX_DRAW_WELL_WATER:
471 self.butaba.strength += 2
472 self.status_message += " and gained strength!"
473 return
474 self.status_message = "You have no empty bucket to draw %s with!" % obj.liquid
475 else:
476 self.status_message = "%s appears to be dry!" % obj.text
477
478 def move_butaba_left (self):
479 # clear any status messages
480 self.status_message = None
481
482 # first if Butaba is not facing left, make him face left
483 if self.butaba.position <> constants.LEFT:
484 self.butaba.position = constants.LEFT
485 return
486
487 # if butaba is trying to move off the left edge
488 if self.butaba.col <= 0:
489 # if there is a level to the right set current level to that one
490 if self.currentlevel.levelleft is not None:
491 # get the last column of the previous level
492 lastcol = len (self.currentlevel.levelleft.background[0]) - 1
493 # interact with objects if any
494 # if any object is a blocking object then avoid movement
495 if self.level_interact (self.currentlevel.levelleft, self.butaba.row, lastcol) is False:
496 return
497 # make sure there is no obstacle at that position of movement
498 if self.check_background_obstacle (self.currentlevel.levelleft, self.butaba.row, lastcol) is False:
499 self.currentlevel = self.currentlevel.levelleft
500 self.butaba.col = lastcol
501 # normal left movement
502 else:
503 # interact with objects if any
504 # if any object is a blocking object then avoid movement
505 if self.level_interact (self.currentlevel, self.butaba.row, self.butaba.col-1) is False:
506 return
507 if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False:
508 self.butaba.col -= 1
509
510 def move_butaba_right (self):
511 # clear any status messages
512 self.status_message = None
513
514 # First if Butaba is not facing right make him face right
515 if self.butaba.position <> constants.RIGHT:
516 self.butaba.position = constants.RIGHT
517 return
518
519 # if butaba is trying to move off the right edge
520 if self.butaba.col >= len (self.currentlevel.background[0])-1:
521 # if there is a level to the right swap current level with that one
522 if self.currentlevel.levelright is not None:
523 # interact with objects if any
524 # if any object is a blocking object then avoid movement
525 if self.level_interact (self.currentlevel.levelright, self.butaba.row, 0) is False:
526 return
527
528 # make sure there is no obstacle at that position of movement
529 # get the last column of the previous level
530 if self.check_background_obstacle (self.currentlevel.levelright, self.butaba.row, 0) is False:
531 self.currentlevel = self.currentlevel.levelright
532 self.butaba.col = 0
533 # normal right movement
534 else:
535 # interact with objects if any
536 # if any object is a blocking object then avoid moving
537 if self.level_interact (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False:
538 return
539 if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False:
540 self.butaba.col += 1
541
542 def draw_butaba (self):
543 if self.butaba.position == constants.FRONT:
544 self.screen.blit (self.butaba.imagefront, (self.butaba.col*48, self.butaba.row*48))
545 elif self.butaba.position == constants.BACK:
546 self.screen.blit (self.butaba.imageback, (self.butaba.col*48, self.butaba.row*48))
547 elif self.butaba.position == constants.LEFT:
548 self.screen.blit (self.butaba.imageleft, (self.butaba.col*48, self.butaba.row*48))
549 elif self.butaba.position == constants.RIGHT:
550 self.screen.blit (self.butaba.imageright, (self.butaba.col*48, self.butaba.row*48))
551
552
553 # Draw the status infodisplay
554 def draw_status (self):
555 self.screen.blit (self.img_redpotion, (485, 10))
556 utility.put_text (self.screen, 550, 25, 20, (255, 0, 0), "%d" % self.butaba.health)
557
558 self.screen.blit (self.img_lightning, (620, 10))
559 utility.put_text (self.screen, 660, 25, 20, (255,255,255), "%d" % self.butaba.strength)
560
561 self.screen.blit (self.img_wand, (485, 65))
562 utility.put_text (self.screen, 550, 75, 20, (0, 0, 255), "%d" % self.butaba.magic)
563
564 self.screen.blit (self.img_bulb, (620, 65))
565 utility.put_text (self.screen, 660, 75, 20, (0, 255, 0), "%d" % self.butaba.experience)
566
567 self.screen.blit (self.img_goldcoins, (485, 110))
568 utility.put_text (self.screen, 550, 130, 20, (255, 255, 0), "%d" % self.butaba.gold)
569
570 if self.status_message is not None:
571 utility.put_text (self.screen, 10, 485, 10, (255,255, 0), "%s" % self.status_message)
572
573 # display the inventory of the player
574 def draw_inventory (self):
575 # draw the inventory slots
576 r = 1
577 c = 1
578 utility.put_text (self.screen, 490, 170, 16, (255,255 , 0), "Inventory")
579 for i in range (constants.MAXITEMS):
580 self.screen.blit (self.img_inventory, (440+c*54, 150+r*54))
581 if c % 4 == 0:
582 r += 1
583 c = 1
584 else:
585 c += 1
586
587 r = 1
588 c = 1
589 for obj in self.butaba.objects:
590 self.screen.blit (obj.image, (440+c*54+2, 150+r*54+2))
591 if c % 4 == 0:
592 r += 1
593 c = 1
594 else:
595 c += 1
596
597 # Draw the level background tiles on surface
598 def draw_level_background (self, level):
599 i = 0
600 for row in level.background:
601 j = 0
602 for tilerow, tilecol, is_solid in row:
603 tilex = tilecol * 48
604 tiley = tilerow * 48
605 self.screen.blit (self.img_tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48))
606
607 j += 1
608 i += 1
609
610 # Draw the level objects
611 def draw_level_objects (self, level):
612 for obj in level.objects:
613 if obj.image is not None:
614 self.screen.blit (obj.image, (obj.col*48, obj.row*48))
615
616
617 # Draw the NPCs in the level
618 def draw_level_npcs (self, level):
619 for npc in level.npcs:
620 # if npc is not dead then move the NPC randomly depending on their
621 # movement area
622 if npc.is_dead is False:
623 # move this turn?
624 if random.randint (1, 500) < npc.movement_speed:
625 # whether to change direction?
626 if random.randint (1, 100) < 25:
627 npc.position = random.randint (0, 3)
628 else:
629 # if left movement
630 if npc.position == constants.LEFT:
631 # cannot move beyond level and cannot move beyond the
632 # left limit area
633 if npc.col > 0 and npc.col > npc.initcol - npc.leftlimit:
634 # check if there is any background obstacle
635 if self.check_background_obstacle (level, npc.row, npc.col - 1) is False:
636 # check if there are any objects
637 objblock = False
638 npcblock = False
639 for lobj in level.objects:
640 if lobj.row == npc.row and lobj.col == npc.col - 1:
641 objblock = True
642 break
643 # check if there any any npcs blocking
644 for lnpc in level.npcs:
645 if lnpc.row == npc.row and lnpc.col == npc.col - 1:
646 npcblock = True
647 break
648 if objblock is False and npcblock is False:
649 # if butaba is not blocking
650 if self.butaba.row <> npc.row or self.butaba.col <> npc.col - 1:
651 npc.col -= 1
652 elif npc.position == constants.RIGHT:
653 # cannot move beyond level and cannot move beyond the
654 # right limit area
655 if npc.col < 9 and npc.col < npc.initcol + npc.rightlimit:
656 # check if there is any background obstacle
657 if self.check_background_obstacle (level, npc.row, npc.col + 1) is False:
658 # check if there are any objects
659 objblock = False
660 npcblock = False
661 for lobj in level.objects:
662 if lobj.row == npc.row and lobj.col == npc.col + 1:
663 objblock = True
664 break
665 # check if there any any npcs blocking
666 for lnpc in level.npcs:
667 if lnpc.row == npc.row and lnpc.col == npc.col + 1:
668 npcblock = True
669 break
670 if objblock is False and npcblock is False:
671 # if butaba is not blocking
672 if self.butaba.row <> npc.row or self.butaba.col <> npc.col + 1:
673 npc.col += 1
674 elif npc.position == constants.FRONT:
675 # cannot move beyond level and cannot move beyond the
676 # lower bottom limit area
677 if npc.row < 9 and npc.row < npc.initrow + npc.bottomlimit:
678 # check if there is any background obstacle
679 if self.check_background_obstacle (level, npc.row + 1, npc.col) is False:
680 # check if there are any objects
681 objblock = False
682 npcblock = False
683 for lobj in level.objects:
684 if lobj.row == npc.row + 1 and lobj.col == npc.col:
685 objblock = True
686 break
687 # check if there any any npcs blocking
688 for lnpc in level.npcs:
689 if lnpc.row == npc.row + 1 and lnpc.col == npc.col:
690 npcblock = True
691 break
692 if objblock is False and npcblock is False:
693 # if butaba is not blocking
694 if self.butaba.row <> npc.row + 1 or self.butaba.col <> npc.col:
695 npc.row += 1
696 elif npc.position == constants.BACK:
697 # cannot move beyond level and cannot move beyond the
698 # top upper limit area
699 if npc.row > 0 and npc.row > npc.initrow - npc.toplimit:
700 # check if there is any background obstacle
701 if self.check_background_obstacle (level, npc.row - 1, npc.col) is False:
702 # check if there are any objects
703 objblock = False
704 npcblock = False
705 for lobj in level.objects:
706 if lobj.row == npc.row - 1 and lobj.col == npc.col:
707 objblock = True
708 break
709 # check if there any any npcs blocking
710 for lnpc in level.npcs:
711 if lnpc.row == npc.row - 1 and lnpc.col == npc.col:
712 npcblock = True
713 break
714 # if no object is blocking
715 if objblock is False and npcblock is False:
716 # if butaba is not blocking
717 if self.butaba.row <> npc.row - 1 or self.butaba.col <> npc.col:
718 npc.row -= 1
719
720 if npc.position == constants.FRONT:
721 img = npc.imagefront
722 elif npc.position == constants.BACK:
723 img = npc.imageback
724 elif npc.position == constants.LEFT:
725 img = npc.imageleft
726 else:
727 img = npc.imageright
728
729 self.screen.blit (img, (npc.col*48, npc.row*48))