From 5075eb87b2a41e7f93c81a3ad2cfc1ac94a11314 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sat, 1 Oct 2011 13:51:57 +0530 Subject: [PATCH] Started on the level editor Began work on the level editor for the game --- gameobjects.py | 15 +++++ leveleditor.py | 133 +++++++++++++++++++++++++++++++++++++++++ maingame.py | 14 ++++- objects/gold-coins.png | Bin 443 -> 460 bytes objects/red-potion.png | Bin 606 -> 628 bytes 5 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 leveleditor.py diff --git a/gameobjects.py b/gameobjects.py index e64dc91..b4992a3 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -25,6 +25,21 @@ class GameObject: def use (self, otherobject): pass +class GoldCoins (GameObject): + # initialize + def __init__ (self, row, col, image, value): + text = "gold coins" + self.value = value + GameObject.__init__ (self, row, col, text, image, False) + + # no interaction with this object + def interact (self): + return True + + # use the object on Butaba - add to his gold + def use (self, butaba): + butaba.gold += self.value + class HealthPotion (GameObject): # initialize diff --git a/leveleditor.py b/leveleditor.py new file mode 100644 index 0000000..784199b --- /dev/null +++ b/leveleditor.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 + +# level designer gui - this is used to design level graphics +# very rough and ready - main purpose is to allow quick game +# design. inputting levels by hand array is very tough + +import pygame +import os.path +import sys +import cPickle + +import utility + +# draw the level actually +def draw_level (screen, leveldata, tileset): + i = 0 + for row in leveldata: + j = 0 + for tilerow, tilecol, is_solid in row: + tilex = tilecol * 48 + tiley = tilerow * 48 + screen.blit (tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48)) + if is_solid == 1: + utility.put_text (screen, j*48+2, i*48+2, 10, (255,255,0), "w") + j += 1 + i += 1 + +def display_menu (screen): + utility.put_text (screen, 490, 10, 10, (255,255,0), "p to pick tile") + utility.put_text (screen, 490, 50, 10, (255,255,0), "w to wall (solid)") + utility.put_text (screen, 490, 90, 10, (255,255,0), " to place") + utility.put_text (screen, 490, 130, 10,(255,255,0), "Arrows to move around") + utility.put_text (screen, 490, 170, 10, (255,255,0), "s to Save level") + utility.put_text (screen, 490, 210, 10, (255,255,0), "q to Quit editor") + +# draw the tile cursor +def draw_cursor (screen, currow, curcol): + pygame.draw.rect (screen, (255,255,255), (curcol*48, currow*48, 48, 48), 1) + +# make wall +def make_wall (leveldata, row, col): + # get the actual data in that place + leveldata[row][col][2] = not leveldata[row][col][2] + +# the actual level editor +def level_editor (fname): + # load level data + leveldata = cPickle.load (file (fname)) + + pygame.init () + # load the tileset + tileset = pygame.image.load (os.path.join ("background", "tileset.png")) + screen = pygame.display.set_mode ((720, 512)) + pygame.display.set_caption ("Level editor") + currow, curcol = 0, 0 + + while 1: + screen.fill (pygame.Color (0, 0, 0)) + display_menu (screen) + draw_level (screen, leveldata, tileset) + draw_cursor (screen, currow, curcol) + pygame.display.update () + for event in pygame.event.get (): + if event.type == pygame.KEYDOWN: + if event.key == ord ("q"): + pygame.quit () + return + elif event.key == pygame.K_DOWN: + currow += 1 + if currow > 9: + currow = 0 + elif event.key == pygame.K_UP: + currow -= 1 + if currow < 0: + currow = 9 + elif event.key == pygame.K_LEFT: + curcol -= 1 + if curcol < 0: + curcol = 9 + elif event.key == pygame.K_RIGHT: + curcol += 1 + if curcol > 9: + curcol = 0 + elif event.key == ord ("w"): + make_wall (leveldata, currow, curcol) + +def new_level (): + print + fname = raw_input ("New level file path: ") + # prepare level with blank tiles + leveldata = [ +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], +[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] +] + # save the level first + cPickle.dump (leveldata, file (fname, "w")) + + # now edit it + level_editor (fname) + +def load_level (): + pass + +def main (): + while 1: + print + print ("Level Editor for the Adventures of Butaba") + print + print ("1. Start a new level") + print ("2. Load existing level") + print ("3. Quit") + + ch = raw_input ("Your choice: ") + if ch == "1": + new_level () + elif ch == "2": + load_level () + elif ch == "3": + sys.exit (0) + else: + print ("Wrong choice. Must be 1, 2, or 3") + + +if __name__=="__main__": + main () \ No newline at end of file diff --git a/maingame.py b/maingame.py index 7b54c2d..bde655d 100644 --- a/maingame.py +++ b/maingame.py @@ -13,7 +13,7 @@ class MainGame: # initialize the game def __init__ (self): pygame.init () - self.screen = pygame.display.set_mode ((720, 512), pygame.FULLSCREEN) + self.screen = pygame.display.set_mode ((720, 512)) pygame.display.set_caption ("The Adventures of Butaba") # initalize background graphics @@ -70,7 +70,8 @@ class MainGame: objects = [ gameobjects.Key (4, 3, "a chest key", self.img_key2, level.KEY_CHEST1), gameobjects.Key (4, 3, "a room key", self.img_key, level.KEY_ROOM1), gameobjects.HealthPotion (4, 2, self.img_redpotion), - gameobjects.Chest (2, 5, "chest", self.img_chest, level.KEY_CHEST1, True) + gameobjects.Chest (2, 5, "chest", self.img_chest, level.KEY_CHEST1, True), + gameobjects.GoldCoins (5, 2, self.img_goldcoins, 50) ] ) self.level1.levelright = self.level1e @@ -272,6 +273,15 @@ class MainGame: # display the contents of the chest else: pass + # if the object is gold coins + elif isinstance (obj, gameobjects.GoldCoins) is True: + obj.use (self.butaba) + self.status_message = "You picked up %d gold." % obj.value + # remove the gold coins after adding it to Butaba's gold + if obj in self.currentlevel.objects: + self.currentlevel.objects.remove (obj) + elif obj in self.butaba.inventory: + self.butaba.inventory.remove (obj) def move_butaba_left (self): # clear any status messages diff --git a/objects/gold-coins.png b/objects/gold-coins.png index c5228aca0987179d6bc57dc93d7c8cccb97ac878..a6848a92da8cd6c779a7539c7199f60c645bb9a7 100644 GIT binary patch delta 357 zcmV-r0h<211Iz=EF%Je}OGiWiF909_0D_-IN|7!f2?_xQ5)3o$dBTxaD1RqOL_t(o z!|m8X3W7is2H@Xl=^0$Q6FP$X7M@0Mr4R@~r)kl?BLrTheJw_w;~UeNun79!ZZev$ zdB4vn!UI258-O5!2qK6ef(RmrpkJ!(>YrFwqvhfFd63#tRk;U%cz5mBRUTxD{C`u6 z!``xKb6GZRUSt4}CL>eR27j1*L3M?4kJAORr4oqGykaU%aJpdq6RNAhVVi(qDOcV4 zNXx`@?&jY1rIty*+|1e;QY}+-Pt!8UKAR%zzr88dzIb=N-*V}iOZPD?fK--^NoCf~ zkWs4M+2^-tnggjUhn7qCY25M&6&cRmbaaO4=L{stTxg5}GUN^$_dae?vugbeDbuU* zqXIs8b+xfb~NE#xFe6N>pST z?=@YX5a5{Pl=LW5=Z$4kXZXhJNhdd)$^q##y^~b$j=j0|Jn73cpBJiR17 zFYPVQ%4wQa9hw{|^L=OX-oG~`>%QK5`Rz`-o?Y6jX77=Db4CbIjpWi-b8G89tMPlJ zT5gNkwY|_$JtS?%9eelmId$h8>?fUZJ*uJS{BA?xdo|P5)6P#Y^6ixu{m>sSzWekt mxtX8+x4chFK2+aq^Ob2v(8Rs1Ji$vCfWXt$&t;ucLK6TiH=Viw diff --git a/objects/red-potion.png b/objects/red-potion.png index 243f25f1be75521281f442c424dcadc9c485b7c5..0d58259c98d8f38c463fc8adc02b7d3664e3418d 100644 GIT binary patch delta 526 zcmV+p0`dLc1oQ-uF%Je}OGiWiF909_0D_-IN|7!f2?_xQ5;F3SWNDFBD1XmML_t(o z!|m8HZWBQi#_``cX%eKxNZ67s#gz2905v5?NW&TEu&+VI4JfI(2n7j_lr+{hsnAfE zH4~hGH?#IG;MHoi(n@ds{^q^!y>SqA>gurv+5_!@{x6`z24aNdJ?00CxdC2)H{fM9 zQ)s}cyQvVK^DdjI*A&fi%YR<~BG^uW5*mEq75JFPl(A*EfzsVC4(0xi|#LV@f zGa?~*zHiqUpWW6g-$NR3>Phl`-i2Nk88d3e7n=ZDB~a(Oee4KO{F4c^uw_AGr>R~( zjl_i72Z{++=!imbgI==Jd1%H7_`Qv_4jC|J!ej{)wpL{^G3CG$9&u289hD_c%d*d~l5lrrA2|Zw zs{67ia9I>#t*dNtglAplL!XXjVt|{x89H3RI!N7v6!dX-o-6b{D9+&JzwUEr%XbA|Ym~51kPSmH*HCHO5zW z^-A}U2Aq0YxxVN^FN;hVH{**<0Id_KbKO361StN`1om)bL1Ui?2I({^CbWN`SiuS% zQ7CTFOLjUB%{c+zneBNVYOdP5`__>(4}W3Clre!HT=FqZx5oknb8y0h99*;Dl9-T8 z^??HZB=VRML$KtA1(AytQ+=$OK;ic`Qzjs1$T5*ymRy5HIf~SkYEX;jtfU!}^OENr zbHE`-oEF81oC#B=E1=L?mBorF2VU@ugYxdUC~#U7Lq?^9yDR(15%^jDuCfeQSz{J9 zx=M>9>^dKY+-}_cmSvlLw?&}!H6xTJ w^l$XXIph{V_lZ7#AZM$=+BC+HP>MCzFIgXO4=M#ZDgXcg07*qoM6N<$f