-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManipulatableDirtySprite.py
431 lines (386 loc) · 14.4 KB
/
ManipulatableDirtySprite.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import pygame
from pygame.locals import *
import math
class ManipulatableDirtySprite(pygame.sprite.DirtySprite):
"""This class is to highly overload the DirtySprite class to have lots of
neat little features, such as scaling and rotation."""
_blank = None
_image = None
_opaqueScreen = None
_opaqueImage = None
_scaledImage = None
_rotatedImage = None
_rect = None
_x = 0
_y = 0
_xscale = 1.0
_yscale = 1.0
_xcenter = 0.0 #number of half-widths, 0.0 is center of sprite
_ycenter = 0.0 #number of half-heights, 0.0 is center of sprite
_rotation = 0.0
_sine = 0.0
_cosine = 1.0
_opacity = 1.0
#_ichanged indicates if the image has been changed.
_ichanged = True
#_ochanged indicates if the image opacity has been changed.
_ochanged = False
#_schanged indicates if the scale has changed.
_schanged = False
#_rchanged indicates if the image has been rotated.
_rchanged = False
#_pchanged indicates if the rect has changed.
_pchanged = False
def __init__(self, image = None, *args, **kwargs):
if self._blank == None:
self._blank = pygame.Surface((1, 1), SRCALPHA)
ManipulatableDirtySprite._blank = self._blank
super(ManipulatableDirtySprite, self).__init__(*args, **kwargs)
self.image = image
#Property wrapper the rect that this object has.
@property
def rect(self):
"""Return a copy of this object's rect."""
self._update()
return self._rect.copy()
#No setter for rect.
@property
def top(self):
"""Return the true top of the sprite bounds."""
return self._rect.top
#No setter for top.
@property
def left(self):
"""Return the true left of the sprite bounds."""
return self._rect.left
#No setter for left.
@property
def bottom(self):
"""Return the true bottom of the sprite bounds."""
return self.rect.bottom
#No setter for bottom.
@property
def right(self):
"""Return the true right of the sprite bounds."""
return self.rect.right
#No setter for right.
@property
def topleft(self):
"""Return the true topleft of the sprite bounds."""
return self.rect.topleft
#No setter for topleft.
@property
def bottomleft(self):
"""Return the true bottomleft of the sprite bounds."""
return self.rect.bottomleft
#No setter for bottomleft.
@property
def topright(self):
"""Return the true topright of the sprite bounds."""
return self.rect.topright
#No setter for topright.
@property
def bottomright(self):
"""Return the true bottomright of the sprite bounds."""
return self.rect.bottomright
#No setter for bottomright.
@property
def midtop(self):
"""Return the true middle top of the sprite bounds."""
return self.rect.midtop
#No setter for midtop.
@property
def midleft(self):
"""Return the true middle left of the sprite bounds."""
return self.rect.midleft
#No setter for midleft.
@property
def midbottom(self):
"""Return the true middle bottom of the sprite bounds."""
return self.rect.midbottom
#No setter for midbottom.
@property
def midright(self):
"""Return the true middle right of the sprite bounds."""
return self.rect.midright
#No setter for midright.
@property
def center(self):
"""Return the true center of the sprite bounds."""
return self.rect.center
#No setter for center.
@property
def centerx(self):
"""Return the true center of x of the sprite bounds."""
return self.rect.centerx
#No setter for centerx.
@property
def centery(self):
"""Return the true center of y of the sprite bounds."""
return self.rect.centery
#No setter for centery.
@property
def size(self):
"""Return the true size of the sprite bounds."""
return self.rect.size
#No setter for size.
@property
def width(self):
"""Return the true width of the sprite bounds."""
return self.rect.width
#No setter for width.
@property
def height(self):
"""Return the true height of the sprite bounds."""
return self.rect.height
#No setter for height.
@property
def w(self):
"""Return the true width of the sprite bounds."""
return self.rect.w
#No setter for w.
@property
def h(self):
"""Return the true height of the sprite bounds."""
return self.rect.h
#No setter for h.
#Now define properties for positioning and manipulating the sprite.
@property
def x(self):
"""Return the x position of the specified center of the sprite."""
return self._x
@x.setter
def x(self, value):
"""Set the x position of the specified center of the sprite."""
self._x = value
self._pchanged = True
@property
def y(self):
"""Return the y position of the specified center of the sprite."""
return self._y
@y.setter
def y(self, value):
"""Set the y position of the specified center of the sprite."""
self._y = value
self._pchanged = True
@property
def xscale(self):
"""Return the x scaling value of the sprite."""
return self._xscale
@xscale.setter
def xscale(self, value):
"""Set the x scaling value of the sprite."""
self._xscale = value
self._schanged = True
@property
def yscale(self):
"""Return the y scaling value of the sprite."""
return self._yscale
@yscale.setter
def yscale(self, value):
"""Set the y scaling value of the sprite."""
self._yscale = value
self._schanged = True
@property
def xcenter(self):
"""Return the x offset of the center of the sprite."""
return self._xcenter
@xcenter.setter
def xcenter(self, value):
"""Set the x offset of the center of the sprite."""
self._xcenter = value
self._rchanged = True
@property
def ycenter(self):
"""Return the y offset of the center of the sprite."""
return self._ycenter
@ycenter.setter
def ycenter(self, value):
"""Set the y offset of the center of the sprite."""
self._ycenter = value
self._rchanged = True
@property
def rotation(self):
"""Return the rotation about the center of the sprite in radians."""
return self._rotation
@rotation.setter
def rotation(self, value):
"""Set the rotation about the center of the sprite in degrees."""
self._rotation = value
rads = math.radians(value)
self._sine = math.sin(rads)
#Negative because y increases going down the screen.
self._cosine = math.cos(rads)
self._rchanged = True
@property
def opacity(self):
"""Return the opacity of the sprite."""
return self._opacity
@opacity.setter
def opacity(self, value):
"""Set the opacity of the sprite."""
self._opacity = value
self._ochanged = True
@property
def image(self):
"""Return the cached image for this sprite.
If it needs to be updated first, it is."""
self._update()
return self._rotatedImage
@image.setter
def image(self, value):
"""Set the base image for this sprite."""
self._image = value
self._ichanged = True
def _update(self):
"""This update function updates the image and it's rect as needed."""
#Update image if needed.
if self._ichanged == True:
self._ichanged = False
#Destroy the opaque screen.
self._opaqueScreen = None
#Case: No image.
if self._image == None:
self._rotatedImage = ManipulatbleDirtySprite._blank
self._rect = pygame.Rect(self._x, self._y, 0, 0)
self._ochanged = False
self._schanged = False
self._rchanged = False
self._pchanged = False
return
#Otherwise, the opacity has changed!
self._ochanged = True
#Update opacity, if needed.
if self._ochanged == True:
self._ochanged = False
#By default, the scaling has also changed.
self._schanged = True
image = self._image
#Case: Image is transparent.
if self._opacity <= 0.0:
self._rotatedImage = ManipulatableDirtySprite._blank
self._rect = pygame.Rect(self._x, self._y, self._image.get_width(), self._image.get_height())
self._schanged = False
self._rchanged = False
self._pchanged = False
return
#Case: Image is not 100% opaque.
if self._opacity < 1.0:
if self._opaqueScreen == None:
self._opaqueScreen = pygame.Surface(self._image.get_size(), SRCALPHA)
image = image.copy()
self._opaqueScreen.fill((255, 255, 255, int(max((0, self._opacity * 255.0)))))
image.blit(self._opaqueScreen, (0, 0), special_flags = BLEND_RGBA_MULT)
self._opaqueImage = image
#Update scaling if needed.
if self._schanged == True:
self._schanged = False
#By default, the rotation has changed, too.
self._rchanged = True
image = self._opaqueImage
#Case: Scaled to 0.
if self._xscale == 0.0 or self._yscale == 0.0:
self._scaledImage = None
self._rotatedImage = ManipulatableDirtySprite._blank
self._rect = pygame.Rect(self._x, self._y, 0, 0)
self._schanged = False
self._rchanged = False
self._pchanged = False
return
#Case: Image needs scaling.
if math.fabs(self._xscale) != 1.0 or math.fabs(self._yscale) != 1.0:
image = pygame.transform.smoothscale(image, (math.fabs(image.get_width() * self._xscale), math.fabs(image.get_height() * self._yscale)))
if self._xscale < 0.0 or self._yscale < 0.0:
image = pygame.transform.flip(image, self._xscale < 0.0, self._yscale < 0.0)
self._scaledImage = image
#Update rotation, if needed.
if self._rchanged == True:
self._rchanged = False
#By default, the position has also changed.
self._pchanged = True
image = self._scaledImage
#image.set_at((int((1 - self._xcenter) * image.get_width() * 0.5), int((1 - self._ycenter) * image.get_height() * 0.5)), (0, 255, 0, 255))
#Case: Image needs rotation
if self._rotation:
image = pygame.transform.rotate(image, -self._rotation)
self._rotatedImage = image
#Update position, if needed.
if self._pchanged == True:
self._pchanged = False
rect = self._rotatedImage.get_rect()
##We now have the width and height of our sprite.
##Now calculate the top and left of the rect that
##will describe where the sprite is to be rendered.
#Get half width and half height.
w, h = self._scaledImage.get_width() * .5, self._scaledImage.get_height() * .5
#Find the rotated center of rotation
ox = self._xcenter * w
oy = self._ycenter * h
rox = ox * self._cosine - oy * self._sine
roy = oy * self._cosine + ox * self._sine
#We now know the offset of rotation relative to the center of the
#rotated image in pixels.
#Move the rect to the base x and y location.
rect.move_ip(self._x, self._y)
#Get the half width and half height of the rotated image.
rw, rh = rect.width * .5, rect.height * .5
#Now move the rect out by the rotated center of rotation
#and the half size of the image.
self._rect = rect.move(int(rox - rw), int(roy - rh))
if self.dirty == 0: self.dirty = 1
def test():
import test
pygame.init()
image = pygame.image.load("qbird.png")
sprite = ManipulatableDirtySprite(image = image)
clock = pygame.time.Clock()
group = pygame.sprite.LayeredDirty(sprite, layer = 0, _use_update = True)
keysused = {}
def testlogic(event):
if event == None:
time = clock.get_time()
if K_UP in keysused and keysused[K_UP]:
sprite.y -= 1
if K_DOWN in keysused and keysused[K_DOWN]:
sprite.y += 1
if K_LEFT in keysused and keysused[K_LEFT]:
sprite.x -= 1
if K_RIGHT in keysused and keysused[K_RIGHT]:
sprite.x += 1
if K_q in keysused and keysused[K_q]:
sprite.rotation -= 1
if K_e in keysused and keysused[K_e]:
sprite.rotation += 1
if K_w in keysused and keysused[K_w]:
sprite.ycenter -= 0.015625
if K_s in keysused and keysused[K_s]:
sprite.ycenter += 0.015625
if K_a in keysused and keysused[K_a]:
sprite.xcenter -= 0.015625
if K_d in keysused and keysused[K_d]:
sprite.xcenter += 0.015625
if K_r in keysused and keysused[K_r]:
sprite.yscale += 0.015625
if K_f in keysused and keysused[K_f]:
sprite.yscale -= 0.015625
if K_t in keysused and keysused[K_t]:
sprite.xscale += 0.015625
if K_g in keysused and keysused[K_g]:
sprite.xscale -= 0.015625
if K_y in keysused and keysused[K_y]:
sprite.opacity += 0.015625
if K_h in keysused and keysused[K_h]:
sprite.opacity -= 0.015625
return
if event.type == pygame.KEYDOWN:
if event.key == 27:
return True
keysused[event.key] = True
if event.type == pygame.KEYUP:
keysused[event.key] = False
def testrender(screen):
clock.tick()
bgd = pygame.Surface((screen.get_width(), screen.get_height()))
group.draw(screen, bgd = bgd)
test.test(testlogic, testrender)
if __name__ == '__main__': test()