-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_03_kinds.py
207 lines (172 loc) · 6.42 KB
/
04_03_kinds.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
#!/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 SimpleAnimation(Sprite):
def __init__(self, frames, topleft_pos):
Sprite.__init__(self)
self.frames = frames # save the images in here
self.current = 0 # idx of current image of the animation
self.image = frames[0] # just to prevent some errors
self.rect = self.image.get_rect() # same here
self.rect.topleft = topleft_pos
self.playing = 0
def update(self, *args):
if self.playing: # only update the animation if it is playing
self.current += 1
if self.current == len(self.frames):
self.current = 0
self.image = self.frames[self.current]
# only needed if size changes within the animation
self.rect = self.image.get_rect(center=self.rect.center)
def start(self):
self.current = 0
self.playing = True
def stop(self):
self.playing = False
def pause(self):
self.playing = False
def resume(self):
self.playing = True
class AnimationGroup(pygame.sprite.RenderUpdates):
def start(self):
for spr in self.sprites():
spr.start()
def stop(self):
for spr in self.sprites():
spr.stop()
def pause(self):
for spr in self.sprites():
spr.pause()
def resume(self):
for spr in self.sprites():
spr.resume()
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 name not in cache.keys(): # 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))
# define a variable to control the main loop
running = True
# generate a list of names
image_names = get_names_list(os.path.normpath("data/ball"), "png", 20, 2, 1)
# generate a sequence, here simply 0,1,2,3...
sequence = range(20)
# load images
frames = get_sequence(image_names, sequence)
# prepare animation
anims = AnimationGroup()
anims.add(SimpleAnimation(frames, (100, 300 - 75)))
# generate another sequence, here simply 20,19,18,17,16,...
sequence = reversed(range(20)) # reverse
# load images
frames = get_sequence(image_names, sequence)
anims.add(SimpleAnimation(frames, (325, 300 - 75)))
# generate another sequence, here simply 0,1,2,3,...,19,20,20,19,...,1,0
sequence = [*range(20), *reversed(range(20))] # forward+reverse = pingpong
# load images
frames = get_sequence(image_names, sequence)
anims.add(SimpleAnimation(frames, (550, 300 - 75)))
anims.start()
# use a clock to fix the fps of the main loop
clock = pygame.time.Clock()
fps = 20
pygame.key.set_repeat(500, 30)
font = pygame.font.Font(None, 25)
screen.blit(font.render("forward", 3, (255, 255, 255)), (100, 150))
screen.blit(font.render("reverse", 3, (255, 255, 255)), (325, 150))
screen.blit(font.render("pingpong", 3, (255, 255, 255)), (550, 150))
pygame.display.flip()
# 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:
fps -= 1
fps = max(fps, 1)
elif event.key == pygame.K_q:
fps += 1
elif event.key == pygame.K_SPACE:
if anims.sprites()[0].playing:
anims.pause()
else:
anims.resume()
elif event.key == pygame.K_r:
if anims.sprites()[0].playing:
anims.stop()
else:
anims.start()
# update the caption
pygame.display.set_caption(caption_str + " set fps: " + str(fps) + "/" + "%2d" % (clock.get_fps()))
# fix the fps
clock.tick(fps)
# erase things
screen.fill((0, 0, 0))
# update anim
anims.update()
# draw anim
dirty_rect = anims.draw(screen)
# update screen
pygame.display.update(dirty_rect)
# 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()