Started on the level editor
[butaba-adventures.git] / leveleditor.py
diff --git a/leveleditor.py b/leveleditor.py
new file mode 100644 (file)
index 0000000..784199b
--- /dev/null
@@ -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), "<space> 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