6d81a67aed710406bb74b6f9aad9811057d2687a
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 self
.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 img
= pygame
.image
.load(imagePath
).convert_alpha()
53 self
.images
.append(img
)
55 self
._imageLength
= len(self
.images
)
56 # assumes that all images have same dimensions
57 self
.width
= self
.images
[0].get_width()
58 self
.height
= self
.images
[0].get_height()
59 self
.duration
= duration
61 self
.image
= self
.images
[0]
62 self
.rect
= pygame
.Rect((0,0), (self
.width
, self
.height
))
64 surface
= pygame
.display
.get_surface()
65 surface
.blit(self
.image
, self
.rect
)
67 #self.flashImagePath = flashImage
68 #self.durations = durations
69 #self.centerPosition = initCenterPosition
70 #self.flashLength = 100
71 #self.flashing = False
72 #self.image = pygame.image.load(self.images[0]).convert_alpha()
73 #self._imagePointer = 0
74 #self._animationOffset = 0
80 # def update(self, elapsedTime, centerPosition):
82 # Update the cursor's look and position
85 # The time passed since the previous update
87 # the new position of the creep
89 # self._updateImage(elapsedTime)
90 # self.centerPosition = centerPosition
92 # self._flashTimer += elapsedTime
93 # if self._flashTimer > self.flashLength:
94 # self.flashing = False
96 def _updateImage(self
, elapsedTime
):
98 Update the cursor's image
101 The time passed since the previous update
103 self
._animationOffset
+= elapsedTime
105 if self
._animationOffset
> self
.duration
:
106 #New animation offset is computed first, before updating the pointer
107 self
._animationOffset
-= self
.duration
108 #point to the next image (restarts from the beginning when it reaches the end)
109 self
._imagePointer
= (self
._imagePointer
+ 1) % len(self
.images
)
112 self
.image
= pygame
.image
.load(self
.flashImagePath
).convert_alpha()
114 self
.image
= pygame
.image
.load(self
.images
[self
._imagePointer
]).convert_alpha()
116 def flash(self
,flashLength
= None):
120 self
.flashlength
= flashLength
122 def blit(self
,surface
):
124 Draw the circle on surface
127 newPos
= (self
.centerPosition
[0] - self
.image
.get_width() / 2, self
.centerPosition
[1] - self
.image
.get_height() / 2)
128 surface
.blit(self
.image
, newPos
)