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:

  1. We can create a Pygame clock, as follows:
    clock = pygame.time.Clock()
  2. 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')
  3. 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)))
  4. It's trivial to set the coordinates of the positions of the image. However, there is one tricky bit to be aware of – the [::-1] notation leads to reversing the order of the array elements.
    right[0] = steps
    right[1] = 20
    
    down[0] = 360
    down[1] = steps
    
    left[0] = steps[::-1]
    left[1] = 360
    
    up[0] = 20
    up[1] = steps[::-1]
  5. The path sections can be joined, but before we can do this, the arrays have to be transposed with the T operator, because they are not aligned properly for concatenation.
    pos = np.concatenate((right.T, down.T, left.T, up.T))
  6. In the main event loop we will set the clock tick at a rate of 30 frames per second:
        clock.tick(30)

    The following is a screenshot of the moving head:

    Time for action – animating objects with NumPy and Pygame

    You should be able to watch a movie of this animation at https://www.youtube.com/watch?v=m2TagGiq1fs.

    The code of this example uses almost everything we learned so far, but should still be simple enough to understand:

    import pygame, sys
    from pygame.locals import *
    import numpy as np
    
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((400, 400))
    
    pygame.display.set_caption('Animating Objects')
    img = pygame.image.load('head.jpg')
    
    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)))
    
    right[0] = steps
    right[1] = 20
    
    down[0] = 360
    down[1] = steps
    
    left[0] = steps[::-1]
    left[1] = 360
    
    up[0] = 20
    up[1] = steps[::-1]
    
    pos = np.concatenate((right.T, down.T, left.T, up.T))
    i = 0
    
    while True: 
        # Erase screen
        screen.fill((255, 255, 255))
    
        if i >= len(pos):
          i = 0
    
        screen.blit(img, pos[i])
        i += 1
    
        for event in pygame.event.get():
          if event.type == QUIT:
             pygame.quit()
             sys.exit()
    
        pygame.display.update()
        clock.tick(30)

What just happened?

We learned a bit about animation in this tutorial. The most important concept we learned about, is about the clock. The new functions that we used are described in the following table:

Function

Description

pygame.time.Clock()

This creates a game clock.

clock.tick(30)

This executes a "tick" of the game clock. Here 30 is the number of frames per second.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset