forked from pygame/pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask.py
212 lines (164 loc) · 5.67 KB
/
mask.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
#!/usr/bin/env python
""" pygame.examples.mask
A pygame.mask collision detection production.
Brought
to
you
by
the
pixels
0000000000000
and
111111
This is 32 bits:
11111111111111111111111111111111
There are 32 or 64 bits in a computer 'word'.
Rather than using one word for a pixel,
the mask module represents 32 or 64 pixels in one word.
As you can imagine, this makes things fast, and saves memory.
Compute intensive things like collision detection,
and computer vision benefit greatly from this.
This module can also be run as a stand-alone program, excepting
one or more image file names as command line arguments.
"""
import sys
import os
import random
import pygame as pg
def maskFromSurface(surface, threshold=127):
return pg.mask.from_surface(surface, threshold)
def vadd(x, y):
return [x[0] + y[0], x[1] + y[1]]
def vsub(x, y):
return [x[0] - y[0], x[1] - y[1]]
def vdot(x, y):
return x[0] * y[0] + x[1] * y[1]
class Sprite:
def __init__(self, surface, mask=None):
self.surface = surface
if mask:
self.mask = mask
else:
self.mask = maskFromSurface(self.surface)
self.setPos([0, 0])
self.setVelocity([0, 0])
def setPos(self, pos):
self.pos = [pos[0], pos[1]]
def setVelocity(self, vel):
self.vel = [vel[0], vel[1]]
def move(self, dr):
self.pos = vadd(self.pos, dr)
def kick(self, impulse):
self.vel[0] += impulse[0]
self.vel[1] += impulse[1]
def collide(self, s):
"""Test if the sprites are colliding and
resolve the collision in this case."""
offset = [int(x) for x in vsub(s.pos, self.pos)]
overlap = self.mask.overlap_area(s.mask, offset)
if overlap == 0:
return
"""Calculate collision normal"""
nx = self.mask.overlap_area(
s.mask, (offset[0] + 1, offset[1])
) - self.mask.overlap_area(s.mask, (offset[0] - 1, offset[1]))
ny = self.mask.overlap_area(
s.mask, (offset[0], offset[1] + 1)
) - self.mask.overlap_area(s.mask, (offset[0], offset[1] - 1))
if nx == 0 and ny == 0:
"""One sprite is inside another"""
return
n = [nx, ny]
dv = vsub(s.vel, self.vel)
J = vdot(dv, n) / (2 * vdot(n, n))
if J > 0:
"""Can scale up to 2*J here to get bouncy collisions"""
J *= 1.9
self.kick([nx * J, ny * J])
s.kick([-J * nx, -J * ny])
return
# """Separate the sprites"""
# c1 = -overlap/vdot(n,n)
# c2 = -c1/2
# self.move([c2*nx,c2*ny])
# s.move([(c1+c2)*nx,(c1+c2)*ny])
def update(self, dt):
self.pos[0] += dt * self.vel[0]
self.pos[1] += dt * self.vel[1]
def main(*args):
"""Display multiple images bounce off each other using collision detection
Positional arguments:
one or more image file names.
This pg.masks demo will display multiple moving sprites bouncing
off each other. More than one sprite image can be provided.
"""
if len(args) == 0:
raise ValueError("Require at least one image file name: non given")
print("Press any key to quit")
pg.init()
screen = pg.display.set_mode((640, 480))
images = []
masks = []
for impath in args:
images.append(pg.image.load(impath).convert_alpha())
masks.append(maskFromSurface(images[-1]))
numtimes = 10
import time
t1 = time.time()
for x in range(numtimes):
unused_mask = maskFromSurface(images[-1])
t2 = time.time()
print("python maskFromSurface :%s" % (t2 - t1))
t1 = time.time()
for x in range(numtimes):
unused_mask = pg.mask.from_surface(images[-1])
t2 = time.time()
print("C pg.mask.from_surface :%s" % (t2 - t1))
sprites = []
for i in range(20):
j = i % len(images)
s = Sprite(images[j], masks[j])
s.setPos(
(
random.uniform(0, screen.get_width()),
random.uniform(0, screen.get_height()),
)
)
s.setVelocity((random.uniform(-5, 5), random.uniform(-5, 5)))
sprites.append(s)
pg.time.set_timer(pg.USEREVENT, 33)
while 1:
event = pg.event.wait()
if event.type == pg.QUIT:
return
elif event.type == pg.USEREVENT:
# Do both mechanics and screen update
screen.fill((240, 220, 100))
for i, sprite in enumerate(sprites):
for j in range(i + 1, len(sprites)):
sprite.collide(sprites[j])
for s in sprites:
s.update(1)
if s.pos[0] < -s.surface.get_width() - 3:
s.pos[0] = screen.get_width()
elif s.pos[0] > screen.get_width() + 3:
s.pos[0] = -s.surface.get_width()
if s.pos[1] < -s.surface.get_height() - 3:
s.pos[1] = screen.get_height()
elif s.pos[1] > screen.get_height() + 3:
s.pos[1] = -s.surface.get_height()
screen.blit(s.surface, s.pos)
pg.display.update()
elif event.type == pg.KEYDOWN:
return
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: mask.py <IMAGE> [<IMAGE> ...]")
print("Let many copies of IMAGE(s) bounce against each other")
print("Press any key to quit")
main_dir = os.path.split(os.path.abspath(__file__))[0]
imagename = os.path.join(main_dir, "data", "chimp.png")
main(imagename)
else:
main(*sys.argv[1:])
pg.quit()