19 self
.screen
= pygame
.display
.set_mode ((720, 512))
20 pygame
.display
.set_caption ("The Adventures of Butaba")
22 # initalize background graphics
23 self
.img_tileset
= pygame
.image
.load (os
.path
.join ("background", "tileset.png")).convert ()
25 self
.img_menu
= pygame
.image
.load (os
.path
.join ("background", "menu_screen.png")).convert ()
27 self
.img_inventory
= pygame
.image
.load (os
.path
.join ("background", "inventory.png")).convert ()
29 self
.img_chestbg
= pygame
.image
.load (os
.path
.join ("background", "chestcontent.png")).convert ()
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))
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))
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))
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))
67 # initialize NPC portraits
68 self
.img_bulisa_portrait
= pygame
.image
.load (os
.path
.join ("portraits", "bulisa.png")).convert ()
72 # set current level and position of our character
73 self
.currentlevel
= self
.level1
75 # set the status message
76 self
.status_message
= "Game started"
78 self
.butaba
= butaba
.Butaba (5,0, butaba
.Butaba
.RIGHT
)
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
)
94 npc_bulisa
= npcs
.Bulisa (4, 3, self
.img_bulisa
, self
.img_bulisa_portrait
,
95 [ os
.path
.join ("dialogues", "bulisa1.dlg") ])
97 chest1
.objects
= [ gold50
, gold25
, potion2
, potion3
, key2
, gold10
]
100 self
.level1
= level
.Level (cPickle
.load (file ("levels/level1.dat")),
101 objects
= [ chest1
] )
103 self
.level1w
= level
.Level (cPickle
.load (file ("levels/level1w.dat")))
105 self
.level1e
= level
.Level (cPickle
.load (file ("levels/level1e.dat")),
106 objects
= [ key1
, potion
, chest2
], npcs
= [ npc_bulisa
])
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
114 def main_loop (self
):
118 self
.screen
.fill (pygame
.Color (0,0,0))
120 self
.draw_level_background (self
.currentlevel
)
122 self
.draw_level_objects (self
.currentlevel
)
123 # draw the NPCs in the level
124 self
.draw_level_npcs (self
.currentlevel
)
127 # display the character's inventory
128 self
.draw_inventory ()
129 # draw the status info
132 pygame
.display
.update ()
134 # get keyboard events
135 for event
in pygame
.event
.get ():
136 if event
.type == pygame
.QUIT
:
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 ()
152 elif event
.key
== ord ("q") or event
.key
== ord ("Q"):
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
)
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
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:
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
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:
191 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
-1, self
.butaba
.col
) is False:
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
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:
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
214 # normal downward movement
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:
220 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
+1, self
.butaba
.col
) is False:
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):
230 # get and interact with objects and characters if present in a particular row/col
231 def level_interact (self
, level
, row
, col
):
233 # get list of objects at current location
234 for obj
in level
.objects
:
235 if obj
.row
== row
and obj
.col
== col
:
238 notblock
= self
.interact_objects (level
, objs
)
240 # get npc at current location
242 for npc
in level
.npcs
:
243 if npc
.row
== row
and npc
.col
== col
:
247 # npcs always block the tile. So return false if there is an NPC
249 if current_npc
is not None:
250 self
.interact_npc (current_npc
)
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)
262 self
.status_message
= "You cannot initiate a conversation with %s" % npc
.charname
264 # interaction with objects
265 def interact_objects (self
, container
, objs
):
266 # overall flag for blocking/non-blocking objects
269 # now perform interaction
271 # run the object interact function
272 if obj
.interact () is 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"
279 self
.pickup_object (container
, obj
)
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
285 ans
= utility
.ask_question (self
.screen
, "Found %s." % obj
.text
, [obj
.use_str
, "Ignore"], self
.img_menu
)
287 self
.use_object (container
, obj
)
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
)
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"
307 # add item to inventory
308 self
.transfer_object (container
, obj
, self
.butaba
)
310 self
.status_message
= "You picked up %s" % obj
.text
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"
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
330 for invobj
in self
.butaba
.objects
:
331 fittedkey
= obj
.use (invobj
)
333 if fittedkey
is not None:
336 if fittedkey
is None:
337 self
.status_message
= "No key found to open %s" % obj
.text
338 # chest successfully unlocked
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
350 item
= utility
.get_container_object (self
.screen
, obj
, self
.img_chestbg
, 30)
352 self
.interact_objects (obj
, [ item
, ])
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
)
361 def move_butaba_left (self
):
362 # clear any status messages
363 self
.status_message
= None
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
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:
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
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:
390 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
-1) is False:
393 def move_butaba_right (self
):
394 # clear any status messages
395 self
.status_message
= None
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
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:
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
416 # normal right movement
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:
422 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
+ 1) is False:
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))
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
)
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
)
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
)
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
)
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
)
453 if self
.status_message
is not None:
454 utility
.put_text (self
.screen
, 10, 485, 10, (255,255, 0), "%s" % self
.status_message
)
456 # display the inventory of the player
457 def draw_inventory (self
):
458 # draw the inventory slots
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))
472 for obj
in self
.butaba
.objects
:
473 self
.screen
.blit (obj
.image
, (440+c
*54+2, 150+r
*54+2))
480 # Draw the level background tiles on surface
481 def draw_level_background (self
, level
):
483 for row
in level
.background
:
485 for tilerow
, tilecol
, is_solid
in row
:
488 self
.screen
.blit (self
.img_tileset
, (j
*48, i
*48), pygame
.Rect (tilex
, tiley
, 48, 48))
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))
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))