Added a different font
[butaba-adventures.git] / maingame.py
1 import pygame
2 import sys
3 import random
4 import os.path
5
6 import level
7 import butaba
8 import utility
9 import gameobjects
10
11 class MainGame:
12
13 # initialize the game
14 def __init__ (self):
15 pygame.init ()
16 self.screen = pygame.display.set_mode ((720, 512), pygame.FULLSCREEN)
17 pygame.display.set_caption ("The Adventures of Butaba")
18
19 # initalize background graphics
20 self.img_tileset = pygame.image.load (os.path.join ("background", "tileset.png")).convert ()
21
22 self.img_menu = pygame.image.load (os.path.join ("background", "menu_screen.png")).convert ()
23
24 self.img_inventory = pygame.image.load (os.path.join ("background", "inventory.png")).convert ()
25
26
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))
44
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))
54
55 # set level data
56 self.setup_levels ()
57 # set current level and position of our character
58 self.currentlevel = self.level1
59
60 # set the status message
61 self.status_message = "Game started"
62
63 self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT, 75)
64
65 # set up the levels and their interactions
66 def setup_levels (self):
67
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)
74 ]
75 )
76 self.level1.levelright = self.level1e
77 self.level1e.levelleft = self.level1
78
79 def main_loop (self):
80 # main game loop
81 while 1:
82 # clear screen
83 self.screen.fill (pygame.Color (0,0,0))
84 # draw the level
85 self.draw_level_background (self.currentlevel)
86 # draw level objects
87 self.draw_level_objects (self.currentlevel)
88 # draw our character
89 self.draw_butaba ()
90 # display the character's inventory
91 self.draw_inventory ()
92 # draw the status info
93 self.draw_status ()
94 # update the display
95 pygame.display.update ()
96
97 # get keyboard events
98 for event in pygame.event.get ():
99 if event.type == pygame.QUIT:
100 sys.exit (0)
101 # if keyboard event
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 ()
114 # quit the game
115 elif event.key == ord ("q") or event.key == ord ("Q"):
116 sys.exit (0)
117
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."
123 else:
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)
128 break
129
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
136 return
137
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:
146 return
147
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
153 else:
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:
157 return
158
159 if self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False:
160 self.butaba.row -= 1
161
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
168 return
169
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:
177 return
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
181 self.butaba.row = 0
182 # normal downward movement
183 else:
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:
187 return
188 if self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False:
189 self.butaba.row += 1
190
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):
194 return True
195 else:
196 return False
197
198 # get and interact with objects if present in a particular row/col
199 def interact_objects (self, level, row, col):
200 objs = []
201 # get list of objects at current location
202 for obj in level.objects:
203 if obj.row == row and obj.col == col:
204 objs.append (obj)
205
206 # overall flag for blocking/non-blocking objects
207 notblock = True
208 # now perform interaction
209 for obj in objs:
210 # run the object interact function
211 if obj.interact () is False:
212 notblock = False
213 # if object can be picked up ask
214 self.pickup_object (obj)
215
216 return notblock
217
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"
224 if ans == 1:
225 # check if the inventory is full
226 if len (self.butaba.inventory) >= butaba.Butaba.MAXITEMS:
227 self.status_message = "Failed. Inventory full"
228 else:
229 # add item to inventory
230 self.butaba.inventory.append (obj)
231 self.currentlevel.objects.remove (obj)
232
233 self.status_message = "You picked up %s" % obj.text
234 elif ans == 2:
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
238 else:
239 self.use_object (obj)
240
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
258 fittedkey = None
259 for invobj in self.butaba.inventory:
260 fittedkey = obj.use (invobj)
261 # if a key fits
262 if fittedkey is not None:
263 break
264 # if no key found
265 if fittedkey is None:
266 self.status_message = "No key found to open %s" % obj.text
267 # chest successfully unlocked
268 else:
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
273 else:
274 pass
275
276 def move_butaba_left (self):
277 # clear any status messages
278 self.status_message = None
279
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
283 return
284
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:
294 return
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
300 else:
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:
304 return
305 if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False:
306 self.butaba.col -= 1
307
308 def move_butaba_right (self):
309 # clear any status messages
310 self.status_message = None
311
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
315 return
316
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:
324 return
325
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
330 self.butaba.col = 0
331 # normal right movement
332 else:
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:
336 return
337 if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False:
338 self.butaba.col += 1
339
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))
349
350
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)
355
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)
358
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)
361
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)
364
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)
367
368 if self.status_message is not None:
369 utility.put_text (self.screen, 10, 485, 16, (255,255, 0), "%s" % self.status_message)
370
371 # display the inventory of the player
372 def draw_inventory (self):
373 # draw the inventory slots
374 r = 1
375 c = 1
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))
379 if c % 4 == 0:
380 r += 1
381 c = 1
382 else:
383 c += 1
384
385 r = 1
386 c = 1
387 for obj in self.butaba.inventory:
388 self.screen.blit (obj.image, (440+c*54+2, 150+r*54+2))
389 if c % 4 == 0:
390 r += 1
391 c = 1
392 else:
393 c += 1
394
395 # Draw the level background tiles on surface
396 def draw_level_background (self, level):
397 i = 0
398 for row in level.background:
399 j = 0
400 for tilerow, tilecol, is_solid in row:
401 tilex = tilecol * 48
402 tiley = tilerow * 48
403 self.screen.blit (self.img_tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48))
404
405 j += 1
406 i += 1
407
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))