Python Game Programming By Example

Book Description

A pragmatic guide for developing your own games with Python

About This Book

  • Strengthen your fundamentals of game programming with Python language
  • Seven hands-on games to create 2D and 3D games rapidly from scratch
  • Illustrative guide to explore the different GUI libraries for building your games

Who This Book Is For

If you have ever wanted to create casual games in Python and you would like to explore various GUI technologies that this language offers, this is the book for you. This title is intended for beginners to Python with little or no knowledge of game development, and it covers step by step how to build seven different games, from the well-known Space Invaders to a classical 3D platformer.

What You Will Learn

  • Take advantage of Python's clean syntax to build games quickly
  • Discover distinct frameworks for developing graphical applications
  • Implement non-player characters (NPCs) with autonomous and seemingly intelligent behaviors
  • Design and code some popular games like Pong and tower defense
  • Compose maps and levels for your sprite-based games in an easy manner
  • Modularize and apply object-oriented principles during the design of your games
  • Exploit libraries like Chimpunk2D, cocos2d, and Tkinter
  • Create natural user interfaces (NUIs), using a camera and computer vision algorithms to interpret the player's real-world actions

In Detail

With a growing interest in learning to program, game development is an appealing topic for getting started with coding. From geometry to basic Artificial Intelligence algorithms, there are plenty of concepts that can be applied in almost every game. Python is a widely used general-purpose, high-level programming language. It provides constructs intended to enable clear programs on both a small and large scale. It is the third most popular language whose grammatical syntax is not predominantly based on C. Python is also very easy to code and is also highly flexible, which is exactly what is required for game development. The user-friendliness of this language allows beginners to code games without too much effort or training. Python also works with very little code and in most cases uses the "use cases" approach, reserving lengthy explicit coding for outliers and exceptions, making game development an achievable feat.

Python Game Programming by Example enables readers to develop cool and popular games in Python without having in-depth programming knowledge of Python. The book includes seven hands-on projects developed with several well-known Python packages, as well as a comprehensive explanation about the theory and design of each game.

It will teach readers about the techniques of game design and coding of some popular games like Pong and tower defense. Thereafter, it will allow readers to add levels of complexities to make the games more fun and realistic using 3D.

At the end of the book, you will have added several GUI libraries like Chimpunk2D, cocos2d, and Tkinter in your tool belt, as well as a handful of recipes and algorithms for developing games with Python.

Style and approach

This book is an example-based guide that will teach you to build games using Python. This book follows a step-by-step approach as it is aimed at beginners who would like to get started with basic game development. By the end of this book you will be competent game developers with good knowledge of programming in Python.

Publisher Resources

Download Example Code

Table of Contents

  1. Python Game Programming By Example
    1. Table of Contents
    2. Python Game Programming By Example
    3. Credits
    4. About the Authors
    5. About the Reviewers
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Hello, Pong!
      1. Installing Python
      2. An overview of Breakout
      3. The basic GUI layout
      4. Diving into the Canvas widget
      5. Basic game objects
        1. The Ball class
        2. The Paddle class
        3. The Brick class
      6. Adding the Breakout items
      7. Movement and collisions
      8. Starting the game
      9. Playing Breakout
      10. Summary
    9. 2. Cocos Invaders
      1. Installing cocos2d
      2. Getting started with cocos2d
        1. Handling user input
        2. Updating the scene
        3. Processing collisions
      3. Creating game assets
      4. Space Invaders design
        1. The PlayerCannon and GameLayer classes
      5. Invaders!
      6. Shoot'em up!
        1. Adding an HUD
        2. Extra feature – the mystery ship
      7. Summary
    10. 3. Building a Tower Defense Game
      1. The tower defense gameplay
      2. Cocos2d actions
        1. Interval actions
        2. Instant actions
        3. Combining actions
        4. Custom actions
      3. Adding a main menu
      4. Tile maps
        1. Tiled Map Editor
        2. Loading tiles
      5. The scenario definition
        1. The scenario class
      6. Transitions between scenes
        1. Game over cut scene
      7. The tower defense actors
        1. Turrets and slots
        2. Enemies
        3. Bunker
      8. Game scene
        1. The HUD class
        2. Assembling the scene
      9. Summary
    11. 4. Steering Behaviors
      1. NumPy installation
        1. The ParticleSystem class
        2. A quick demonstration
      2. Implementing steering behaviors
        1. Seek and flee
        2. Arrival
        3. Pursuit and evade
        4. Wander
        5. Obstacle avoidance
      3. Gravitation game
        1. Basic game objects
        2. Planets and pickups
        3. Player and enemies
        4. Explosions
        5. The game layer
      4. Summary
    12. 5. Pygame and 3D
      1. Installing packages
      2. Getting started with OpenGL
        1. Initializing the window
        2. Drawing shapes
        3. Running the demo
        4. Refactoring our OpenGL program
        5. Processing the user input
      3. Adding the Pygame library
        1. Pygame 101
        2. Pygame integration
      4. Drawing with OpenGL
        1. The Cube class
        2. Enabling face culling
      5. Basic collision detection game
      6. Summary
    13. 6. PyPlatformer
      1. An introduction to game design
        1. Level design
        2. Platformer skills
        3. Component-based game engines
      2. Introducing Pymunk
      3. Building a game framework
        1. Adding physics
        2. Renderable components
          1. The Camera component
        3. The InputManager module
        4. The Game class
      4. Developing PyPlatformer
        1. Creating the platforms
        2. Adding pickups
        3. Shooting!
        4. The Player class and its components
        5. The PyPlatformer class
      5. Summary
    14. 7. Augmenting a Board Game with Computer Vision
      1. Planning the Checkers application
      2. Setting up OpenCV and other dependencies
        1. Windows
        2. Mac
        3. Debian and its derivatives, including Raspbian, Ubuntu, and Linux Mint
        4. Fedora and its derivatives, including RHEL and CentOS
        5. OpenSUSE and its derivatives
      3. Supporting multiple versions of OpenCV
      4. Configuring cameras
      5. Working with colors
      6. Building the analyzer
        1. Providing access to the images and classification results
        2. Providing access to parameters for the user to configure
        3. Initializing the entire model of the game
        4. Updating the entire model of the game
        5. Capturing and converting an image
        6. Detecting the board's corners and tracking their motion
        7. Creating and analyzing the bird's-eye view of the board
        8. Analyzing the dominant colors in a square
        9. Classifying the contents of a square
        10. Drawing text
      7. Converting OpenCV images for wxPython
      8. Building the GUI application
        1. Creating a window and binding events
        2. Creating and laying out images in the GUI
        3. Creating and laying out controls
        4. Nesting layouts and setting the root layout
        5. Starting a background thread
        6. Closing a window and stopping a background thread
        7. Configuring the analyzer based on user input
        8. Updating and showing images
        9. Running the application
      9. Troubleshooting the project in real-world conditions
      10. Further reading on OpenCV
      11. Summary
    15. Index

Product Information

  • Title: Python Game Programming By Example
  • Author(s): Alejandro Rodas de Paz, Joseph Howse
  • Release date: September 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785281532