#import required libraries import random, os import pygame #place pygame module in global namespace from pygame import * class Explosion(pygame.sprite.Sprite): """The Explosion Used When Blowing Up a Missle""" image = None #Again there will be multiple explosions so we only want to load image once def __init__(self, position): pygame.sprite.Sprite.__init__(self) #call Sprite initalizer if Explosion.image is None: #first explosion Explosion.image, self.rect = load_image('Explosion.png', (255, 255, 255, 0)) #Then load image self.image = Explosion.image #Set the image to gloabal image self.rect = self.image.get_rect() #Get the rectangle of the image self.rect.center = position #Set postion to given position self.life = 20 #Life span of how long explosion stays self.rotation = 0 self.scale = 0.97 self.center = self.rect.center def update(self): #Keeps explosion alive for so long if self.life == 0: # if its time to die pass #TODO: Kill explosion else: #self.life -= 1 #Minus one from its life self.image = Explosion.image self.rotation += 1 self.scale = 1./self.scale #self.image = transform.rotozoom(self.image, self.rotation, 1) #self.rect.center = self.center pass #Remove when it will dissapear #Function Used To Load an Image File def load_image(file_name, colorkey=None): #Grab the directory full_name = os.path.join('data', file_name) #try to load it try: image = pygame.image.load(full_name) except pygame.error, message: print 'Cannot load image:', full_name raise SystemExit, message #Should Convert But Causes Trouble With Alphas #image = image.convert() # Set the color key if colorkey is not None: if colorkey is -1: colorkey = image.get_at((0,0)) print colorkey image.set_colorkey(colorkey, RLEACCEL) #Return the image, and the images rectangle return image, image.get_rect() def main(): pygame.init() screen = pygame.display.set_mode((width,height)) pygame.display.set_caption('Meteor Shower by Corey Ling') #pygame.mouse.set_visible(False) //Possible Use Later running = 1 missleexplosion_sprite = pygame.sprite.RenderClear() #Explosion Group missleexplosion_sprite.add(Explosion((100,100)) #Create the explosion while running == 1: missleexplosion_sprite.update() missleexplosion_sprite.clear(screen,surface.fill(0,0,0)) missleexplosion_sprite.draw(screen) return if __name__ == '__main__': main()