O'Reilly logo

Python Game Programming By Example by Joseph Howse, Alejandro Rodas de Paz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required