Level editor completed somewhat
authorHarishankar <v.harishankar@gmail.com>
Sat, 1 Oct 2011 09:52:59 +0000 (15:22 +0530)
committerHarishankar <v.harishankar@gmail.com>
Sat, 1 Oct 2011 09:52:59 +0000 (15:22 +0530)
Level editor is finished, but lacking the ability to
scroll in the tileset. So if the tileset is greater
than the screen size, the code does not work.

leveleditor.py [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 784199b..5b98c09
@@ -1,8 +1,8 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python
 
 # 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
+# design. inputting levels by hand array is very tedious
 
 import pygame
 import os.path
@@ -42,6 +42,47 @@ def make_wall (leveldata, row, col):
        # get the actual data in that place
        leveldata[row][col][2] = not leveldata[row][col][2]
 
+# place a tile at current spot
+def put_tile (leveldata, row, col, tilerow, tilecol):
+       leveldata[row][col][0] = tilerow
+       leveldata[row][col][1] = tilecol
+
+# picking a tile from the tileset
+def pick_tile (screen, tileset):
+       selrow, selcol = 0, 0
+
+       totalrows = tileset.get_height () / 48
+       totalcols = tileset.get_width () / 48
+
+       while 1:
+               screen.fill (pygame.Color (0, 0, 0))
+               screen.blit (tileset, (0, 0))
+               draw_cursor (screen, selrow, selcol)
+               pygame.display.update ()
+               for event in pygame.event.get ():
+                       if event.type == pygame.KEYDOWN:
+                               if event.key == pygame.K_ESCAPE:
+                                       return None
+                               elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
+                                       return selrow, selcol
+                               elif event.key == pygame.K_UP:
+                                       selrow -= 1
+                                       if selrow < 0:
+                                               selrow = totalrows - 1
+                               elif event.key == pygame.K_DOWN:
+                                       selrow += 1
+                                       if selrow >= totalrows:
+                                               selrow = 0
+                               elif event.key == pygame.K_LEFT:
+                                       selcol -= 1
+                                       if selcol < 0:
+                                               selcol = totalcols - 1
+                               elif event.key == pygame.K_RIGHT:
+                                       selcol += 1
+                                       if selcol >= totalcols:
+                                               selcol = 0
+
+
 # the actual level editor
 def level_editor (fname):
        # load level data
@@ -53,6 +94,7 @@ def level_editor (fname):
        screen = pygame.display.set_mode ((720, 512))
        pygame.display.set_caption ("Level editor")
        currow, curcol = 0, 0
+       tilerow, tilecol = 0, 0
 
        while 1:
                screen.fill (pygame.Color (0, 0, 0))
@@ -65,6 +107,9 @@ def level_editor (fname):
                                if event.key == ord ("q"):
                                        pygame.quit ()
                                        return
+                               elif event.key == ord ("s"):
+                                       cPickle.dump (leveldata, file (fname, "w"))
+                                       print ("Level saved")
                                elif event.key == pygame.K_DOWN:
                                        currow += 1
                                        if currow > 9:
@@ -81,6 +126,13 @@ def level_editor (fname):
                                        curcol += 1
                                        if curcol > 9:
                                                curcol = 0
+                               elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
+                                       put_tile (leveldata, currow, curcol, tilerow, tilecol)
+                               elif event.key == ord ("p"):
+                                       tile = pick_tile (screen, tileset)
+                                       if tile is not None:
+                                               tilerow = tile[0]
+                                               tilecol = tile[1]
                                elif event.key == ord ("w"):
                                        make_wall (leveldata, currow, curcol)
 
@@ -107,7 +159,8 @@ def new_level ():
        level_editor (fname)
 
 def load_level ():
-       pass
+       fname = raw_input ("Level file to load: ")
+       level_editor (fname)
 
 def main ():
        while 1:
@@ -130,4 +183,4 @@ def main ():
 
 
 if __name__=="__main__":
-       main ()
\ No newline at end of file
+       main ()