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. Perform the following steps to do so:
We can 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. We will load this image and move it around on the screen.
img = pygame.image.load('head.jpg')
We will define some arrays to hold the coordinates of the positions where we would like to put the image during the animation. Since the object will be moved, there are four logical sections of the path – right, down, left, and up. Each of these sections will have 40 equidistant steps. We will initialize all the values in these sections to 0.
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 trivial to set the coordinates of the positions...