-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyunity.py
353 lines (271 loc) · 9.41 KB
/
pyunity.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import pygame
import random
from PPlay.sprite import Sprite
from PPlay.window import *
from PPlay.mouse import *
from keycode import _KeyCode
pygame.init()
class _Game(object):
def __init__(self):
self.defaults = Defaults()
self.window = Window(self.defaults.resolution.x, self.defaults.resolution.y)
self.input = _Input()
self.activeScene = None
return
def init(self, scene):
self.loadScene(scene)
self.loop()
return self
def loadScene(self, scene):
if scene is not None:
self.activeScene = scene()
if self.defaults.fullscreen:
self.window.set_fullscreen()
self.activeScene.onStart()
return
def update(self):
if self.activeScene is not None:
if len(self.activeScene.toDestroy) > 0:
self.activeScene.objects = filter(lambda o: o.destroyed is not True, self.activeScene.objects)
self.activeScene.onUpdate()
for obj in self.activeScene.objects:
obj.baseUpdate()
self.input.update()
return
def draw(self):
if self.activeScene is not None:
bgColor = self.activeScene.backgroundColor
self.window.set_background_color((bgColor[0], bgColor[1], bgColor[2]))
self.activeScene.onDraw()
camera = self.activeScene.camera
for obj in self.activeScene.objects:
if not isinstance(obj, GameUI):
#check if object is inside the screen
if obj.position.x + obj.size.x > camera.position.x and obj.position.x < camera.position.x + camera.getSize().x and obj.position.y + obj.size.y > camera.position.y and obj.position.y < camera.position.y + camera.getSize().y:
realPos = obj.position
#Calculate camera-based position
obj.position = obj.position.sumWith(self.activeScene.camera.position.byScalar(-1))
obj.baseDraw()
#Restore real position
obj.position = realPos
else:
obj.baseDraw()
return
def setFullscreen(self, boolean):
if boolean:
self.window.set_fullscreen()
else:
self.window.restoreScreen()
return
def getFullscreen(self):
return self.window.fullscreen_enabled
def loop(self):
while True:
self.update()
self.draw()
self.window.update()
def instantiate(self, obj, position):
if self.activeScene is not None:
o = obj()
o.position = position.clone()
self.activeScene.objects.append(o)
return o
else:
return None
def setTitle(self, text):
self.window.set_title(text)
return
def setDefaults(self, defaults):
self.defaults = defaults
self.window.set_resolution(self.defaults.resolution.x, self.defaults.resolution.y)
self.setFullscreen(self.defaults.fullscreen)
return
class GameObject(object):
def __init__(self):
self.position = Vector2.zero()
self.size = Vector2.zero()
self.velocity = Vector2.zero()
self.sprite = None
self.destroyed = False
return
def base(self):
GameObject.__init__(self)
return
def __str__(self):
return str(self.__class__.__name__)
def setSprite(self, name):
if name is None:
self.sprite = None
self.size = Vector2.zero()
else:
self.sprite = Sprite(name + ".png")
self.size = Vector2(self.sprite.width, self.sprite.height)
def onUpdate(self):
return
def onDraw(self):
return
def baseUpdate(self):
self.onUpdate()
self.position = self.position.sumWith(self.velocity)
return
def baseDraw(self):
if self.sprite is not None:
self.sprite.set_position(self.position.x, self.position.y)
self.sprite.draw()
self.onDraw()
return
def destroy(self):
self.destroyed = True
Game.activeScene.toDestroy.append(self)
return
class GameUI(GameObject):
def __init__(self):
self.isDragging = False
self.draggable = False
return
def base(self):
GameObject.base(self)
GameUI.__init__(self)
return
def baseUpdate(self):
if self.draggable:
if Input.GetMouseButtonDown(0) and self.position.x <= Input.mousePosition.x < self.position.x + self.size.x and self.position.y <= \
Input.mousePosition.y < self.position.y + self.size.y:
self.isDragging = True
elif self.isDragging and Input.GetMouseButtonUp(0):
self.isDragging = False
if self.isDragging:
self.velocity = Vector2.zero()
self.position = Input.mousePosition.sumWith(self.size.byScalar(-0.5))
# self.position.x = Input.mousePosition.x - self.size.x / 2
# self.position.y = Input.mousePosition.y - self.size.y / 2
GameObject.baseUpdate(self)
class TextUI(GameUI):
def __init__(self):
self.text = ""
self.color = (0, 0, 0)
self.font = pygame.font.SysFont(pygame.font.get_default_font(), 30)
return
def base(self):
GameUI.base(self)
TextUI.__init__(self)
return
def baseDraw(self):
if self.font is not None:
surface = self.font.render(self.text, True, self.color)
rect = pygame.Rect(self.position.x, self.position.y, self.size.x, self.size.y)
Game.window.get_screen().blit(surface, rect)
return
def setText(self, text):
self.text = text
size = self.font.size(self.text)
self.size = Vector2(size[0], size[1])
return
def setColor(self, color):
self.color = color
return
class Camera(object):
def __init__(self, position):
self.position = position.clone()
return
def getSize(self):
return Vector2(Game.window.width, Game.window.height)
class Scene(object):
def __init__(self):
self.backgroundColor = (0, 0, 0)
self.objects = []
self.toDestroy = []
self.camera = Camera(Vector2.zero())
return
def base(self):
Scene.__init__(self)
return
def onStart(self):
return
def onUpdate(self):
return
def onDraw(self):
return
def baseStart(self):
self.onStart()
return
def baseUpdate(self):
self.onUpdate()
return
def baseDraw(self):
self.onDraw()
return
class _Input(object):
def __init__(self):
self.keyboard = Window.get_keyboard()
self.mouse = Mouse()
self.lastKeys = pygame.key.get_pressed()
self.lastMouseButtons = [pygame.mouse.get_pressed()] * 2
mPos = self.mouse.get_position()
self.mousePosition = Vector2(mPos[0], mPos[1])
return
def update(self):
self.lastKeys = pygame.key.get_pressed()
mPos = self.mouse.get_position()
self.mousePosition = Vector2(mPos[0], mPos[1])
self.lastMouseButtons[1] = self.lastMouseButtons[0]
self.lastMouseButtons[0] = pygame.mouse.get_pressed()
return
def GetKey(self, key):
if isinstance(key, basestring):
key = self.keyboard.to_pattern(key)
return pygame.key.get_pressed()[key] == 1
def GetKeyDown(self, key):
if isinstance(key, basestring):
key = self.keyboard.to_pattern(key)
return self.lastKeys[key] != 1 and pygame.key.get_pressed()[key] == 1
def GetKeyUp(self, key):
if isinstance(key, basestring):
key = self.keyboard.to_pattern(key)
return self.lastKeys[key] == 1 and pygame.key.get_pressed()[key] != 1
def GetMouseButton(self, button):
return pygame.mouse.get_pressed()[button] == 1
def GetMouseButtonDown(self, button):
return self.lastMouseButtons[1][button] != 1 and pygame.mouse.get_pressed()[button] == 1
def GetMouseButtonUp(self, button):
return self.lastMouseButtons[1][button] == 1 and pygame.mouse.get_pressed()[button] != 1
class _Random(object):
def Range(self, min, max):
if type(min) == float or type(max) == float:
return random.uniform(float(min), float(max))
else:
return random.randint(min, max)
class Vector2():
def __init__(self, x, y):
self.x = x
self.y = y
return
def sumWith(self, vector2):
clone = self.clone()
if vector2 is not None:
clone.x += vector2.x
clone.y += vector2.y
return clone
def byScalar(self, factor):
clone = self.clone()
if factor is not None:
clone.x *= factor
clone.y *= factor
return clone
def clone(self):
return Vector2(self.x, self.y)
def __str__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
@staticmethod
def zero():
return Vector2(0, 0)
class Defaults():
def __init__(self):
self.resolution = Vector2(800, 600)
self.fullscreen = False
return
KeyCode = _KeyCode()
Game = _Game()
Input = Game.input
Random = _Random()
Instantiate = Game.instantiate