# stars class import pygame from pygame.locals import * import game, gfx, math from random import randrange import gameinit # Stars MAXSTARS = 30 MINSTARS = 10 STARSIZE = [2,2] # pixels MINLIFE = 20 MAXLIFE = 50 STARCHANGE = 2 STARDELAY = 3 BLACK = [20, 20, 40] WHITE = [255, 255, 255] LIGHTGRAY = [180, 180, 180] DARKGRAY = [120, 120, 120] STARCOLOR = [DARKGRAY,LIGHTGRAY,WHITE] starimages = [] class Star: def __init__(self,activerect): global starimages val = randrange(1, 3) self.speed = -2, 0 x = randrange(activerect.left,activerect.right) y = randrange(activerect.top,activerect.bottom) self.image = starimages[val-1] self.rect = self.image.get_rect().move(x,y) self.lastrect = self.rect self.life = randrange(MINLIFE,MAXLIFE) class Stars: def __init__(self, activerect=None): self.counter = 0 # build star images global starimages starimages = [] for loop in range(0,3): color = STARCOLOR[loop] image = pygame.Surface(STARSIZE).convert(gfx.surface) image.fill(color) starimages.append(image) self.numstars = 20 self.stars = [] if activerect == None: # whole screen self.star_rect = gfx.rect else: self.star_rect = activerect for loop in range(self.numstars): self.stars.append(Star(self.star_rect)) self.laststars = self.stars def recalc_num_stars(self, fps): return if isinstance(game.handler, gameinit.GameInit): #don't change stars while loading resources return change = randrange(-STARCHANGE,STARCHANGE) if (change < 0): for star in self.stars[0:abs(change)]: star.life = 0 elif (change > 0): for loop in range(0,change): self.stars.append(Star(self.star_rect)) #print 'STAR:', len(self.stars), fps def erase_tick_draw(self, background, gfx): self.counter = (self.counter + 1) % STARDELAY if self.counter != 0: return # local variables for speed R, B = gfx.rect.bottomright BLIT,DIRTY = gfx.surface.blit, gfx.dirty # erase & check life, make list of new stars self.stars = [] stars_append = self.stars.append for star in self.laststars: DIRTY(background(star.lastrect)) star.life += -1 if star.life > 0: stars_append(star) # add stars if necessary if len(self.stars) < MINSTARS: for loop in range(0,MINSTARS-len(self.stars)): stars_append(Star(self.star_rect)) # tick, draw for star in self.stars[:self.numstars]: star.rect.left = (star.rect.left + star.speed[0]) % R star.rect.top = (star.rect.top + star.speed[1]) % B DIRTY(BLIT(star.image, star.rect)) star.lastrect = star.rect self.laststars = self.stars