--- /dev/null
+# level.py - level data and class
+# level data
+# tuple represents a background tile
+# first item is tile row
+# second item is tile col
+# 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) ]]
+
+# Class to represent levels
+class Level:
+ def __init__ (self, bgdata, levelleft=None, levelright=None, leveltop = None, levelbottom = None):
+ 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
--- /dev/null
+import pygame
+import sys
+import random
+import os.path
+
+import level
+import butaba
+
+class MainGame:
+
+ # initialize the game
+ def __init__ (self):
+ pygame.init ()
+ self.screen = pygame.display.set_mode ((640, 480))
+ pygame.display.set_caption ("The Adventures of Butaba")
+
+ # initalize background graphics
+ self.tileset = pygame.image.load (os.path.join ("background", "tileset.png")).convert ()
+
+ # initialize object graphics
+ self.redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert ()
+ self.redpotion.set_colorkey (pygame.Color (0, 255, 0))
+ self.goldcoins = pygame.image.load (os.path.join ("objects", "gold-coins.png")).convert ()
+ self.goldcoins.set_colorkey (pygame.Color (0, 255, 0))
+ self.wand = pygame.image.load (os.path.join ("objects", "wand.png")).convert ()
+ self.wand.set_colorkey (pygame.Color (0, 255, 0))
+ self.bulb = pygame.image.load (os.path.join ("objects", "bulb.png")).convert ()
+ self.bulb.set_colorkey (pygame.Color (0, 255, 0))
+ self.lightning = pygame.image.load (os.path.join ("objects", "lightning.png")).convert ()
+ self.lightning.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))
+ self.butababack = pygame.image.load (os.path.join ("sprite", "butaba-back.png")).convert ()
+ self.butababack.set_colorkey (pygame.Color (0, 255, 0))
+ self.butabaleft = pygame.image.load (os.path.join ("sprite", "butaba-left.png")).convert ()
+ self.butabaleft.set_colorkey (pygame.Color (0, 255, 0))
+ self.butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert ()
+ self.butabaright.set_colorkey (pygame.Color (0, 255, 0))
+
+ # set level data
+ self.setup_levels ()
+ # set current level and position of our character
+ self.currentlevel = self.level1
+
+ self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT)
+
+ # 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))
+
+ def main_loop (self):
+ # main game loop
+ while 1:
+ # clear screen
+ self.screen.fill (pygame.Color (0,0,0))
+ # draw the level
+ self.draw_level (self.currentlevel)
+ # draw our character
+ self.draw_butaba ()
+ # draw the status info
+ self.draw_information ()
+ # update the display
+ pygame.display.update ()
+
+ # get keyboard events
+ for event in pygame.event.get ():
+ if event.type == pygame.QUIT:
+ sys.exit (0)
+ if event.type == pygame.KEYDOWN:
+ if event.key == pygame.K_UP:
+ self.move_butaba_up ()
+ elif event.key == pygame.K_DOWN:
+ self.move_butaba_down ()
+ elif event.key == pygame.K_LEFT:
+ self.move_butaba_left ()
+ elif event.key == pygame.K_RIGHT:
+ self.move_butaba_right ()
+
+ def move_butaba_up (self):
+ # first if butaba is not facing up, make him face up
+ if self.butaba.position <> butaba.Butaba.BACK:
+ self.butaba.position = butaba.Butaba.BACK
+ return
+
+ # if butaba is trying to move off the top of the screen
+ if self.butaba.row <= 0:
+ # if there is a level above set current level to that one
+ if self.currentlevel.leveltop is not None:
+ # make sure there is no obstacle
+ lastrow = len (self.currentlevel.leveltop.background) - 1
+ if self.check_background_obstacle (self.currentlevel.leveltop, lastrow, self.butaba.col) is False:
+ self.currentlevel = self.currentlevel.leveltop
+ self.butaba.row = lastrow
+ # normal upward movement
+ elif self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False:
+ self.butaba.row -= 1
+
+ def move_butaba_down (self):
+ # first if butaba is not facing forward, make him face forward/down
+ if self.butaba.position <> butaba.Butaba.FRONT:
+ self.butaba.position = butaba.Butaba.FRONT
+ return
+
+ # if butaba is trying to move off the bottom of the screen
+ if self.butaba.row >= len (self.currentlevel.background)-1:
+ # if there is a level below set current level to that one
+ if self.currentlevel.levelbottom is not None:
+ # make sure there is no obstacle at that position
+ if self.check_background_obstacle (self.currentlevel.levelbottom, 0, self.butaba.col) is False:
+ self.currentlevel = self.currentlevel.levelbottom
+ self.butaba.row = 0
+ # normal downward movement
+ elif self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False:
+ self.butaba.row += 1
+
+ # check if a background tile is an obstacle
+ def check_background_obstacle (self, level, row, col):
+ if (level.background[row][col][2] == 1):
+ return True
+ else:
+ return False
+
+ def move_butaba_left (self):
+ # first if Butaba is not facing left, make him face left
+ if self.butaba.position <> butaba.Butaba.LEFT:
+ self.butaba.position = butaba.Butaba.LEFT
+ return
+
+ # if butaba is trying to move off the left edge
+ if self.butaba.col <= 0:
+ # if there is a level to the right set current level to that one
+ if self.currentlevel.levelleft is not None:
+ # make sure there is no obstacle at that position of movement
+ # get the last column of the previous level
+ lastcol = len (self.currentlevel.levelleft.background[0]) - 1
+ if self.check_background_obstacle (self.currentlevel.levelleft, self.butaba.row, lastcol) is False:
+ self.currentlevel = self.currentlevel.levelleft
+ self.butaba.col = lastcol
+ # normal left movement
+ elif self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False:
+ self.butaba.col -= 1
+
+ def move_butaba_right (self):
+ # First if Butaba is not facing right make him face right
+ if self.butaba.position <> butaba.Butaba.RIGHT:
+ self.butaba.position = butaba.Butaba.RIGHT
+ return
+
+ # if butaba is trying to move off the right edge
+ if self.butaba.col >= len (self.currentlevel.background[0])-1:
+ # if there is a level to the right swap current level with that one
+ if self.currentlevel.levelright is not None:
+ # make sure there is no obstacle at that position of movement
+ # get the last column of the previous level
+ if self.check_background_obstacle (self.currentlevel.levelright, self.butaba.row, 0) is False:
+ self.currentlevel = self.currentlevel.levelright
+ self.butaba.col = 0
+ # normal right movement
+ elif self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False:
+ self.butaba.col += 1
+
+ def draw_butaba (self):
+ if self.butaba.position == butaba.Butaba.FRONT:
+ self.screen.blit (self.butabafront, (self.butaba.col*48, self.butaba.row*48))
+ elif self.butaba.position == butaba.Butaba.BACK:
+ self.screen.blit (self.butababack, (self.butaba.col*48, self.butaba.row*48))
+ elif self.butaba.position == butaba.Butaba.LEFT:
+ self.screen.blit (self.butabaleft, (self.butaba.col*48, self.butaba.row*48))
+ 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):
+ self.screen.blit (self.redpotion, (485, 10))
+ self.put_text (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)
+ self.screen.blit (self.wand, (485, 120))
+ self.put_text (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)
+ self.screen.blit (self.lightning, (485, 240))
+ self.put_text (550, 250, 28, (255,255,255), "%d" % self.butaba.strength)
+
+ # Draw the level background tiles
+ def draw_level (self, level):
+ i = 0
+ for row in level.background:
+ j = 0
+ for tilerow, tilecol, is_solid in row:
+ tilex = tilecol * 48
+ tiley = tilerow * 48
+ self.screen.blit (self.tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48))
+
+ j += 1
+ i += 1
+