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)

Preface

Pygame is a fun Python API with which we can easily create simple games. It has support for drawing, sound, images, animation, OpenGL, and more. We will explore many aspects of Pygame with more than 20 recipes in this book. The book is self-contained and only assumes that you have basic knowledge of Python programming. You are encouraged to try out all the example games and modify them to your own needs.

What this book covers

Preparing your development environment (Simple) is a basic recipe that will help you with the installation of all the necessary software required in your development environment.

Running a simple game (Simple) is where we will create a basic game to get us started. The game demonstrates fonts and screen management in the time-honored tradition of Hello world examples.

Drawing with Pygame (Simple) teaches us how to draw basic shapes such as rectangles, ovals, circles, lines, and others. We will also learn important information about colors and color management.

Animating objects (Simple) starts with the "Hit the avatar!" running game example of this book. We will learn how to animate objects in our funny little game.

Using fonts(Simple) is about fonts and font management.

Using Matplotlib with Pygame (Simple) lets us plot a graph within Pygame with the amazing open source Python plotting library Matplotlib. Matplotlib is highly versatile and offers a ton of features for plotting and visualization.

Accessing surface pixel data (Intermediate) shows us how to manipulate pixel data stored in special arrays for efficient drawing. The efficient NumPy open source Python mathematical library is introduced in this recipe.

Accessing sound data (Simple) has us process audio data as arrays. This recipe requires you to listen well while running the example code for this recipe.

Playing a movie (Intermediate) guides us through the steps required to play a movie. The value an in-game movie will add to your game is just priceless.

Pygame on Android (Intermediate) introduces us to the wonderful world of Android. Android is a well-known open source mobile computing framework originally created by Google. We create an example Android game in the process.

Artificial intelligence (Intermediate) is a hot topic these days. We dive right in with the popular Scikits-learn open source Python framework. Of course, this is a huge topic that could require years to master. We expose the tip of the iceberg and give clustering a go.

Drawing sprites (Intermediate) talks us through sprite management. Sprites is a term from computer graphics denoting two-dimensional visible objects that we can manipulate on the screen. Sprites can be grouped together for easier management.

Using OpenGL with Pygame (Advanced) helps us get a hold on OpenGL, a famous open source graphics framework that is available on a variety of platforms and programming languages. OpenGL is used in the industry to render complex two-dimensional and three-dimensional objects.

Detecting collisions (Intermediate) is essential for good game development, whether we are crashing into a car, deploying air-to-air missiles, or playing football. We will give tips on how to detect collisions with ease.

Adding networking functionality (Advanced) runs us through a rudimentary client-server setup. We will use the brilliant open source Python Twisted framework to create a networked game. The game requires us to guess a word that is only known at the server side.

Debugging your game (Intermediate) gives you the life-saving debugging techniques you will need to create a robust working game. Debugging is stressful, so it helps when you have reliable tools. We will introduce such a tool.

Profiling your code (Intermediate) is something you should do to ensure that your game performs well. Tips and ideas are given in this recipe to facilitate the profiling process.

Puzzle game with Pygame (Advanced) showcases an interactive client-server game, building on all the previously learned material.

Simulating with Pygame (Advanced) simulates life in a simplistic yet fun manner.

What you need for this book

This book is pretty self-contained. Instructions to install required software are given throughout the book.

Who this book is for

This book is for Pythonistas who are interested in learning how to create games with all the accompanying bells and whistles. Even if you don't know Python that well, the book should be easy to follow.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: " We can sort with the following sort command."

A block of code is set as follows:

import os, pygame
from pygame.locals import *
import numpy
from scipy import ndimage

def get_pixar(arr, weights):
  states = ndimage.convolve(arr, weights, mode='wrap')

  bools = (states == 13) | (states == 12 ) | (states == 3)

  return bools.astype(int)

Any command-line input or output is written as follows:

ffmpeg -i <infile> -vcodec mpeg1video -acodec libmp3lame -intra <outfile.mpg>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "you should see Hello being displayed followed by 1 IMPORTANT MESSAGE! to 19 IMPORTANT MESSAGE!".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.