-'''\r
-Created on 29 mai 2009\r
-\r
-@author: samsam\r
-'''\r
-\r
-import os, sys, math\r
-\r
-import pygame\r
-from pygame.sprite import Sprite\r
-\r
-class WarpingCursor(Sprite):\r
- '''\r
- The class for animating the warping cursor\r
- \r
- screen:\r
- the screen on which the WarpingCursor is painted\r
- images:\r
- The images constituting the animation of the cursor\r
- durations:\r
- The duration of each image in the animation\r
- centerPosition:\r
- The Position of the center of the cursor\r
- _imagePointer:\r
- A pointer to the current image\r
- _animationOffset:\r
- The time elapsed since when the current image should have been displayed\r
- '''\r
- screen = None\r
- images = None\r
- durations = None\r
- centerPosition = None\r
- _imagePointer = None\r
- _animationOffset = None\r
- \r
-\r
- def __init__(self, scr, images, durations, initCenterPosition,flashImage):\r
- '''\r
- Constructor\r
- \r
- scr:\r
- the screen on which the WarpingCursor is painted\r
- images:\r
- The images constituting the animation of the cursor\r
- durations:\r
- The duration of each image in the animation\r
- initCenterPosition:\r
- The Position of the center of the cursor at the beginning \r
- '''\r
- self.screen = scr\r
- self.images = images\r
- self.flashImagePath = flashImage\r
- self.durations = durations\r
- self.centerPosition = initCenterPosition\r
- self.flashLength = 100\r
- self.flashing = False\r
- self.image = pygame.image.load(self.images[0]).convert_alpha()\r
- self._imagePointer = 0\r
- self._animationOffset = 0\r
- self._flashTimer = 0 \r
- \r
- def update(self, elapsedTime, centerPosition):\r
- '''\r
- Update the cursor's look and position\r
- \r
- elapsedTime:\r
- The time passed since the previous update\r
- centerPosition:\r
- the new position of the creep\r
- '''\r
- self._updateImage(elapsedTime)\r
- self.centerPosition = centerPosition\r
- if self.flashing :\r
- self._flashTimer += elapsedTime\r
- if self._flashTimer > self.flashLength:\r
- self.flashing = False\r
- \r
- def _updateImage(self, elapsedTime):\r
- '''\r
- Update the cursor's image\r
- \r
- elapsedTime:\r
- The time passed since the previous update\r
- '''\r
- self._animationOffset += elapsedTime\r
- \r
- if self._animationOffset > self.durations[self._imagePointer]:\r
- #New animation offset is computed first, before updating the pointer\r
- self._animationOffset -= self.durations[self._imagePointer]\r
- #point to the next image (restarts from the beginning when it reaches the end)\r
- self._imagePointer = (self._imagePointer + 1) % len(self.images)\r
- \r
- if self.flashing:\r
- self.image = pygame.image.load(self.flashImagePath).convert_alpha() \r
- else : \r
- self.image = pygame.image.load(self.images[self._imagePointer]).convert_alpha()\r
- \r
- def flash(self,flashLength = None):\r
- self._flashTimer = 0\r
- self.flashing = True\r
- if flashLength:\r
- self.flashlength = flashLength\r
- \r
- def blit(self,surface):\r
- '''\r
- Draw the circle on surface\r
- '''\r
- \r
- newPos = (self.centerPosition[0] - self.image.get_width() / 2, self.centerPosition[1] - self.image.get_height() / 2)\r
- surface.blit(self.image, newPos)\r
-\r
-def createImageListFromPath(path, imageCount):\r
- '''\r
- Create a list of images for a cursor (the concatenation of the original and reversed lists of images).\r
- Images must be stored as path/imageNumber.png\r
- \r
- path:\r
- The folder where the images for that cursor are stored\r
- imageCount:\r
- The number of images in the folder\r
- '''\r
- \r
- tempImages = [''.join([path, '/', str(i), '.png']) for i in range(imageCount)]\r
- #tempImagesReversed = tempImages[:]\r
- #tempImagesReversed.reverse()\r
- #return(tempImages+tempImagesReversed)\r
- return(tempImages) \r
-\r
-#testing\r
-if __name__ == "__main__" :\r
- window = pygame.display.set_mode((1680, 1050), pygame.FULLSCREEN)\r
- screen = pygame.display.get_surface()\r
- clock = pygame.time.Clock()\r
- \r
- images = createImageListFromPath('cursorImages/black',11)\r
- durations = [50 for i in range(22)]\r
- position = (400, 300)\r
- cursor = WarpingCursor(screen, images, durations, position)\r
- \r
- while True:\r
- # Limit frame speed to 50 FPS\r
- #\r
- timePassed = clock.tick(50)\r
- \r
- for event in pygame.event.get():\r
- if event.type == pygame.QUIT:\r
- sys.exit()\r
- if event.type == pygame.MOUSEMOTION:\r
- position = event.pos\r
- \r
- cursor.update(timePassed, position)\r
- cursor.blit(screen)\r
- pygame.display.flip()\r
- \r
- \r