-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_11_movement.py
277 lines (235 loc) · 9.13 KB
/
04_11_movement.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
#!/usr/bin/env python
#
#
#
#
#
# documentation string of this module
"""
Animation tutorials.
"""
# some informational variables
__author__ = "$Author: DR0ID $"
__version__ = "$Revision: 184 $"
__date__ = "$Date: 2007-08-24 19:22:06 +0200 (Fr, 24 Aug 2007) $"
__license__ = 'GPL2, see: gpl-2.0.txt'
__copyright__ = "DR0ID (c) 2006-2007 http://dr0id.ch.vu"
#----------------------------- actual code --------------------------------
# import the modules used
import pygame
import os
from pygame.sprite import Sprite
class TimedAnimation(Sprite):
def __init__(self, frames, pos, fps=40):
Sprite.__init__(self)
self.image = frames
self.rect = pygame.Rect(pos, (150,150))
self.source_rect = pygame.Rect(0,0,150,150)
self.current = 0
self.playing = 0 # to know if it is playing
self._next_update = 0 # next time it has to be updated in ms
self._period = 1000./fps # frequency/period of the animation in ms
self._frame_w = 150
self._len = 40
# movement
self.speed = 0 # [px/s]
self.optimal_speed = 80. # [px/s] assuming at 40 [frames/s]
self.optimal_fps = 40. # [frames/s]
self.x, self.y = pos
def update(self, dt, t):
if self.playing:
# accumulate time since last update
self._next_update += dt
# if more time has passed as a period, then we need to update
if self._next_update >= self._period:
# skipping frames if too much time has passed
# since _next_update is bigger than period this is at least 1
self.current += int(self._next_update/self._period)
# time that already has passed since last update
self._next_update %= self._period
# known code
self.current %= self._len
# update image
self.source_rect.x = self.current*self._frame_w
# updating movement
## self.x += (self.speed * self._len * dt/1000.) # [px/s]/[ms/s]*[ms]
self.x += (self.speed * dt/1000.) # [px/s]*[ms]/[ms/s]
self.rect.x = int(self.x)
# dont let it vanish from screen
if self.x > 800-75:
self.x = -75
self.current = 0
def start(self, t):
self.current = 0
self.playing = True
def stop(self, t):
self.playing = False
def pause(self, t):
self.playing = False
def resume(self, t):
self.playing = True
def set_fps(self, fps):
self._period = 1000./fps
def set_speed1(self, speed):
if speed > 0:
self.playing = True
self.speed = speed
else:
self.stop(0)
self.speed = 0
print '-------------------'
print 'speed', speed
print 'fps1:',1000./self._period
def set_speed2(self, speed):
if speed > 0:
self.playing = True
self.speed = speed
self._period = 1000./(speed/self.optimal_speed*self.optimal_fps) # 1000[ms/s]/([px/s]/[px/s]*[frames/s])= ms
else:
self.stop(0)
self.speed = 0
print 'real speed:', speed
print 'fps2',1000./self._period
class AnimationGroup(pygame.sprite.RenderUpdates):
def start(self, t):
for spr in self.sprites():
spr.start(t)
def stop(self, t):
for spr in self.sprites():
spr.stop(t)
def pause(self, t):
for spr in self.sprites():
spr.pause(t)
def resume(self, t):
for spr in self.sprites():
spr.resume(t)
def update(self, dt, t):
for spr in self.sprites():
spr.update(dt, t)
cache = {} # has to be global (or a class variable)
def get_sequence(frames_names, sequence, optimize=True):
frames = []
global cache
for name in frames_names:
if not cache.has_key(name): # check if it has benn loaded already
image = pygame.image.load(name) # not optimized
if optimize:
if image.get_alpha() is not None:
image = image.convert_alpha()
else:
image = image.convert()
cache[name] = image
# constructs a sequence of frames equal to frames_names
frames.append(cache[name])
frames2 = []
for idx in sequence:
# constructing the animation sequence according to sequence
frames2.append(frames[idx])
return frames2
def get_names_list(basename, ext, num, num_digits=1, offset=0):
names = []
# format string basename+zero_padded_number+.+ext
format = "%s%0"+str(num_digits)+"d.%s"
for i in range(offset, num+1):
names.append(format % (basename, i,ext))
return names
# define a main function
def main():
# initialize the pygame module
pygame.init()
# load and set the logo
logo = pygame.image.load(os.path.normpath("data/logo32x32.png"))
pygame.display.set_icon(logo)
caption_str = os.path.split(__file__)[1]+" keys: a/q: change fps, space: pause/resume, r: start/stop "
pygame.display.set_caption(caption_str)
# create a surface on screen that has the size of 800 x 600
screen = pygame.display.set_mode((800,600))
bgd = pygame.Surface(screen.get_size()).convert()
bgd.fill((255,255,255))
# define a variable to control the main loop
running = True
# generate a list of names
## frames = pygame.image.load(os.path.normpath('data/locomotive.png'))
frames = pygame.image.load(os.path.normpath('data/locomotive.png'))
# prepare animation
anims = AnimationGroup()
anim_faulty = TimedAnimation(frames, (-75, 150))
anim_ok = TimedAnimation(frames, (-75, 450))
anims.add(anim_faulty)
anims.add(anim_ok)
# use a clock to fix the fps of the main loop
clock = pygame.time.Clock()
fps = 100
pygame.key.set_repeat(500, 30)
font = pygame.font.Font(None, 25)
screen.blit(font.render("The animations run with fps depending on speed!", 3, (255, 255, 255)), (200, 300))
screen.blit(font.render("change speed of steamer using 'a'/'q'", 3, (255, 255, 255)), (200, 325))
screen.blit(font.render("upper is wrong (because wheels slip), beneath its good", 3, (255, 255, 255)), (200, 350))
pygame.display.flip()
dt = 0
t = 0
speed = 0
last_x1 = 0
last_x2 = 0
# main loop
while running:
# event handling, gets all event from the eventqueue
for event in pygame.event.get():
# only do something if the event if of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_a:
speed -= 1
speed = max(speed, 0)
## fps -= 1
## fps = max(fps, 1)
elif event.key == pygame.K_q:
speed += 1
## fps += 1
elif event.key == pygame.K_SPACE:
if anims.sprites()[0].playing:
anims.pause(t)
else:
anims.resume(t)
elif event.key == pygame.K_r:
if anims.sprites()[0].playing:
anims.stop(t)
else:
anims.start(t)
# update speed
anim_faulty.set_speed1(speed)
anim_ok.set_speed2(speed)
# update the caption
pygame.display.set_caption(caption_str+" set fps: "+str(fps)+"/"+"%2d"%(clock.get_fps()))
# fix the fps
dt = clock.tick(fps)
t = pygame.time.get_ticks()
# erase things
## screen.fill((0,0,0))
## screen.blit(bgd, (0,0))
# update anim
anims.update(dt, t)
# draw anim
dirty1 = screen.blit(anim_faulty.image, anim_faulty.rect, anim_faulty.source_rect)
dirty2 = screen.blit(anim_ok.image, anim_ok.rect, anim_ok.source_rect)
if anim_faulty.current == 0:
## print 'faulty:',anim_faulty.x-last_x1
last_x1 = anim_faulty.x
pygame.draw.line(screen, (0,255,0), (anim_faulty.x, 125), ((anim_faulty.x, 175)))
if anim_ok.current == 0:
## print 'good:',anim_ok.x-last_x2
last_x2 = anim_ok.x
pygame.draw.line(screen, (255,0,0), (anim_ok.x, 425), ((anim_ok.x, 600)))
# update screen
pygame.display.update()
screen.blit(bgd, dirty1, dirty1)
screen.blit(bgd, dirty2, dirty2)
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
# call the main function
main()