-
Notifications
You must be signed in to change notification settings - Fork 1
/
RotatableSprite.py
executable file
·237 lines (205 loc) · 9.5 KB
/
RotatableSprite.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
import pygame
from pygame.locals import *
import math
class RotatableSprite(pygame.sprite.Sprite):
empty_canvas = None # Can't be initialized until pygame.init() has run
def __init__(self, texture, position = (0, 0),
angle = 0, scale = 1.0,
texture_rect = None,
width_factor = 1.0,
height_factor = 1.0,
center_of_rotation = None,
smooth_noncentered_zoom = False):
pygame.sprite.Sprite.__init__(self)
self.smooth_noncentered_zoom = smooth_noncentered_zoom
self.x, self.y = position
self.angle = angle
self.width_factor = width_factor
self.height_factor = height_factor
self.scale = scale
self.setTexture(texture, texture_rect, center_of_rotation)
if self.center_of_rotation == None:
self.center_of_rotation = self.texture_rect.center
# Set class variables
if RotatableSprite.empty_canvas == None:
empty_canvas = pygame.Surface((1, 1), SRCALPHA).convert_alpha()
empty_canvas.fill((0, 0, 0, 0))
self._update_draw_state()
def setTexture(self, texture, texture_rect = None, center_of_rotation = None):
if not (self.smooth_noncentered_zoom and center_of_rotation != None):
self.texture = texture
if texture_rect == None:
texture_rect = texture.get_rect()
self.texture_rect = texture_rect
if center_of_rotation == None:
center_of_rotation = self.texture_rect.center
self.center_of_rotation = center_of_rotation
else:
if texture_rect == None:
texture_rect = texture.get_rect()
if center_of_rotation == None:
center_of_rotation = texture_rect.center
orig_w, orig_h = texture.get_size()
dx, dy = (orig_w - 2*center_of_rotation[0],
orig_h - 2*center_of_rotation[1])
new_w, new_h = orig_w + abs(dx), orig_h + abs(dy)
centered_texture = pygame.Surface((new_w, new_h),
SRCALPHA).convert_alpha()
centered_texture.fill((0, 0, 0, 0))
centered_texture.blit(texture, (dx, dy), texture_rect)
self.texture = centered_texture
self.texture_rect = centered_texture.get_rect()
self.center_of_rotation = self.texture_rect.center
def update(self):
self._update_draw_state()
def _update_draw_state(self):
# TODO
# Be smarter, don't redraw stuff that has not changed
# Also keep the squeezed image
squeeze_size = (int(round(self.texture_rect.width *
self.width_factor)),
int(round(self.texture_rect.height *
self.height_factor)))
sz_before_rot = (int(round(self.texture_rect.width *
self.width_factor *
self.scale)),
int(round(self.texture_rect.height *
self.height_factor *
self.scale)))
if any(d == 0 for d in sz_before_rot):
# Don't show anything
canvas = empty_canvas
else:
# --- Start from the original texture ---
if self.texture_rect != self.texture.get_rect:
canvas = self.texture.subsurface(self.texture_rect)
else:
canvas = self.texture
is_right_angle = self.angle % 90 == 0
# --- Rotate ---
if is_right_angle:
if sz_before_rot != self.texture_rect.size:
canvas = pygame.transform.smoothscale(canvas, sz_before_rot)
# Make right angle images "clean"
canvas = pygame.transform.rotate(canvas, -self.angle)
else:
if self.width_factor != 1.0 or self.height_factor != 1.0:
canvas = pygame.transform.smoothscale(canvas, squeeze_size)
canvas = pygame.transform.rotozoom(canvas, -self.angle, self.scale)
self.image = canvas
px, py = self.texture_rect.center
ox, oy = self.center_of_rotation
dx, dy = px-ox, py-oy
sdx, sdy = self.scale*self.width_factor*dx, self.scale*self.height_factor*dy
radians = self.angle * math.pi / 180.0
s, c = math.sin(radians), math.cos(radians)
x_add, y_add = c*sdx - s*sdy, c*sdy + s*sdx
self.rect = self.image.get_rect(center = (self.x + x_add, self.y + y_add))
def screen_2_texture_pos(self, pos):
sx, sy = pos
# diff to middle
mdx, mdy = sx - self.rect.centerx, sy - self.rect.centery
# rotate back
radians = -self.angle * math.pi / 180.0
s, c = math.sin(radians), math.cos(radians)
x, y = c*mdx - s*mdy, c*mdy + s*mdx
unrot_w, unrot_h = (int(round(self.texture_rect.width *
self.width_factor *
self.scale)),
int(round(self.texture_rect.height *
self.height_factor *
self.scale)))
sx, sy = x/unrot_w, y/unrot_h
return sx + 0.5, sy + 0.5
def get_texture_at(self, pos):
"""
Gets the pixel value at pos in texture. The coordiates are (0, 0) in the
top left corner and (1, 1) in the bottom right corner.
"""
nx, ny = pos
tex_pos = (int(self.texture_rect.left + nx * self.texture_rect.width),
int(self.texture_rect.top + ny * self.texture_rect.height))
return self.texture.get_at(tex_pos)
def covers(self, pos):
if not self.rect.collidepoint(pos):
return False
ipos = self.screen_2_texture_pos(pos)
# The pixel is inside the texture and is not transparent
return (ipos[0] >= 0.0 and ipos[0] <= 1.0 and
ipos[1] >= 0.0 and ipos[1] <= 1.0 and
self.get_texture_at(ipos)[3] != 0)
if __name__ == "__main__":
def draw_sprites():
sprite_group.clear(screen, background)
sprite_group.update() # Calls update on all sprites
dirty = sprite_group.draw(screen)
pygame.display.update(dirty)
pygame.init()
from pygame.colordict import THECOLORS
width, height = 320, 240
screen = pygame.display.set_mode((width, height), 0*FULLSCREEN)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(THECOLORS['white'])
for x in xrange(0, width, 10):
for y in xrange(0, height, 10):
background.set_at((x, y), THECOLORS["gray"])
screen.blit(background, (0, 0))
pygame.display.update()
mouse_texture = pygame.image.load('mouse_pointer.png').convert_alpha()
mouse_texture_nopad = \
pygame.image.load('mouse_pointer_unpadded.png').convert_alpha()
# A rotating sprite, one rotation in four seconds
rotating_sprite = RotatableSprite(mouse_texture, (20, 20))
# A sprite that use a texture without the single pixel transparent border
# This will cause the border to be ugly when rotating
noborder_sprite = RotatableSprite(mouse_texture_nopad, (20, 60))
# A sprite that only use the left part of the texture
half_rect = mouse_texture.get_rect(width = mouse_texture.get_width()/2)
half_sprite = \
RotatableSprite(mouse_texture, (60, 20),
texture_rect = half_rect,
angle = 180)
# A sprite of half width
squeezed_sprite = RotatableSprite(mouse_texture, (100, 20),
width_factor = 0.5)
# A sprite with double height
high_sprite = RotatableSprite(mouse_texture, (160, 30), height_factor = 2)
# A side, double sized sprite
big_sprite = RotatableSprite(mouse_texture, (220, 60),
width_factor = 2.0, scale = 2.0)
# A sprite rotating about it's tip
tip_sprite = RotatableSprite(mouse_texture, (90, 90),
scale = 2.0,
center_of_rotation = (1, 1))
# A sprite rotating about it's tip smooth rot
smooth_tip_sprite = RotatableSprite(mouse_texture, (150, 90),
scale = 2.0,
center_of_rotation = (1, 1), # Top left, not counting the one pixel border
smooth_noncentered_zoom = True)
# A sprite rotating about it's tip smooth rot
smooth_tip_sprite2 = RotatableSprite(mouse_texture, (150, 150),
scale = 2.0,
smooth_noncentered_zoom = True)
four_sec_rot_sprites = [rotating_sprite, half_sprite, squeezed_sprite,
noborder_sprite, high_sprite, big_sprite,
tip_sprite, smooth_tip_sprite, smooth_tip_sprite2]
sprite_group = pygame.sprite.OrderedUpdates(four_sec_rot_sprites)
clock = pygame.time.Clock()
run = True
MS_PER_SEC = 1000.0
while run:
# Try to keep a fps of 40. Returns the ms passed since last call.
secs = clock.tick(40) / MS_PER_SEC
events = pygame.event.get()
for event in events:
if (event.type == QUIT or
(event.type == KEYDOWN and event.key in [K_ESCAPE, K_q])):
run = False
for s in four_sec_rot_sprites:
s.angle += 0.25 * 360 * secs
if smooth_tip_sprite.covers(pygame.mouse.get_pos()):
pygame.mouse.set_cursor(*pygame.cursors.broken_x)
else:
pygame.mouse.set_cursor(*pygame.cursors.diamond)
draw_sprites()