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

The tower defense actors


We will define several classes in the actors.py module to represent the game objects.

As with our previous game, we will include a base class, from which the rest of the actor classes will inherit:

import cocos.sprite
import cocos.euclid as eu
import cocos.collision_model as cm

class Actor(cocos.sprite.Sprite):
    def __init__(self, img, x, y):
        super(Actor, self).__init__(img, position=(x, y))
        self._cshape = cm.CircleShape(self.position,
                                      self.width * 0.5)

    @property
    def cshape(self):
        self._cshape.center = eu.Vector2(self.x, self.y)
        return self._cshape

In the Actor implementation in the previous chapter, when the sprite was displaced by calling the move() method, both cshape and position were updated at the same time. However, actions such as MoveBy only modify the sprite position, and the CShape center is not updated.

To solve this issue, we wrap the access to the CShape member through the...