-
Notifications
You must be signed in to change notification settings - Fork 0
/
animator.py
222 lines (180 loc) · 6.11 KB
/
animator.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
import os, math, sys
import pygame, pygame.font, pygame.image, pygame.mixer, pygame.transform
from pygame.locals import *
class AnimatedObject:
def getFrame(time):
return
def getSurface(time):
return
def getPosition(time):
return
class AnimatedMoveObject:
def __init__(self, image, startpos, endpos, playtime, delay=0):
self.image = image
self.startpos = startpos
self.endpos = endpos
self.playtime = playtime
self.delta = (endpos[0]-startpos[0])/float(playtime), (endpos[1]-startpos[1])/float(playtime)
self.delay = delay
def getPosition(self, time):
offset = (time-self.delay)
x = self.startpos[0]+int(self.delta[0]*offset)
y = self.startpos[1]+int(self.delta[1]*offset)
return x,y
def getSurface(self, time):
return self.image
def visable(self, time):
if time < self.delay or time-self.delay > self.playtime: return None
return 1
class AnimatedAlphaObject:
def __init__(self, image, position, startalpha, endalpha, playtime, delay=0):
#self.image = pygame.Surface( (image.get_width(),image.get_height()),pygame.locals.SRCALPHA)
#self.image.blit(image,(0,0))
self.image = image.convert()
self.startalpha = startalpha
self.endalpha = endalpha
self.playtime = playtime
if startalpha < endalpha:
self.delta = (endalpha - startalpha)/float(playtime)
else:
self.delta = (startalpha - endalpha)/float(playtime)
self.delay = delay
self.position = position
def getPosition(self, time):
return self.position
def getSurface(self, time):
offset = (time-self.delay)
alpha = int((self.delta*offset))
# print self.image.get_alpha()
self.image.set_alpha(alpha)
return self.image
def visable(self, time):
if time < self.delay or time-self.delay > self.playtime: return None
return 1
class AnimatedTurnObject:
def __init__(self, image, position, number_of_turns, playtime, delay=0):
self.image = image
self.playtime = playtime
self.delta = (number_of_turns*3.14)/float(playtime)
self.delay = delay
self.position = position
def getPosition(self, time):
offset = (time-self.delay)
position = (self.position[0]-(math.sin(self.delta*offset)*self.image.get_width())/2, self.position[1])
return position
def getSurface(self, time):
offset = (time-self.delay)
return pygame.transform.scale(self.image, (int(math.sin(self.delta*offset)*self.image.get_width()), self.image.get_height()))
def visable(self, time):
if time < self.delay or time-self.delay > self.playtime: return None
return 1
class AnimatedRotateObject:
def __init__(self, image, position, number_of_turns, playtime, delay=0):
self.image = image
self.playtime = playtime
self.delta = (number_of_turns*360)/float(playtime)
self.delay = delay
self.position = position
self.cache={}
for angle in range(360):
self.cache[angle]=pygame.transform.rotozoom(self.image, angle, 1)
def getPosition(self, time):
offset = (time-self.delay)
angle = int(self.delta*offset)%360
return self.position[0]-self.cache[angle].get_width()/2, self.position[1]-self.cache[angle].get_height()/2
def getSurface(self, time):
offset = (time-self.delay)
#return pygame.transform.rotozoom(self.image, self.delta*offset, 1)
return self.cache[int(self.delta*offset)%360]
def visable(self, time):
if time < self.delay or time-self.delay > self.playtime: return None
return 1
# Based on wavey by Pete Shinners
class AnimatedWaveObject:
def __init__(self, x, y, font, message, fontcolor, offset=0.0, speed=0.001, amount=10):
self.x = x
self.y = y
self.base = font.render(message, 0, fontcolor)
self.steps = range(0, self.base.get_width(), 2)
self.amount = amount
self.size = self.base.get_rect().inflate(0, amount).size
self.offset = offset
self.speed = speed
def getSurface(self, time):
s = pygame.Surface(self.size)
height = self.size[1]
self.offset += self.speed * time
for step in self.steps:
src = Rect(step, 0, 2, height)
dst = src.move(0, math.cos(self.offset + step*.02)*self.amount)
s.blit(self.base, dst, src)
return s
def getPosition(self, time):
return (self.x, self.y)
def visable(self, time):
return 1
class AnimatedFPSTimerObject:
def __init__(self, x, y, font, fontcolor, background, average=0):
self.x = x
self.y = y
self.average = average
self.background = pygame.Surface((200,50))
self.background.blit(background,(0,0))
self.oldtime = 0
self.font = font
self.fontcolor = fontcolor
def getSurface(self, time):
if (time-self.oldtime):
fps=1000/(time-self.oldtime)
else:
fps = 0
message = "FPS: "+str(fps)
surface = pygame.Surface((200,50))
tekst = self.font.render(message, 0, self.fontcolor)
surface.blit(self.background,(0,0))
surface.blit(tekst,(0,0))
self.oldtime = time
return surface
def getPosition(self, time):
return (self.x, self.y)
def visable(self, time):
return 1
class Animator:
def __init__(self, screen, background, playtime, objects = None):
self.screen = screen
self.background = background
self.playtime = playtime
self.objects = []
self.starttime = -1
self.oldrects = {}
def addObject(self, object):
self.objects.append(object)
def drawFrame(self, time):
for rect in self.oldrects.values():
self.screen.blit(self.background.subsurface(rect), rect.topleft)
rects_to_update = []
newrects={}
for object in self.objects:
if(object.visable(time)):
image = object.getSurface(time)
position = object.getPosition(time)
rect = self.screen.blit(image, position)
newrects[object]=rect
if self.oldrects.has_key(object):
rects_to_update.append(rect.union(self.oldrects[object]))
else:
rects_to_update.append(rect)
for key in (filter(lambda x,l=newrects.keys(): x not in l,self.oldrects.keys())):
rects_to_update.append(self.oldrects[key])
pygame.display.update(rects_to_update)
self.oldrects = newrects
def play(self):
self.screen.blit(self.background, (0, 0))
pygame.display.flip()
self.starttime = pygame.time.get_ticks()
time = pygame.time.get_ticks() - self.starttime
while time < self.playtime:
time = pygame.time.get_ticks() - self.starttime
self.drawFrame(time)
# Reset the clippin
self.screen.set_clip(((0,0), self.background.get_size()))