forked from pygame/pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsprite.py
261 lines (200 loc) · 6.83 KB
/
testsprite.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
#!/usr/bin/env python
""" pg.examples.testsprite
Like the testsprite.c that comes with libsdl, this pygame version shows
lots of sprites moving around.
It is an abomination of ugly code, and mostly used for testing.
See pg.examples.aliens for some prettyier code.
"""
import sys
import os
from random import randint
from time import time
import pygame as pg
if "-psyco" in sys.argv:
try:
import psyco
psyco.full()
except Exception:
print("No psyco for you! psyco failed to import and run.")
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, "data")
# use this to use update rects or not.
# If the screen is mostly full, then update rects are not useful.
update_rects = True
if "-update_rects" in sys.argv:
update_rects = True
if "-noupdate_rects" in sys.argv:
update_rects = False
use_static = False
if "-static" in sys.argv:
use_static = True
use_layered_dirty = False
if "-layered_dirty" in sys.argv:
update_rects = True
use_layered_dirty = True
flags = 0
if "-flip" in sys.argv:
flags ^= pg.DOUBLEBUF
if "-fullscreen" in sys.argv:
flags ^= pg.FULLSCREEN
if "-sw" in sys.argv:
flags ^= pg.SWSURFACE
use_rle = True
if "-hw" in sys.argv:
flags ^= pg.HWSURFACE
use_rle = False
if "-scaled" in sys.argv:
flags ^= pg.SCALED
screen_dims = [640, 480]
if "-height" in sys.argv:
i = sys.argv.index("-height")
screen_dims[1] = int(sys.argv[i + 1])
if "-width" in sys.argv:
i = sys.argv.index("-width")
screen_dims[0] = int(sys.argv[i + 1])
if "-alpha" in sys.argv:
use_alpha = True
else:
use_alpha = False
print(screen_dims)
##class Thingy(pg.sprite.Sprite):
## images = None
## def __init__(self):
## pg.sprite.Sprite.__init__(self)
## self.image = Thingy.images[0]
## self.rect = self.image.get_rect()
## self.rect.x = randint(0, screen_dims[0])
## self.rect.y = randint(0, screen_dims[1])
## #self.vel = [randint(-10, 10), randint(-10, 10)]
## self.vel = [randint(-1, 1), randint(-1, 1)]
##
## def move(self):
## for i in [0, 1]:
## nv = self.rect[i] + self.vel[i]
## if nv >= screen_dims[i] or nv < 0:
## self.vel[i] = -self.vel[i]
## nv = self.rect[i] + self.vel[i]
## self.rect[i] = nv
class Thingy(pg.sprite.DirtySprite):
images = None
def __init__(self):
## pg.sprite.Sprite.__init__(self)
pg.sprite.DirtySprite.__init__(self)
self.image = Thingy.images[0]
self.rect = self.image.get_rect()
self.rect.x = randint(0, screen_dims[0])
self.rect.y = randint(0, screen_dims[1])
# self.vel = [randint(-10, 10), randint(-10, 10)]
self.vel = [randint(-1, 1), randint(-1, 1)]
self.dirty = 2
def update(self):
for i in [0, 1]:
nv = self.rect[i] + self.vel[i]
if nv >= screen_dims[i] or nv < 0:
self.vel[i] = -self.vel[i]
nv = self.rect[i] + self.vel[i]
self.rect[i] = nv
class Static(pg.sprite.DirtySprite):
images = None
def __init__(self):
pg.sprite.DirtySprite.__init__(self)
self.image = Static.images[0]
self.rect = self.image.get_rect()
self.rect.x = randint(0, 3 * screen_dims[0] / 4)
self.rect.y = randint(0, 3 * screen_dims[1] / 4)
def main(
update_rects=True,
use_static=False,
use_layered_dirty=False,
screen_dims=[640, 480],
use_alpha=False,
flags=0,
):
"""Show lots of sprites moving around
Optional keyword arguments:
update_rects - use the RenderUpdate sprite group class (default True)
use_static - include non-moving images (default False)
use_layered_dirty - Use the FastRenderGroup sprite group (default False)
screen_dims - Pygame window dimensions (default [640, 480])
use_alpha - use alpha blending (default False)
flags - additional display mode flags (default no additional flags)
"""
if use_layered_dirty:
update_rects = True
pg.init() # needed to initialise time module for get_ticks()
pg.display.init()
# if "-fast" in sys.argv:
screen = pg.display.set_mode(screen_dims, flags, vsync="-vsync" in sys.argv)
# this is mainly for GP2X, so it can quit.
pg.joystick.init()
num_joysticks = pg.joystick.get_count()
if num_joysticks > 0:
stick = pg.joystick.Joystick(0)
stick.init() # now we will receive events for the joystick
screen.fill([0, 0, 0])
pg.display.flip()
sprite_surface = pg.image.load(os.path.join(data_dir, "asprite.bmp"))
sprite_surface2 = pg.image.load(os.path.join(data_dir, "static.png"))
if use_rle:
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY | pg.RLEACCEL)
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY | pg.RLEACCEL)
else:
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY)
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY)
if use_alpha:
sprite_surface = sprite_surface.convert_alpha()
sprite_surface2 = sprite_surface2.convert_alpha()
else:
sprite_surface = sprite_surface.convert()
sprite_surface2 = sprite_surface2.convert()
Thingy.images = [sprite_surface]
if use_static:
Static.images = [sprite_surface2]
if len(sys.argv) > 1:
try:
numsprites = int(sys.argv[-1])
except Exception:
numsprites = 100
else:
numsprites = 100
sprites = None
if use_layered_dirty:
## sprites = pg.sprite.FastRenderGroup()
sprites = pg.sprite.LayeredDirty()
else:
if update_rects:
sprites = pg.sprite.RenderUpdates()
else:
sprites = pg.sprite.Group()
for i in range(0, numsprites):
if use_static and i % 2 == 0:
sprites.add(Static())
sprites.add(Thingy())
frames = 0
start = time()
background = pg.Surface(screen.get_size())
background = background.convert()
background.fill([0, 0, 0])
going = True
while going:
if not update_rects:
screen.fill([0, 0, 0])
## for sprite in sprites:
## sprite.move()
if update_rects:
sprites.clear(screen, background)
sprites.update()
rects = sprites.draw(screen)
if update_rects:
pg.display.update(rects)
else:
pg.display.flip()
for event in pg.event.get():
if event.type in [pg.QUIT, pg.KEYDOWN, pg.QUIT, pg.JOYBUTTONDOWN]:
going = False
frames += 1
end = time()
print(f"FPS: {frames / (end - start):f}")
pg.quit()
if __name__ == "__main__":
main(update_rects, use_static, use_layered_dirty, screen_dims, use_alpha, flags)