Book Image

Python Game Programming By Example

Book Image

Python Game Programming By Example

Overview of this book

Table of Contents (14 chapters)
Python Game Programming By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Basic collision detection game


With all of these ingredients, you are now able to write a simple game that detects simple collisions between shapes.

The scene consists of an infinite lane. Blocks appear randomly at the end of the lane and move towards the player, represented as the sphere in the following screenshot. He or she must avoid hitting the blocks by moving the sphere from right to left in the horizontal axis.

The game is over when the player's character collides with one of the blocks.

This gameplay is direct and uncomplicated, and it will allow us to develop a 3D game without worrying too much about more complicated physics calculations.

Since we are going to reuse the Light, Cube, and Sphere classes, we need to define a new class only to represent our game blocks:

class Block(Cube):
    color = (0, 0, 1, 1)
    speed = 0.01

    def __init__(self, position, size):
        super().__init__(position, (size, 1, 1), Block.color)
        self.size = size

    def update(self, dt):
   ...