Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

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.

  1. 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. Load this image and move it around on the screen:

    img = pygame.image.load('head.jpg')
  3. 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, and up. Each of these sections will have 40 equidistant steps. Initialize all the values in the 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 straight-forward to set the coordinates of the positions of the image. However, there is one tricky bit to notice—the [...