From 7dbb8e9b6766b7c0c17433bea3c88a321036b837 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Thu, 29 Sep 2011 16:16:21 +0530 Subject: [PATCH] Objects added to game rendering Added object class for representing in-game objects Also added rendering for in-game objects, but not yet added interaction with objects. --- level.py | 58 +++++++++++++++++++++++++++++++++++------------- maingame.py | 47 +++++++++++++++++++++++++-------------- object.py | 41 ++++++++++++++++++++++++++++++++++ objects/key.png | Bin 0 -> 471 bytes utility.py | 29 ++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 32 deletions(-) create mode 100644 object.py create mode 100644 objects/key.png create mode 100644 utility.py diff --git a/level.py b/level.py index d464a20..a449365 100644 --- a/level.py +++ b/level.py @@ -1,27 +1,55 @@ # 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 diff --git a/maingame.py b/maingame.py index b70d48c..af06593 100644 --- a/maingame.py +++ b/maingame.py @@ -5,6 +5,8 @@ import os.path import level import butaba +import utility +import object class MainGame: @@ -29,6 +31,9 @@ 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)) @@ -48,13 +53,12 @@ class MainGame: # 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 @@ -62,11 +66,13 @@ class MainGame: # 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 () @@ -177,21 +183,23 @@ class MainGame: 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 @@ -203,3 +211,8 @@ class MainGame: 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)) diff --git a/object.py b/object.py new file mode 100644 index 0000000..f6638f1 --- /dev/null +++ b/object.py @@ -0,0 +1,41 @@ +# object classes - classes for game interactive objects +import pygame +import os.path + +import utility + +# base class for all objects +class GameObject: + # initialization routine + def __init__ (self, row, col, image = None): + self.row = row + self.col = col + self.image = image + + # override this for interaction + def interact (self): + pass + + # use the object on another object + def use (self, otherobject): + pass + + +class Key (GameObject): + def __init__ (self, row, col, image, key_id): + self.key_id = key_id + GameObject.__init__ (self, row, col, image) + + # ask whether to pick up the key + def interact (self, surface): + ans = utility.ask_question (surface, "You found a key.", ["Pick it up", "Leave it"]) + return ans + + # using the key + def use (self, lockitem): + if type (lockitem) == Chest or type (lockitem) == Door: + if self.key_id == lockitem.key_id: + if lockitem.unlocked is False: + lockitem.unlocked = True + else: + lockitem.unlocked = True diff --git a/objects/key.png b/objects/key.png new file mode 100644 index 0000000000000000000000000000000000000000..69f35fd53dee6ffb5c94d717ef46e49162383479 GIT binary patch literal 471 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1SD@HFdgVn^Trci!MP|ku_QG`p**uBL&4qCHz2%`PaJ5Kk*AAeNW|f{H|+fm8;G!dIN!0yPd+X??hB{syO884u5< zdtKu^U=ewAEz5^pb=Fp{Kg;ZbAFlqgF{*|4Ri@R}RNded&ZYlzjy2w9-?{uH%Zkij xLE`tNSJh6wpK|oZmD$~mKlGJf