5b98c09e43f0397bbcb2257b6429c24aae0327e5
[butaba-adventures.git] / leveleditor.py
1 #!/usr/bin/env python
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 tedious
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 # place a tile at current spot
46 def put_tile (leveldata, row, col, tilerow, tilecol):
47 leveldata[row][col][0] = tilerow
48 leveldata[row][col][1] = tilecol
49
50 # picking a tile from the tileset
51 def pick_tile (screen, tileset):
52 selrow, selcol = 0, 0
53
54 totalrows = tileset.get_height () / 48
55 totalcols = tileset.get_width () / 48
56
57 while 1:
58 screen.fill (pygame.Color (0, 0, 0))
59 screen.blit (tileset, (0, 0))
60 draw_cursor (screen, selrow, selcol)
61 pygame.display.update ()
62 for event in pygame.event.get ():
63 if event.type == pygame.KEYDOWN:
64 if event.key == pygame.K_ESCAPE:
65 return None
66 elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
67 return selrow, selcol
68 elif event.key == pygame.K_UP:
69 selrow -= 1
70 if selrow < 0:
71 selrow = totalrows - 1
72 elif event.key == pygame.K_DOWN:
73 selrow += 1
74 if selrow >= totalrows:
75 selrow = 0
76 elif event.key == pygame.K_LEFT:
77 selcol -= 1
78 if selcol < 0:
79 selcol = totalcols - 1
80 elif event.key == pygame.K_RIGHT:
81 selcol += 1
82 if selcol >= totalcols:
83 selcol = 0
84
85
86 # the actual level editor
87 def level_editor (fname):
88 # load level data
89 leveldata = cPickle.load (file (fname))
90
91 pygame.init ()
92 # load the tileset
93 tileset = pygame.image.load (os.path.join ("background", "tileset.png"))
94 screen = pygame.display.set_mode ((720, 512))
95 pygame.display.set_caption ("Level editor")
96 currow, curcol = 0, 0
97 tilerow, tilecol = 0, 0
98
99 while 1:
100 screen.fill (pygame.Color (0, 0, 0))
101 display_menu (screen)
102 draw_level (screen, leveldata, tileset)
103 draw_cursor (screen, currow, curcol)
104 pygame.display.update ()
105 for event in pygame.event.get ():
106 if event.type == pygame.KEYDOWN:
107 if event.key == ord ("q"):
108 pygame.quit ()
109 return
110 elif event.key == ord ("s"):
111 cPickle.dump (leveldata, file (fname, "w"))
112 print ("Level saved")
113 elif event.key == pygame.K_DOWN:
114 currow += 1
115 if currow > 9:
116 currow = 0
117 elif event.key == pygame.K_UP:
118 currow -= 1
119 if currow < 0:
120 currow = 9
121 elif event.key == pygame.K_LEFT:
122 curcol -= 1
123 if curcol < 0:
124 curcol = 9
125 elif event.key == pygame.K_RIGHT:
126 curcol += 1
127 if curcol > 9:
128 curcol = 0
129 elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
130 put_tile (leveldata, currow, curcol, tilerow, tilecol)
131 elif event.key == ord ("p"):
132 tile = pick_tile (screen, tileset)
133 if tile is not None:
134 tilerow = tile[0]
135 tilecol = tile[1]
136 elif event.key == ord ("w"):
137 make_wall (leveldata, currow, curcol)
138
139 def new_level ():
140 print
141 fname = raw_input ("New level file path: ")
142 # prepare level with blank tiles
143 leveldata = [
144 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
145 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
146 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
147 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
148 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
149 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
150 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
151 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
152 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
153 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
154 ]
155 # save the level first
156 cPickle.dump (leveldata, file (fname, "w"))
157
158 # now edit it
159 level_editor (fname)
160
161 def load_level ():
162 fname = raw_input ("Level file to load: ")
163 level_editor (fname)
164
165 def main ():
166 while 1:
167 print
168 print ("Level Editor for the Adventures of Butaba")
169 print
170 print ("1. Start a new level")
171 print ("2. Load existing level")
172 print ("3. Quit")
173
174 ch = raw_input ("Your choice: ")
175 if ch == "1":
176 new_level ()
177 elif ch == "2":
178 load_level ()
179 elif ch == "3":
180 sys.exit (0)
181 else:
182 print ("Wrong choice. Must be 1, 2, or 3")
183
184
185 if __name__=="__main__":
186 main ()