So I'm trying to code a game using the Model-View-Controller pattern, and therefore have a need to separate the models for my game objects with their sprite representations. The problem that I'm running into is with PyGame's sprite class. For example, let's take the code below for a Tank object in my game.
class Tank(Projectile): '''A tank object.''' MAX_SPEED = 8.0 @property def MAX_TURN(self): return 10.0 - 0.75 * abs(self.velocity) def __init__(self, position=(0, 0), heading=0.0, power=100.0, name=None): Projectile.__init__(self, position, heading) self.gun = Gun(self) self.radar = Radar(self) self.power = power self.name = name def update(self, dt): super(Tank, self).update(dt) #Just writing this here for illustrative purposes
Additionally, in a separate views file, I have the code for the basis of a sprite:
class TankSprite(RotatableSprite): _image = pygame.image.load('images/tank/body.png').convert_alpha() _layer = 0 TankSprite.groups = tank_group, all_group
Now the standard solution might just be to merge these two into one, and then use pygame's pygame.sprite.group.update() method to update and draw everything as desired. But with a MVC pattern, I run into a few problems:
- First, there is no way of knowing when a new object is created. For example, when the Tank fires its Gun, its creates a Bullet object. Since the Bullet is a model, there is no way to detect this creation without some awkward View code in the Models.
- Second, I don't believe I can use pygame's collision methods or Group objects, which isn't the worst, but somewhat awkward.
Really, the bullet problem is killing me more than anything else. Any help would be appreciated.