import
pygame,random,time,math
class
Spot(
object
):
def
__init__(ss,x,y,ang,leti):
ss.x
=
x
ss.y
=
y
ss.ang
=
ang
ss.leti
=
leti
def
makesnake(startang,rot,rad,wordreps,w,h):
step
=
w
/
(wordreps
*
6.0
)
nsteps
=
wordreps
*
6
spots
=
[]
ang
=
startang
x
=
0
leti
=
0
for
i
in
range
(nsteps):
y
=
(h
/
2
)
+
(math.sin(math.radians(ang))
*
rad)
s
=
Spot(x,y,
None
,leti)
spots.append(s)
x
+
=
step
ang
=
(ang
+
rot)
%
360
leti
=
(leti
+
1
)
%
6
return
spots
def
makesnakeanim(ang,angstep,rot,rotstep,rotmax,rotmin,rad,radstep,radmax,radmin,wordreps,w,h,frames):
snakeanim
=
[]
for
i
in
range
(frames):
spots
=
makesnake(ang,rot,rad,wordreps,w,h)
setspotangs(spots)
snakeanim.append(spots)
ang
+
=
angstep
rot
+
=
rotstep
if
rot>rotmax
or
rot<rotmin:
rotstep
*
=
-
1
rot
+
=
rotstep
*
2
rad
+
=
radstep
if
rad>radmax
or
rad<radmin:
radstep
*
=
-
1
rad
+
=
radstep
*
2
return
snakeanim
def
drawsnake(spots):
for
s
in
spots:
roted
=
pygame.transform.rotate(letrs[s.leti],s.ang)
screen.blit(roted,(s.x,s.y))
def
setspotangs(spots):
for
i
in
range
(
len
(spots)
-
1
):
x1
=
spots[i].x
y1
=
spots[i].y
x2
=
spots[i
+
1
].x
y2
=
spots[i
+
1
].y
spots[i].ang
=
math.atan2(y2
-
y1,x2
-
x1)
*
-
180.0
/
math.pi
x1
=
spots[
-
1
].x
y1
=
spots[
-
1
].y
x2
=
spots[
-
2
].x
y2
=
spots[
-
2
].y
spots[
-
1
].ang
=
math.atan2(y2
-
y1,x2
-
x1)
*
-
180.0
/
math.pi
letrs
=
[]
for
i
in
range
(
6
):
s
=
pygame.Surface((
12
,
12
))
s.set_colorkey((
1
,
1
,
1
))
s.fill((random.randint(
0
,
255
),random.randint(
0
,
255
),random.randint(
0
,
255
)))
letrs.append(s)
sw
=
512
sh
=
512
screen
=
pygame.display.set_mode((sw,sh))
ang
=
0
angstep
=
8
rot
=
20
rotstep
=
1
rotmax
=
40
rotmin
=
10
rad
=
32
radstep
=
8
radmax
=
64
radmin
=
16
wordreps
=
6
w
=
512
h
=
512
frames
=
500
anim1
=
makesnakeanim(ang,angstep,rot,rotstep,rotmax,rotmin,rad,radstep,radmax,radmin,wordreps,w,h,frames)
cc
=
0
running
=
True
while
running:
for
e
in
pygame.event.get():
if
e.
type
=
=
pygame.QUIT:
running
=
False
screen.fill((
255
,
255
,
255
))
drawsnake(anim1[cc])
pygame.display.flip()
time.sleep(
0.1
)
cc
=
(cc
+
1
)
%
len
(anim1)
pygame.quit()