Book Image

Instant Pygame for Python Game Development How-to

By : Ivan Idris
Book Image

Instant Pygame for Python Game Development How-to

By: Ivan Idris

Overview of this book

<p>Pygame is a library created to make multimedia software documenting easy to design. It adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the Python language. Pygame comes with functions and tools that will help you create a great user experience."Instant Pygame for Python Game Development How-to" is written in a concise and result-oriented format. Leading you through practical recipes, you'll find that this essential reference guide helps you to create visually appealing and thrilling games with a few clicks.This book starts with the basic instructions to install Pygame on different servers. It then goes into creating a sample game and explaining the features of drawing, animating, using fonts and Matplotlib with Pygame. The book then takes you through recipes to get access to some great sound and graphic effects. Giving you the steps to allow you to configure these games on Android and other networks, it ends with a walkthrough of the features of Sprites, OpenGL, and Simulation.</p>
Table of Contents (7 chapters)

Accessing surface pixel data (Intermediate)


The Pygame surfarray module handles the conversion between Pygame Surface objects and NumPy arrays. As you may recall, NumPy can manipulate big arrays in a fast and efficient manner.

How to do it...

In this recipe we will tile a small image to fill the game screen.

  1. Copying pixels to array: The array2d function copies pixels into a two-dimensional array. There is a similar function for three-dimensional arrays. We will copy the pixels from the avatar image into an array:

    pixels = pygame.surfarray.array2d(img)
  2. Creating the game screen: A NumPy array has a shape attribute that corresponds to the dimensions of the array. This attribute is a tuple. A two-dimensional array for instance, will have a two-element shape tuple. Let's create the game screen from the shape of the pixels array using the shape attribute of the array. The screen will be seven times larger in both directions:

    X = pixels.shape[0] * 7
    Y = pixels.shape[1] * 7
    screen = pygame.display.set_mode((X, Y))
  3. Tiling the image: Tiling the image is easy with the NumPy tile function. The data needs to be converted to integer values, since colors are defined as integers:

    new_pixels = numpy.tile(pixels, (7, 7)).astype(int)
  4. Displaying the array: The surfarray module has the following special function (blit_array) to display the array on the screen:

    pygame.surfarray.blit_array(screen, new_pixels)

    The following screenshot displays the result of the code:

    The following code does the tiling of the image:

    import pygame, sys
    from pygame.locals import *
    import numpy
    
    pygame.init()
    img = pygame.image.load('head.jpg')
    pixels = pygame.surfarray.array2d(img)
    X = pixels.shape[0] * 7
    Y = pixels.shape[1] * 7
    screen = pygame.display.set_mode((X, Y))
    pygame.display.set_caption('Surfarray Demo')
    new_pixels = numpy.tile(pixels, (7, 7)).astype(int)
    
    
    while True: 
       screen.fill((255, 255, 255))
       pygame.surfarray.blit_array(screen, new_pixels)
    
       for event in pygame.event.get():
          if event.type == QUIT:
             pygame.quit()
             sys.exit()
    
       pygame.display.update()

How it works...

The following table gives us a brief description of the new functions and attributes we used:

Function

Description

pygame.surfarray.array2d(img)

This copies pixel data into a 2D array

pixels.shape[0]

The shape attribute holds the dimensions of a NumPy array as a tuple

numpy.tile(pixels, (7, 7))

This tiles an array the given dimensions specified as a tuple

pygame.surfarray.blit_array(screen, new_pixels)

This displays array values on the screen