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 sound data (Simple)


A good game needs to have great music and sound effects. The Pygame mixer module lets us play a sound or any audio for that matter.

How to do it...

We will download a WAV audio file using standard Python. We will play this sound when the game quits. This example requires you to actually execute the example code, because this book has no audio support.

  1. Creating a sound object: We can create a Pygame Sound object after specifying the name of the audio file. This class as you would expect embodies the concept of sounds:

    audio = pygame.mixer.Sound(WAV_FILE)
  2. Playing the sound: The Sound object has a play method, which has a number of loops parameters. If the value of this parameter is set to -1, the sound will loop indefinitely:

    audio.play(-1)
  3. Pausing the game: Sometimes we need to pause the execution of a game, as in our case in order to be able to hear a sound. We can do this with the following code snippet:

    pygame.time.delay(TIMEOUT * 1000)

    The delay is specified in milliseconds, that's why we are multiplying by 1000.

  4. Stopping the sound: After a while we need to stop the sound with the corresponding stop method:

    audio.stop()

    The audio demo code is listed as follows:

    import pygame, sys
    from pygame.locals import *
    import numpy
    import urllib2
    import time
    
    WAV_FILE = 'smashingbaby.wav'
    
    def play():
        audio = pygame.mixer.Sound(WAV_FILE)
        audio.play(-1)
        TIMEOUT = 1
        pygame.time.delay(TIMEOUT * 1000)
        audio.stop()
        time.sleep(TIMEOUT)
    
    pygame.init()
    pygame.display.set_caption('Sound Demo')
    response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav')
    filehandle = open(WAV_FILE, 'w')
    filehandle.write(response.read())
    filehandle.close()
    screen = pygame.display.set_mode((400, 400))
    
    while True: 
       sys_font = pygame.font.SysFont("None", 19)
       rendered = sys_font.render('Smashing Baby', 0, (255, 100, 100))
       screen.blit(rendered, (100, 100))
    
       for event in pygame.event.get():
          if event.type == QUIT:
             play()
             pygame.quit()
             sys.exit()
    
       pygame.display.update()

How it works...

The most important functions of this demo are summed up in the following table:

Function

Description

pygame.mixer.Sound(WAV_FILE)

This function creates a Sound object given a filename.

audio.play(-1)

This function plays and loops indefinitely (-1 means indefinitely). By default the sound is played only once. This corresponds with 0 loops. If the value is 2, the sound will be played once and then repeated 2 more times.

pygame.time.delay(TIMEOUT * 1000)

This function pauses the game for a specified number of milliseconds.

audio.stop()

This function stops audio playback.