# level.py - level data and class
-# level data
-# tuple represents a background tile
-# first item is tile row
-# second item is tile col
+
+import object
+
+# Background level data
+# A level is a list of list of tuples. Level is a 10x10 room of 48 pixel images
+#
+# every tuple represents a single background tile
+# first item is tile row in the tileset
+# second item is tile col in the tilest
# third item defines whether solid or not (0 or 1)
-LEVEL_1 = [ [ (0, 0, 0), (1, 0, 0), (0, 5, 1), (0, 6, 1), (1, 0, 0), (0, 0, 0), (1, 0, 0), (1, 0, 0), (2, 0, 1), (1, 0, 0) ],
- [ (0, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (0, 0, 0), (3, 8, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1) ],
- [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
- [ (0, 0, 0), (1, 0, 0), (0, 0, 0), (2, 0, 1), (0, 0, 0), (1, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
- [ (4, 5, 0), (4, 5, 0), (4, 5, 0), (4, 5, 0), (4, 5, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
- [ (5, 5, 0), (5, 5, 0), (5, 5, 0), (5, 5, 0), (5, 5, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
- [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
- [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (1,11, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
- [ (0, 0, 0), (0, 0, 0), (0, 5, 1), (0, 6, 1), (0, 0, 0), (0,10, 1), (0, 7, 1), (0, 7, 1), (1,10, 1), (0, 7, 1) ],
- [ (1, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (1, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (2, 0, 1), (1, 0, 0) ]]
+# constants for in-game use
+KEY_CHEST1 = 1000
+
+# start level
+LEVEL_1 = [
+ [ (0, 0, 0), (1, 0, 0), (0, 5, 1), (0, 6, 1), (1, 0, 0), (0, 0, 0), (1, 0, 0), (1, 0, 0), (2, 0, 1), (1, 0, 0) ],
+ [ (0, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (0, 0, 0), (3, 8, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1) ],
+ [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
+ [ (0, 0, 0), (1, 0, 0), (0, 0, 0), (2, 0, 1), (0, 0, 0), (1, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
+ [ (4, 5, 0), (4, 5, 0), (4, 5, 0), (4, 5, 0), (4, 5, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
+ [ (5, 5, 0), (5, 5, 0), (5, 5, 0), (5, 5, 0), (5, 5, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
+ [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
+ [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (1,11, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ],
+ [ (0, 0, 0), (0, 0, 0), (0, 5, 1), (0, 6, 1), (0, 0, 0), (0,10, 1), (0, 7, 1), (0, 7, 1), (1,10, 1), (0, 7, 1) ],
+ [ (1, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (1, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (2, 0, 1), (1, 0, 0) ]
+]
+
+# level to the east of start level
+LEVEL_1E = [
+ [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0) ],
+ [ (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (3, 9, 1), (0, 0, 0), (0, 0, 0), (2, 0, 0) ],
+ [ (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (0, 0, 0), (0, 0, 0) ],
+ [ (3,10, 0), (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 6, 0), (3, 5, 0) ],
+ [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 5, 0), (3, 6, 0) ],
+ [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 6, 0), (3, 5, 0) ],
+ [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 5, 0), (3, 6, 0) ],
+ [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (0, 0, 0), (0, 0, 0) ],
+ [ (0, 7, 1), (3 ,7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (1,10, 1), (0,11, 1), (0, 0, 0), (0, 0, 0), (2, 0, 0) ],
+ [ (0, 0, 0), (0, 0, 0), (1, 0, 0), (3, 4, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0) ]
+]
# Class to represent levels
class Level:
- def __init__ (self, bgdata, levelleft=None, levelright=None, leveltop = None, levelbottom = None):
+ def __init__ (self, bgdata, levelleft=None, levelright=None, leveltop = None, levelbottom = None, objects=[]):
self.background = bgdata
# portals for level above, below, left or right of the character
self.levelleft = levelleft
self.levelright = levelright
self.leveltop = leveltop
self.levelbottom = levelbottom
+
+ # objects in the level as a list
+ self.objects = objects
import level
import butaba
+import utility
+import object
class MainGame:
self.lightning = pygame.image.load (os.path.join ("objects", "lightning.png")).convert ()
self.lightning.set_colorkey (pygame.Color (0, 255, 0))
+ self.key = pygame.image.load (os.path.join ("objects", "key.png")).convert ()
+ self.key.set_colorkey (pygame.Color (0, 255, 0))
+
# initialize player graphics
self.butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert ()
self.butabafront.set_colorkey (pygame.Color (0, 255, 0))
# set up the levels and their interactions
def setup_levels (self):
- self.level1 = level.Level (level.LEVEL_1)
- # function to draw text
- def put_text (self, x, y, size, (r,g,b), text):
- harisfont = os.path.join ("font", "HarisComic-2.ttf")
- surf = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b))
- self.screen.blit (surf, (x, y))
+ self.level1 = level.Level (level.LEVEL_1)
+ self.level1e = level.Level (level.LEVEL_1E,
+ objects = [ object.Key (4, 3, self.key, level.KEY_CHEST1) ])
+ self.level1.levelright = self.level1e
+ self.level1e.levelleft = self.level1
def main_loop (self):
# main game loop
# clear screen
self.screen.fill (pygame.Color (0,0,0))
# draw the level
- self.draw_level (self.currentlevel)
+ self.draw_level_background (self.currentlevel)
+ # draw level objects
+ self.draw_level_objects (self.currentlevel)
# draw our character
self.draw_butaba ()
# draw the status info
- self.draw_information ()
+ self.draw_status ()
# update the display
pygame.display.update ()
elif self.butaba.position == butaba.Butaba.RIGHT:
self.screen.blit (self.butabaright, (self.butaba.col*48, self.butaba.row*48))
- # Draw the sidebar infodisplay
- def draw_information (self):
+
+ # Draw the status infodisplay
+ def draw_status (self):
self.screen.blit (self.redpotion, (485, 10))
- self.put_text (550, 25, 28, (255, 0, 0), "%d" % self.butaba.health)
+ utility.put_text (self.screen, 550, 25, 28, (255, 0, 0), "%d" % self.butaba.health)
self.screen.blit (self.goldcoins, (485, 50))
- self.put_text (550, 75, 28, (255, 255, 0), "%d" % self.butaba.gold)
+ utility.put_text (self.screen, 550, 75, 28, (255, 255, 0), "%d" % self.butaba.gold)
self.screen.blit (self.wand, (485, 120))
- self.put_text (550, 130, 28, (0, 0, 255), "%d" % self.butaba.magic)
+ utility.put_text (self.screen, 550, 130, 28, (0, 0, 255), "%d" % self.butaba.magic)
self.screen.blit (self.bulb, (485, 180))
- self.put_text (550, 190, 28, (0, 255, 0), "%d" % self.butaba.experience)
+ utility.put_text (self.screen, 550, 190, 28, (0, 255, 0), "%d" % self.butaba.experience)
self.screen.blit (self.lightning, (485, 240))
- self.put_text (550, 250, 28, (255,255,255), "%d" % self.butaba.strength)
+ utility.put_text (self.screen, 550, 250, 28, (255,255,255), "%d" % self.butaba.strength)
+
- # Draw the level background tiles
- def draw_level (self, level):
+ # Draw the level background tiles on surface
+ def draw_level_background (self, level):
i = 0
for row in level.background:
j = 0
j += 1
i += 1
+ # Draw the level objects
+ def draw_level_objects (self, level):
+ for obj in level.objects:
+ if obj.image is not None:
+ self.screen.blit (obj.image, (obj.col*48, obj.row*48))