Time for action – animating objects with NumPy and Pygame
We will load an image and use NumPy again to define a clockwise path around the screen.
- Create a Pygame clock as follows:
clock = pygame.time.Clock()
- As part of the source code accompanying this book, there should be a picture of a head. Load this image and move it around on the screen:
img = pygame.image.load('head.jpg')
- Define some arrays to hold the coordinates of the positions, where we would like to put the image during the animation. Since we will move the object, there are four logical sections of the path:
right
,down
,left
, andup
. Each of these sections will have40
equidistant steps. Initialize all the values in the sections to0
:steps = np.linspace(20, 360, 40).astype(int) right = np.zeros((2, len(steps))) down = np.zeros((2, len(steps))) left = np.zeros((2, len(steps))) up = np.zeros((2, len(steps)))
- It's straight-forward to set the coordinates of the positions of the image. However, there is one tricky...