2cbe4cc84f45470d30491d8bff3078a72f59d4fd
1 # -*- coding: utf-8 -*-
13 class WarpingCursor(pygame
.sprite
.Sprite
):
15 The class for animating the warping cursor
18 The duration of each image in the animation
20 The Position of the center of the cursor
22 A pointer to the current image
24 The time elapsed since when the current image should have been displayed
29 #centerPosition = None
31 #_animationOffset = None
34 def _get_theme_images(name
) :
35 basePath
= os
.path
.abspath(__file__
).split(os
.path
.sep
)[:-1]
36 basePath
.append('data')
38 basePath
= os
.path
.sep
.join(basePath
)
39 images
= [f
for f
in os
.listdir(basePath
) if os
.path
.splitext(f
)[1] == '.png']
40 return basePath
, images
43 def __init__(self
, theme
='black', duration
=75):
44 pygame
.sprite
.Sprite
.__init
__(self
)
45 imagesPath
, images
= WarpingCursor
._get
_theme
_images
(theme
)
46 flashImage
= images
.pop(images
.index('flash.png'))
47 images
.sort(lambda a
, b
: cmp(*[int(os
.path
.splitext(f
)[0]) for f
in [a
, b
]]))
51 imagePath
= os
.path
.sep
.join([imagesPath
, img
])
52 self
.images
= pygame
.image
.load(imagePath
).convert_alpha()
54 #self.flashImagePath = flashImage
55 #self.durations = durations
56 #self.centerPosition = initCenterPosition
57 #self.flashLength = 100
58 #self.flashing = False
59 #self.image = pygame.image.load(self.images[0]).convert_alpha()
60 #self._imagePointer = 0
61 #self._animationOffset = 0
64 def update(self
, elapsedTime
, centerPosition
):
66 Update the cursor's look and position
69 The time passed since the previous update
71 the new position of the creep
73 self
._updateImage
(elapsedTime
)
74 self
.centerPosition
= centerPosition
76 self
._flashTimer
+= elapsedTime
77 if self
._flashTimer
> self
.flashLength
:
80 def _updateImage(self
, elapsedTime
):
82 Update the cursor's image
85 The time passed since the previous update
87 self
._animationOffset
+= elapsedTime
89 if self
._animationOffset
> self
.durations
[self
._imagePointer
]:
90 #New animation offset is computed first, before updating the pointer
91 self
._animationOffset
-= self
.durations
[self
._imagePointer
]
92 #point to the next image (restarts from the beginning when it reaches the end)
93 self
._imagePointer
= (self
._imagePointer
+ 1) % len(self
.images
)
96 self
.image
= pygame
.image
.load(self
.flashImagePath
).convert_alpha()
98 self
.image
= pygame
.image
.load(self
.images
[self
._imagePointer
]).convert_alpha()
100 def flash(self
,flashLength
= None):
104 self
.flashlength
= flashLength
106 def blit(self
,surface
):
108 Draw the circle on surface
111 newPos
= (self
.centerPosition
[0] - self
.image
.get_width() / 2, self
.centerPosition
[1] - self
.image
.get_height() / 2)
112 surface
.blit(self
.image
, newPos
)