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

Shoot'em up!


The Shoot actor, by itself, is quite basic; it only requires a speed attribute and overrides the update method so that the object is moved by the distance determined by this speed and the elapsed time between frames:

class Shoot(Actor):
    def __init__(self, x, y, img='img/shoot.png'):
        super(Shoot, self).__init__(img, x, y)
        self.speed = eu.Vector2(0, -400)

    def update(self, elapsed):
        self.move(self.speed * elapsed)

The PlayerShoot class requires a bit more logic, since the player cannot shoot until the previous beam has hit an enemy or reached the end of the screen.

As we want to avoid global variables, we will use a class attribute to hold the reference to the current shot. When the shot leaves the scene, this reference will be set to None again.

We will override the collide method from the Actor class so that both the beam and the alien are destroyed when a collision occurs. This is done by calling the kill method defined in the Sprite class. It internally...