-
Notifications
You must be signed in to change notification settings - Fork 2
/
leveleditor.py
291 lines (271 loc) · 11 KB
/
leveleditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import pygame
import pickle
import sys, os
from pygame.locals import *
import easygui as eg
import levelformat
class MyButton:
"""Button class based on the
template method pattern."""
def __init__(self, x, y, w, h):
self.rect = Rect(x, y, w, h)
self.msg = ""
def containsPoint(self, x, y):
return self.rect.collidepoint(x, y)
def draw(self, surface):
# You could of course use pictures here.
# This method could also be implemented
# by subclasses.
pygame.draw.rect(
surface,
(150,150,150), #gray
self.rect,
)
# Here's some stuff
font = pygame.font.Font(None, 24)
text = font.render(self.msg, 1, (10,10,10))
textpos = text.get_rect(center=self.rect.center)
surface.blit(text, textpos)
def do(self):
print("Implement in subclasses")
class RectButton(MyButton):
def __init__(self, x, y, w, h, app):
# This is how you call the superclass init
MyButton.__init__(self, x, y, w, h)
self.app = app
self.msg = "Platform"
def do(self):
SimpleUI.state['mode'] = 'rect'
class PlayerButton(MyButton):
def __init__(self, x, y, w, h, app):
MyButton.__init__(self, x, y, w, h)
self.app = app
self.msg = "Player"
def do(self):
SimpleUI.state['mode'] = 'player'
class ExitButton(MyButton):
def __init__(self, x, y, w, h, app):
MyButton.__init__(self, x, y, w, h)
self.app = app
self.msg = "Exit"
def do(self):
SimpleUI.state['mode'] = 'exit'
class ButtonButton(MyButton):
def __init__(self, x, y, w, h, app):
MyButton.__init__(self, x, y, w, h)
self.app = app
self.msg = "Button"
def do(self):
SimpleUI.state['mode'] = 'button'
class RecorderButton(MyButton):
def __init__(self, x, y, w, h, app):
MyButton.__init__(self, x, y, w, h)
self.app = app
self.msg = "Recorder"
def do(self):
SimpleUI.state['mode'] = 'recorder'
class SaveButton(MyButton):
def __init__(self, x, y, w, h, app):
MyButton.__init__(self, x, y, w, h)
self.filename = 'default'
self.msg = 'Save'
self.app = app
def do(self):
level = levelformat.LevelFormat(self.app.gamePlayer, self.app.gameRecorders, self.app.gameButtons, self.app.gameRects, self.app.gameExit)
self.filename = eg.enterbox(msg='Enter a filename for saving.', title='Saving Stuff', default=self.filename)
if self.filename is not None:
pickle.dump(level, open(os.path.join('assets', 'levels', self.filename + '.p'), 'wb'))
class LoadButton(MyButton):
def __init__(self, x, y, w, h, app):
MyButton.__init__(self, x, y, w, h)
self.app = app
self.msg = "Load"
self.filename = None
def do(self):
self.filename = eg.enterbox(msg='Enter a file to load.', title='Loading Stuff')
if self.filename is not None:
level = pickle.load(open(os.path.join('assets', 'levels', self.filename + '.p'), 'rb'))
self.app.gamePlayer, self.app.gameRecorders, self.app.gameButtons, self.app.gameRects, self.app.gameExit = level.player, level.recorders, level.buttons, level.platforms, level.exit
class SimpleUI:
state = {'mode' : 'none', 'clicked' : False}
LAST = [0,0]
CURRENT = [0,0]
WHITE = (255,255,255)
BLUE = (0,0,255)
GREEN = (0,255,0)
RED = (255,0,0)
def __init__(self, width=1000, height=500):
# Initialize PyGame
pygame.init()
pygame.display.set_caption("Simple UI")
self.selected = None
self.screen = pygame.display.set_mode((width,height))
self.screen.fill(SimpleUI.WHITE)
self.buttons = []
self.addButton(RectButton(20, 20, 100, 50, self))
self.addButton(PlayerButton(20, 90, 100, 50, self))
self.addButton(ExitButton(20, 160, 100, 50, self))
self.addButton(ButtonButton(20, 230, 100, 50, self))
self.addButton(RecorderButton(20, 300, 100, 50, self))
self.addButton(LoadButton(20, 370, 100, 50, self))
self.addButton(SaveButton(20, 440, 100, 50, self))
self.gameButtons = []
self.gamePlayer = levelformat.Player(200, 200, 30, 30)
self.gameRects = []
self.gameRecorders = []
self.gameExit = levelformat.Exit(400, 200)
def addButton(self, button):
self.buttons = self.buttons + [button]
def run(self):
# Run the event loop
self.loop()
# Close the Pygame window
pygame.quit()
def loop(self):
clock = pygame.time.Clock()
exit = False
while not exit:
exit = self.handleEvents()
self.draw()
clock.tick(30)
def handleEvents(self):
exit = False
for event in pygame.event.get():
if event.type == QUIT:
exit = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit = True
if event.key == K_SPACE:
SimpleUI.state['mode'] = 'move'
if event.key == K_LSHIFT:
SimpleUI.state['mode'] = 'prop'
elif event.type == KEYUP:
if event.key == K_SPACE:
SimpleUI.state['mode'] = 'none'
if event.key == K_LSHIFT:
SimpleUI.state['mode'] = 'none'
#SimpleUI.state['mode'] = SimpleUI.state['old']
elif event.type == MOUSEBUTTONDOWN:
self.handleMouseDown(pygame.mouse.get_pos())
elif event.type == MOUSEBUTTONUP:
self.handleMouseUp(pygame.mouse.get_pos())
elif event.type == MOUSEMOTION:
self.handleMouseMotion(pygame.mouse.get_pos())
return exit
def handleMouseDown(self, coords):
x,y = coords
but = False
SimpleUI.state['clicked'] = True
for button in self.buttons:
if (button.containsPoint(x, y)):
button.do()
SimpleUI.state['clicked'] = False
but = True
break
for platform in self.gameRects:
if platform.rect.collidepoint(x, y):
self.selected = platform.rect
if SimpleUI.state['mode'] is not 'prop': SimpleUI.state['mode'] = 'drag'
else:
platform.setBy = eg.enterbox('ButtonGroup to that sets this platform')
print("set by"+platform.setBy+"yo")
if eg.boolbox("Platform visible by default?", "One more thing", ["Yes", "No"]):
platform.visibleDefault = True
else:
platform.visibleDefault = False
but = True
break
for gbutton in self.gameButtons:
if gbutton.rect.collidepoint(x,y):
self.selected = gbutton.rect
if SimpleUI.state['mode'] is not 'prop': SimpleUI.state['mode'] = 'drag'
else:
gbutton.sets = eg.enterbox('Object Group to set')
but = True
for recorder in self.gameRecorders:
if recorder.rect.collidepoint(x,y):
self.selected = recorder.rect
SimpleUI.state['mode'] = 'drag'
but = True
if self.gamePlayer.rect.collidepoint(x,y):
self.selected = self.gamePlayer.rect
SimpleUI.state['mode'] = 'drag'
but = True
elif self.gameExit.rect.collidepoint(x,y):
self.selected = self.gameExit.rect
SimpleUI.state['mode'] = 'drag'
but = True
if not but:
if SimpleUI.state['mode'] == 'rect':
SimpleUI.LAST = [x,y]
if SimpleUI.state['mode'] == 'player':
self.gamePlayer.rect.center = (x,y)
if SimpleUI.state['mode'] == 'recorder':
temp = levelformat.Recorder(x,y)
temp.rect.center = (x,y)
self.gameRecorders.append(temp)
if SimpleUI.state['mode'] == 'button':
temp = levelformat.Button(x,y)
temp.rect.center = (x,y)
self.gameButtons.append(temp)
if SimpleUI.state['mode'] == 'exit':
self.gameExit.rect.center = (x,y)
if SimpleUI.state['mode'] == 'move':
SimpleUI.LAST = [x,y]
def handleMouseUp(self, coords):
x,y = coords
if SimpleUI.state['mode'] == 'rect' and SimpleUI.state['clicked'] is True:
width, height = [a-b for a,b in zip([x,y], SimpleUI.LAST)]
temp = levelformat.Platform(SimpleUI.LAST[0], SimpleUI.LAST[1], width, height)
if temp.rect.width > 30 and temp.rect.height > 30:
self.gameRects.append(temp)
SimpleUI.state['clicked'] = False
self.selected = None
def handleMouseMotion(self, coords):
x,y = coords
if SimpleUI.state['clicked'] is True:
xo,yo = [a-b for a,b in zip([x,y], SimpleUI.CURRENT)]
if SimpleUI.state['mode'] is 'move':
self.moveEverything((xo,yo))
elif SimpleUI.state['mode'] is 'drag' and self.selected is not None:
self.moveSomething((xo,yo))
SimpleUI.CURRENT = [x,y]
def moveSomething(self, coords):
x,y = coords
self.selected.move_ip(x,y)
def moveEverything(self, coords):
x,y = coords
for gbutton in self.gameButtons:
gbutton.rect.move_ip(x,y)
for recorder in self.gameRecorders:
recorder.rect.move_ip(x,y)
for lev in self.gameRects:
lev.rect.move_ip(x,y)
self.gamePlayer.rect.move_ip(x,y)
self.gameExit.rect.move_ip(x,y)
def draw(self):
self.screen.fill(SimpleUI.WHITE)
if SimpleUI.state['mode'] == 'rect' and SimpleUI.state['clicked'] is True:
width = SimpleUI.CURRENT[0] - SimpleUI.LAST[0]
height = SimpleUI.CURRENT[1] - SimpleUI.LAST[1]
temp = pygame.Rect(SimpleUI.LAST, (width, height))
temp.normalize()
if temp.width > 30 and temp.height > 30:
pygame.draw.rect(self.screen, SimpleUI.GREEN, temp)
else:
pygame.draw.rect(self.screen, SimpleUI.RED, temp)
for gbutton in self.gameButtons:
gbutton.draw(self.screen)
for recorder in self.gameRecorders:
recorder.draw(self.screen)
self.gamePlayer.draw(self.screen)
self.gameExit.draw(self.screen)
for lev in self.gameRects:
lev.draw(self.screen)
for button in self.buttons:
button.draw(self.screen)
pygame.display.update()
# Start the program
if __name__ == '__main__':
SimpleUI().run()