Initial commit
authorHarishankar <v.harishankar@gmail.com>
Thu, 29 Sep 2011 07:31:57 +0000 (13:01 +0530)
committerHarishankar <v.harishankar@gmail.com>
Thu, 29 Sep 2011 07:31:57 +0000 (13:01 +0530)
Initial commit for the game

15 files changed:
background/tileset.png [new file with mode: 0644]
butaba [new file with mode: 0755]
butaba.py [new file with mode: 0644]
font/HarisComic-2.ttf [new file with mode: 0644]
level.py [new file with mode: 0644]
maingame.py [new file with mode: 0644]
objects/bulb.png [new file with mode: 0644]
objects/gold-coins.png [new file with mode: 0644]
objects/lightning.png [new file with mode: 0644]
objects/red-potion.png [new file with mode: 0644]
objects/wand.png [new file with mode: 0644]
sprite/butaba-back.png [new file with mode: 0644]
sprite/butaba-front.png [new file with mode: 0644]
sprite/butaba-left.png [new file with mode: 0644]
sprite/butaba-right.png [new file with mode: 0644]

diff --git a/background/tileset.png b/background/tileset.png
new file mode 100644 (file)
index 0000000..a891310
Binary files /dev/null and b/background/tileset.png differ
diff --git a/butaba b/butaba
new file mode 100755 (executable)
index 0000000..2165ef1
--- /dev/null
+++ b/butaba
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+
+# main game playing script
+
+import maingame
+
+game = maingame.MainGame ()
+game.main_loop ()
\ No newline at end of file
diff --git a/butaba.py b/butaba.py
new file mode 100644 (file)
index 0000000..c59ee11
--- /dev/null
+++ b/butaba.py
@@ -0,0 +1,22 @@
+# Main player Butaba class
+
+class Butaba:
+       # Position definitions
+       LEFT = 0
+       RIGHT = 1
+       FRONT = 2
+       BACK = 3
+       MAXITEMS = 8
+
+       # initialize our character
+       def __init__ (self, startrow, startcol, position=LEFT, health=100, magic=10, experience=10, strength=10, gold=0, inventory = []):
+               self.position = position
+               self.row = startrow
+               self.col = startcol
+               self.magic = magic
+               self.experience = experience
+               self.strength = strength
+               self.health = health
+               self.inventory = inventory
+               self.gold = gold
+
diff --git a/font/HarisComic-2.ttf b/font/HarisComic-2.ttf
new file mode 100644 (file)
index 0000000..8c69af2
Binary files /dev/null and b/font/HarisComic-2.ttf differ
diff --git a/level.py b/level.py
new file mode 100644 (file)
index 0000000..d464a20
--- /dev/null
+++ b/level.py
@@ -0,0 +1,27 @@
+# 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
diff --git a/maingame.py b/maingame.py
new file mode 100644 (file)
index 0000000..b70d48c
--- /dev/null
@@ -0,0 +1,205 @@
+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
+
diff --git a/objects/bulb.png b/objects/bulb.png
new file mode 100644 (file)
index 0000000..374086d
Binary files /dev/null and b/objects/bulb.png differ
diff --git a/objects/gold-coins.png b/objects/gold-coins.png
new file mode 100644 (file)
index 0000000..c5228ac
Binary files /dev/null and b/objects/gold-coins.png differ
diff --git a/objects/lightning.png b/objects/lightning.png
new file mode 100644 (file)
index 0000000..35a09d6
Binary files /dev/null and b/objects/lightning.png differ
diff --git a/objects/red-potion.png b/objects/red-potion.png
new file mode 100644 (file)
index 0000000..243f25f
Binary files /dev/null and b/objects/red-potion.png differ
diff --git a/objects/wand.png b/objects/wand.png
new file mode 100644 (file)
index 0000000..f8c68b4
Binary files /dev/null and b/objects/wand.png differ
diff --git a/sprite/butaba-back.png b/sprite/butaba-back.png
new file mode 100644 (file)
index 0000000..d453f08
Binary files /dev/null and b/sprite/butaba-back.png differ
diff --git a/sprite/butaba-front.png b/sprite/butaba-front.png
new file mode 100644 (file)
index 0000000..2bbfcb1
Binary files /dev/null and b/sprite/butaba-front.png differ
diff --git a/sprite/butaba-left.png b/sprite/butaba-left.png
new file mode 100644 (file)
index 0000000..feccd1b
Binary files /dev/null and b/sprite/butaba-left.png differ
diff --git a/sprite/butaba-right.png b/sprite/butaba-right.png
new file mode 100644 (file)
index 0000000..30a5321
Binary files /dev/null and b/sprite/butaba-right.png differ