16 self
.screen
= pygame
.display
.set_mode ((720, 512), pygame
.FULLSCREEN
)
17 pygame
.display
.set_caption ("The Adventures of Butaba")
19 # initalize background graphics
20 self
.img_tileset
= pygame
.image
.load (os
.path
.join ("background", "tileset.png")).convert ()
22 self
.img_menu
= pygame
.image
.load (os
.path
.join ("background", "menu_screen.png")).convert ()
24 self
.img_inventory
= pygame
.image
.load (os
.path
.join ("background", "inventory.png")).convert ()
27 # initialize object graphics
28 self
.img_redpotion
= pygame
.image
.load (os
.path
.join ("objects", "red-potion.png")).convert ()
29 self
.img_redpotion
.set_colorkey (pygame
.Color (0, 255, 0))
30 self
.img_goldcoins
= pygame
.image
.load (os
.path
.join ("objects", "gold-coins.png")).convert ()
31 self
.img_goldcoins
.set_colorkey (pygame
.Color (0, 255, 0))
32 self
.img_wand
= pygame
.image
.load (os
.path
.join ("objects", "wand.png")).convert ()
33 self
.img_wand
.set_colorkey (pygame
.Color (0, 255, 0))
34 self
.img_bulb
= pygame
.image
.load (os
.path
.join ("objects", "bulb.png")).convert ()
35 self
.img_bulb
.set_colorkey (pygame
.Color (0, 255, 0))
36 self
.img_lightning
= pygame
.image
.load (os
.path
.join ("objects", "lightning.png")).convert ()
37 self
.img_lightning
.set_colorkey (pygame
.Color (0, 255, 0))
38 self
.img_key
= pygame
.image
.load (os
.path
.join ("objects", "key.png")).convert ()
39 self
.img_key
.set_colorkey (pygame
.Color (0, 255, 0))
40 self
.img_key2
= pygame
.image
.load (os
.path
.join ("objects", "key2.png")).convert ()
41 self
.img_key2
.set_colorkey (pygame
.Color (0, 255, 0))
42 self
.img_chest
= pygame
.image
.load (os
.path
.join ("objects", "chest.png")).convert ()
43 self
.img_chest
.set_colorkey (pygame
.Color (0, 255, 0))
45 # initialize player graphics
46 self
.img_butabafront
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-front.png")).convert ()
47 self
.img_butabafront
.set_colorkey (pygame
.Color (0, 255, 0))
48 self
.img_butababack
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-back.png")).convert ()
49 self
.img_butababack
.set_colorkey (pygame
.Color (0, 255, 0))
50 self
.img_butabaleft
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-left.png")).convert ()
51 self
.img_butabaleft
.set_colorkey (pygame
.Color (0, 255, 0))
52 self
.img_butabaright
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-right.png")).convert ()
53 self
.img_butabaright
.set_colorkey (pygame
.Color (0, 255, 0))
57 # set current level and position of our character
58 self
.currentlevel
= self
.level1
60 # set the status message
61 self
.status_message
= "Game started"
63 self
.butaba
= butaba
.Butaba (5,0, butaba
.Butaba
.RIGHT
, 75)
65 # set up the levels and their interactions
66 def setup_levels (self
):
68 self
.level1
= level
.Level (level
.LEVEL_1
)
69 self
.level1e
= level
.Level (level
.LEVEL_1E
,
70 objects
= [ gameobjects
.Key (4, 3, "a chest key", self
.img_key2
, level
.KEY_CHEST1
),
71 gameobjects
.Key (4, 3, "a room key", self
.img_key
, level
.KEY_ROOM1
),
72 gameobjects
.HealthPotion (4, 2, self
.img_redpotion
),
73 gameobjects
.Chest (2, 5, "chest", self
.img_chest
, level
.KEY_CHEST1
, True)
76 self
.level1
.levelright
= self
.level1e
77 self
.level1e
.levelleft
= self
.level1
83 self
.screen
.fill (pygame
.Color (0,0,0))
85 self
.draw_level_background (self
.currentlevel
)
87 self
.draw_level_objects (self
.currentlevel
)
90 # display the character's inventory
91 self
.draw_inventory ()
92 # draw the status info
95 pygame
.display
.update ()
98 for event
in pygame
.event
.get ():
99 if event
.type == pygame
.QUIT
:
102 if event
.type == pygame
.KEYDOWN
:
103 if event
.key
== pygame
.K_UP
:
104 self
.move_butaba_up ()
105 elif event
.key
== pygame
.K_DOWN
:
106 self
.move_butaba_down ()
107 elif event
.key
== pygame
.K_LEFT
:
108 self
.move_butaba_left ()
109 elif event
.key
== pygame
.K_RIGHT
:
110 self
.move_butaba_right ()
111 # drinking health potion in inventory
112 elif event
.key
== ord ("h") or event
.key
== ord ("H"):
113 self
.inventory_drink_health_potion ()
115 elif event
.key
== ord ("q") or event
.key
== ord ("Q"):
118 # drink a health potion if it is in the player's inventory
119 def inventory_drink_health_potion (self
):
120 # if health is maxed out then ignore
121 if self
.butaba
.health
== butaba
.Butaba
.MAXHEALTH
:
122 self
.status_message
= "You already have maximum health."
124 # look for a health potion
125 for item
in self
.butaba
.inventory
:
126 if isinstance (item
, gameobjects
.HealthPotion
) is True:
127 self
.use_object (item
)
130 def move_butaba_up (self
):
131 # clear any status messages
132 self
.status_message
= None
133 # first if butaba is not facing up, make him face up
134 if self
.butaba
.position
<> butaba
.Butaba
.BACK
:
135 self
.butaba
.position
= butaba
.Butaba
.BACK
138 # if butaba is trying to move off the top of the screen
139 if self
.butaba
.row
<= 0:
140 # if there is a level above set current level to that one
141 if self
.currentlevel
.leveltop
is not None:
142 lastrow
= len (self
.currentlevel
.leveltop
.background
) - 1
143 # if there is any object in that place interact with it
144 # if any object is a blocking object then avoid movement
145 if self
.interact_objects (self
.currentlevel
.leveltop
, lastrow
, self
.butaba
.col
) is False:
148 # make sure there is no obstacle
149 if self
.check_background_obstacle (self
.currentlevel
.leveltop
, lastrow
, self
.butaba
.col
) is False:
150 self
.currentlevel
= self
.currentlevel
.leveltop
151 self
.butaba
.row
= lastrow
152 # normal upward movement
154 # if there is any object in that place interact with it
155 # if any object is a blocking object then avoid movement
156 if self
.interact_objects (self
.currentlevel
, self
.butaba
.row
-1, self
.butaba
.col
) is False:
159 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
-1, self
.butaba
.col
) is False:
162 def move_butaba_down (self
):
163 # clear any status messages
164 self
.status_message
= None
165 # first if butaba is not facing forward, make him face forward/down
166 if self
.butaba
.position
<> butaba
.Butaba
.FRONT
:
167 self
.butaba
.position
= butaba
.Butaba
.FRONT
170 # if butaba is trying to move off the bottom of the screen
171 if self
.butaba
.row
>= len (self
.currentlevel
.background
)-1:
172 # if there is a level below set current level to that one
173 if self
.currentlevel
.levelbottom
is not None:
174 # interact with objects if any
175 # if any object is a blocking object then avoid movement
176 if self
.interact_objects (self
.currentlevel
.levelbottom
, 0, self
.butaba
.col
) is False:
178 # make sure there is no obstacle at that position
179 if self
.check_background_obstacle (self
.currentlevel
.levelbottom
, 0, self
.butaba
.col
) is False:
180 self
.currentlevel
= self
.currentlevel
.levelbottom
182 # normal downward movement
184 # interact with objects if any
185 # if any object is a blocking object then avoid movement
186 if self
.interact_objects (self
.currentlevel
, self
.butaba
.row
+1, self
.butaba
.col
) is False:
188 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
+1, self
.butaba
.col
) is False:
191 # check if a background tile is an obstacle
192 def check_background_obstacle (self
, level
, row
, col
):
193 if (level
.background
[row
][col
][2] == 1):
198 # get and interact with objects if present in a particular row/col
199 def interact_objects (self
, level
, row
, col
):
201 # get list of objects at current location
202 for obj
in level
.objects
:
203 if obj
.row
== row
and obj
.col
== col
:
206 # overall flag for blocking/non-blocking objects
208 # now perform interaction
210 # run the object interact function
211 if obj
.interact () is False:
213 # if object can be picked up ask
214 self
.pickup_object (obj
)
218 # picking up an object
219 def pickup_object (self
, obj
):
220 # only if object can be picked up, pick it up or use it
221 if obj
.can_pickup
is True:
222 ans
= utility
.ask_question (self
.screen
, "Found %s." % obj
.text
, ["Pick up", "Use", "Ignore"], self
.img_menu
)
223 # if the answer is "carry"
225 # check if the inventory is full
226 if len (self
.butaba
.inventory
) >= butaba
.Butaba
.MAXITEMS
:
227 self
.status_message
= "Failed. Inventory full"
229 # add item to inventory
230 self
.butaba
.inventory
.append (obj
)
231 self
.currentlevel
.objects
.remove (obj
)
233 self
.status_message
= "You picked up %s" % obj
.text
235 # use the object according to its type
236 self
.use_object (obj
)
237 # if it cannot be picked up, try to use it anyway
239 self
.use_object (obj
)
241 # this method uses the object first by calling the object use () method
242 # and then performing specific actions as necessary
243 def use_object (self
, obj
):
244 # if the object is a health potion
245 if isinstance (obj
, gameobjects
.HealthPotion
) is True:
246 obj
.use (self
.butaba
)
247 if obj
in self
.currentlevel
.objects
:
248 self
.currentlevel
.objects
.remove (obj
)
249 elif obj
in self
.butaba
.inventory
:
250 self
.butaba
.inventory
.remove (obj
)
251 self
.status_message
= "You gained health"
252 # if the object is a chest
253 elif isinstance (obj
, gameobjects
.Chest
) is True:
254 # if chest is locked, try to open it
255 if obj
.locked
is True:
256 # try opening the chest with every item 9the use () function
257 # of the chest determines if item is a key anyway
259 for invobj
in self
.butaba
.inventory
:
260 fittedkey
= obj
.use (invobj
)
262 if fittedkey
is not None:
265 if fittedkey
is None:
266 self
.status_message
= "No key found to open %s" % obj
.text
267 # chest successfully unlocked
269 self
.status_message
= "You unlocked the %s" % obj
.text
270 # remove the key from inventory
271 self
.butaba
.inventory
.remove (fittedkey
)
272 # display the contents of the chest
276 def move_butaba_left (self
):
277 # clear any status messages
278 self
.status_message
= None
280 # first if Butaba is not facing left, make him face left
281 if self
.butaba
.position
<> butaba
.Butaba
.LEFT
:
282 self
.butaba
.position
= butaba
.Butaba
.LEFT
285 # if butaba is trying to move off the left edge
286 if self
.butaba
.col
<= 0:
287 # if there is a level to the right set current level to that one
288 if self
.currentlevel
.levelleft
is not None:
289 # get the last column of the previous level
290 lastcol
= len (self
.currentlevel
.levelleft
.background
[0]) - 1
291 # interact with objects if any
292 # if any object is a blocking object then avoid movement
293 if self
.interact_objects (self
.currentlevel
.levelleft
, self
.butaba
.row
, lastcol
) is False:
295 # make sure there is no obstacle at that position of movement
296 if self
.check_background_obstacle (self
.currentlevel
.levelleft
, self
.butaba
.row
, lastcol
) is False:
297 self
.currentlevel
= self
.currentlevel
.levelleft
298 self
.butaba
.col
= lastcol
299 # normal left movement
301 # interact with objects if any
302 # if any object is a blocking object then avoid movement
303 if self
.interact_objects (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
-1) is False:
305 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
-1) is False:
308 def move_butaba_right (self
):
309 # clear any status messages
310 self
.status_message
= None
312 # First if Butaba is not facing right make him face right
313 if self
.butaba
.position
<> butaba
.Butaba
.RIGHT
:
314 self
.butaba
.position
= butaba
.Butaba
.RIGHT
317 # if butaba is trying to move off the right edge
318 if self
.butaba
.col
>= len (self
.currentlevel
.background
[0])-1:
319 # if there is a level to the right swap current level with that one
320 if self
.currentlevel
.levelright
is not None:
321 # interact with objects if any
322 # if any object is a blocking object then avoid movement
323 if self
.interact_objects (self
.currentlevel
.levelright
, self
.butaba
.row
, 0) is False:
326 # make sure there is no obstacle at that position of movement
327 # get the last column of the previous level
328 if self
.check_background_obstacle (self
.currentlevel
.levelright
, self
.butaba
.row
, 0) is False:
329 self
.currentlevel
= self
.currentlevel
.levelright
331 # normal right movement
333 # interact with objects if any
334 # if any object is a blocking object then avoid moving
335 if self
.interact_objects (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
+ 1) is False:
337 if self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
+ 1) is False:
340 def draw_butaba (self
):
341 if self
.butaba
.position
== butaba
.Butaba
.FRONT
:
342 self
.screen
.blit (self
.img_butabafront
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
343 elif self
.butaba
.position
== butaba
.Butaba
.BACK
:
344 self
.screen
.blit (self
.img_butababack
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
345 elif self
.butaba
.position
== butaba
.Butaba
.LEFT
:
346 self
.screen
.blit (self
.img_butabaleft
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
347 elif self
.butaba
.position
== butaba
.Butaba
.RIGHT
:
348 self
.screen
.blit (self
.img_butabaright
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
351 # Draw the status infodisplay
352 def draw_status (self
):
353 self
.screen
.blit (self
.img_redpotion
, (485, 10))
354 utility
.put_text (self
.screen
, 550, 25, 20, (255, 0, 0), "%d" % self
.butaba
.health
)
356 self
.screen
.blit (self
.img_lightning
, (620, 10))
357 utility
.put_text (self
.screen
, 660, 25, 20, (255,255,255), "%d" % self
.butaba
.strength
)
359 self
.screen
.blit (self
.img_wand
, (485, 65))
360 utility
.put_text (self
.screen
, 550, 75, 20, (0, 0, 255), "%d" % self
.butaba
.magic
)
362 self
.screen
.blit (self
.img_bulb
, (620, 65))
363 utility
.put_text (self
.screen
, 660, 75, 20, (0, 255, 0), "%d" % self
.butaba
.experience
)
365 self
.screen
.blit (self
.img_goldcoins
, (485, 110))
366 utility
.put_text (self
.screen
, 550, 130, 20, (255, 255, 0), "%d" % self
.butaba
.gold
)
368 if self
.status_message
is not None:
369 utility
.put_text (self
.screen
, 10, 485, 16, (255,255, 0), "%s" % self
.status_message
)
371 # display the inventory of the player
372 def draw_inventory (self
):
373 # draw the inventory slots
376 utility
.put_text (self
.screen
, 490, 170, 16, (255,255 , 0), "Inventory")
377 for i
in range (butaba
.Butaba
.MAXITEMS
):
378 self
.screen
.blit (self
.img_inventory
, (440+c
*54, 150+r
*54))
387 for obj
in self
.butaba
.inventory
:
388 self
.screen
.blit (obj
.image
, (440+c
*54+2, 150+r
*54+2))
395 # Draw the level background tiles on surface
396 def draw_level_background (self
, level
):
398 for row
in level
.background
:
400 for tilerow
, tilecol
, is_solid
in row
:
403 self
.screen
.blit (self
.img_tileset
, (j
*48, i
*48), pygame
.Rect (tilex
, tiley
, 48, 48))
408 # Draw the level objects
409 def draw_level_objects (self
, level
):
410 for obj
in level
.objects
:
411 if obj
.image
is not None:
412 self
.screen
.blit (obj
.image
, (obj
.col
*48, obj
.row
*48))