Started on the level editor
[butaba-adventures.git] / leveleditor.py
1 #!/usr/bin/env python3
2
3 # level designer gui - this is used to design level graphics
4 # very rough and ready - main purpose is to allow quick game
5 # design. inputting levels by hand array is very tough
6
7 import pygame
8 import os.path
9 import sys
10 import cPickle
11
12 import utility
13
14 # draw the level actually
15 def draw_level (screen, leveldata, tileset):
16 i = 0
17 for row in leveldata:
18 j = 0
19 for tilerow, tilecol, is_solid in row:
20 tilex = tilecol * 48
21 tiley = tilerow * 48
22 screen.blit (tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48))
23 if is_solid == 1:
24 utility.put_text (screen, j*48+2, i*48+2, 10, (255,255,0), "w")
25 j += 1
26 i += 1
27
28 def display_menu (screen):
29 utility.put_text (screen, 490, 10, 10, (255,255,0), "p to pick tile")
30 utility.put_text (screen, 490, 50, 10, (255,255,0), "w to wall (solid)")
31 utility.put_text (screen, 490, 90, 10, (255,255,0), "<space> to place")
32 utility.put_text (screen, 490, 130, 10,(255,255,0), "Arrows to move around")
33 utility.put_text (screen, 490, 170, 10, (255,255,0), "s to Save level")
34 utility.put_text (screen, 490, 210, 10, (255,255,0), "q to Quit editor")
35
36 # draw the tile cursor
37 def draw_cursor (screen, currow, curcol):
38 pygame.draw.rect (screen, (255,255,255), (curcol*48, currow*48, 48, 48), 1)
39
40 # make wall
41 def make_wall (leveldata, row, col):
42 # get the actual data in that place
43 leveldata[row][col][2] = not leveldata[row][col][2]
44
45 # the actual level editor
46 def level_editor (fname):
47 # load level data
48 leveldata = cPickle.load (file (fname))
49
50 pygame.init ()
51 # load the tileset
52 tileset = pygame.image.load (os.path.join ("background", "tileset.png"))
53 screen = pygame.display.set_mode ((720, 512))
54 pygame.display.set_caption ("Level editor")
55 currow, curcol = 0, 0
56
57 while 1:
58 screen.fill (pygame.Color (0, 0, 0))
59 display_menu (screen)
60 draw_level (screen, leveldata, tileset)
61 draw_cursor (screen, currow, curcol)
62 pygame.display.update ()
63 for event in pygame.event.get ():
64 if event.type == pygame.KEYDOWN:
65 if event.key == ord ("q"):
66 pygame.quit ()
67 return
68 elif event.key == pygame.K_DOWN:
69 currow += 1
70 if currow > 9:
71 currow = 0
72 elif event.key == pygame.K_UP:
73 currow -= 1
74 if currow < 0:
75 currow = 9
76 elif event.key == pygame.K_LEFT:
77 curcol -= 1
78 if curcol < 0:
79 curcol = 9
80 elif event.key == pygame.K_RIGHT:
81 curcol += 1
82 if curcol > 9:
83 curcol = 0
84 elif event.key == ord ("w"):
85 make_wall (leveldata, currow, curcol)
86
87 def new_level ():
88 print
89 fname = raw_input ("New level file path: ")
90 # prepare level with blank tiles
91 leveldata = [
92 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
93 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
94 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
95 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
96 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
97 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
98 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
99 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
100 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
101 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
102 ]
103 # save the level first
104 cPickle.dump (leveldata, file (fname, "w"))
105
106 # now edit it
107 level_editor (fname)
108
109 def load_level ():
110 pass
111
112 def main ():
113 while 1:
114 print
115 print ("Level Editor for the Adventures of Butaba")
116 print
117 print ("1. Start a new level")
118 print ("2. Load existing level")
119 print ("3. Quit")
120
121 ch = raw_input ("Your choice: ")
122 if ch == "1":
123 new_level ()
124 elif ch == "2":
125 load_level ()
126 elif ch == "3":
127 sys.exit (0)
128 else:
129 print ("Wrong choice. Must be 1, 2, or 3")
130
131
132 if __name__=="__main__":
133 main ()