# explosion template - base class for explosions import random import pygame from pygame.locals import * import game, gfx from basespriteobj import Sprite class ExplosionObj(Sprite): def __init__(self,name,images,x,y,dx,dy): """create a new explosion with initial x,y,dx,dy values note that images must be an array of images (used to animate the explosion)""" Sprite.__init__(self) self.name = name self.images = images self.dead = 0 self.imagenum = 0 self.maximage = len(self.images) -1 self.rect = self.images[0].get_rect() self.lastrect = self.rect self.x = x - self.rect.width / 2 self.y = y - self.rect.height / 2 self.dx = dx self.dy = dy self.rect.topleft = self.x,self.y # how many frames we wait until dying self.timer = game.explosion_time self.interval = int(game.explosion_time/self.maximage) # override draw since we have multiple images def draw(self, gfx): r = gfx.surface.blit(self.images[self.imagenum], self.rect) gfx.dirty2(r, self.lastrect) self.lastrect = r def tick(self, speedadjust): self.physics() self.x += self.dx * speedadjust self.y += self.dy * speedadjust if self.x < game.arena.left - self.rect.width: self.dead = 1 elif self.x > game.arena.right: self.dead = 1 if self.y < game.arena.top - self.rect.height: self.dead = 1 elif self.y > game.groundarena.bottom - self.rect.height + 1: self.y = game.groundarena.top - self.rect.height + 1 self.dy = 0 self.timer += 1 if self.timer % self.interval == 0: self.imagenum += 1 if self.imagenum > self.maximage: self.imagenum = self.maximage self.dead = 1 self.rect.topleft = [self.x,self.y] def collide(self,rect): cr = self.rect.inflate(-15,-15) if cr.colliderect(rect): return 1 else: return 0 def physics(self): self.dx = ((self.dx + 1) * game.explosion_friction) - 1 self.dy *= game.explosion_friction