From: Harishankar Date: Sat, 1 Oct 2011 08:21:57 +0000 (+0530) Subject: Started on the level editor X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=commitdiff_plain;h=5075eb87b2a41e7f93c81a3ad2cfc1ac94a11314 Started on the level editor Began work on the level editor for the game --- 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 c5228ac..a6848a9 100644 Binary files a/objects/gold-coins.png and b/objects/gold-coins.png differ diff --git a/objects/red-potion.png b/objects/red-potion.png index 243f25f..0d58259 100644 Binary files a/objects/red-potion.png and b/objects/red-potion.png differ