10 from pygame
.sprite
import Sprite
12 class WarpingCursor(Sprite
):
14 The class for animating the warping cursor
17 the screen on which the WarpingCursor is painted
19 The images constituting the animation of the cursor
21 The duration of each image in the animation
23 The Position of the center of the cursor
25 A pointer to the current image
27 The time elapsed since when the current image should have been displayed
34 _animationOffset
= None
37 def __init__(self
, scr
, images
, durations
, initCenterPosition
,flashImage
):
42 the screen on which the WarpingCursor is painted
44 The images constituting the animation of the cursor
46 The duration of each image in the animation
48 The Position of the center of the cursor at the beginning
52 self
.flashImagePath
= flashImage
53 self
.durations
= durations
54 self
.centerPosition
= initCenterPosition
55 self
.flashLength
= 100
57 self
.image
= pygame
.image
.load(self
.images
[0]).convert_alpha()
58 self
._imagePointer
= 0
59 self
._animationOffset
= 0
62 def update(self
, elapsedTime
, centerPosition
):
64 Update the cursor's look and position
67 The time passed since the previous update
69 the new position of the creep
71 self
._updateImage
(elapsedTime
)
72 self
.centerPosition
= centerPosition
74 self
._flashTimer
+= elapsedTime
75 if self
._flashTimer
> self
.flashLength
:
78 def _updateImage(self
, elapsedTime
):
80 Update the cursor's image
83 The time passed since the previous update
85 self
._animationOffset
+= elapsedTime
87 if self
._animationOffset
> self
.durations
[self
._imagePointer
]:
88 #New animation offset is computed first, before updating the pointer
89 self
._animationOffset
-= self
.durations
[self
._imagePointer
]
90 #point to the next image (restarts from the beginning when it reaches the end)
91 self
._imagePointer
= (self
._imagePointer
+ 1) % len(self
.images
)
94 self
.image
= pygame
.image
.load(self
.flashImagePath
).convert_alpha()
96 self
.image
= pygame
.image
.load(self
.images
[self
._imagePointer
]).convert_alpha()
98 def flash(self
,flashLength
= None):
102 self
.flashlength
= flashLength
104 def blit(self
,surface
):
106 Draw the circle on surface
109 newPos
= (self
.centerPosition
[0] - self
.image
.get_width() / 2, self
.centerPosition
[1] - self
.image
.get_height() / 2)
110 surface
.blit(self
.image
, newPos
)
112 def createImageListFromPath(path
, imageCount
):
114 Create a list of images for a cursor (the concatenation of the original and reversed lists of images).
115 Images must be stored as path/imageNumber.png
118 The folder where the images for that cursor are stored
120 The number of images in the folder
123 tempImages
= [''.join([path
, '/', str(i
), '.png']) for i
in range(imageCount
)]
124 #tempImagesReversed = tempImages[:]
125 #tempImagesReversed.reverse()
126 #return(tempImages+tempImagesReversed)
130 if __name__
== "__main__" :
131 window
= pygame
.display
.set_mode((1680, 1050), pygame
.FULLSCREEN
)
132 screen
= pygame
.display
.get_surface()
133 clock
= pygame
.time
.Clock()
135 images
= createImageListFromPath('cursorImages/black',11)
136 durations
= [50 for i
in range(22)]
137 position
= (400, 300)
138 cursor
= WarpingCursor(screen
, images
, durations
, position
)
141 # Limit frame speed to 50 FPS
143 timePassed
= clock
.tick(50)
145 for event
in pygame
.event
.get():
146 if event
.type == pygame
.QUIT
:
148 if event
.type == pygame
.MOUSEMOTION
:
151 cursor
.update(timePassed
, position
)
153 pygame
.display
.flip()