def get_colored_objects(frames, has_alpha=True, color_blind=False, pause_frame=True): ''' @param frames: List of sprites we want to create colored versions of @param has_alpha: True if we want transparency @param color_blind: True if we want colorblind-friendly frames @param pause_frame: True if we want these to be all white @rtype: dict of {color: [frames_list]} ''' colored = {id(c):tuple(blend_color(config.get_sprite(f), c) for f in frames) for c in LIST} if pause_frame: #If we're pausing the game... colored[id(WHITE)] = tuple(blend_color(config.get_sprite(f), WHITE) for f in frames) if has_alpha: #If we want these frames to have transparency... for j in chain.from_iterable(colored.values()): j.set_colorkey(j.get_at((0, 0)), config.BLIT_FLAGS) if color_blind: #If we want a colorblind-friendly version of the sprite... for c in LIST: #For each possible game object color... for i in colored[id(c)]: #For each colored frame of this particular color... i.blit(COLOR_BLIND_SYMBOLS[id(c)], (0, 0)) return colored
class FlameTrail(GameObject): ''' FlameTrail is the jet left by the Ship's engines. This is purely a graphical effect. ''' FRAMES = tuple( config.get_sprite(Rect(32 * i, 0, 32, 32)) for i in range(6)) GROUP = None def __init__(self): super().__init__() self.anim = 0.0 self.image = FlameTrail.FRAMES[0] self.position = [-300.0, -300.0] self.rect = Rect(self.position, self.image.get_size()) self.state = 1 del self.acceleration, self.velocity for i in self.__class__.FRAMES: i.set_colorkey(color.COLOR_KEY, config.BLIT_FLAGS) def animate(self): self.anim += 1 / 3 self.image = FlameTrail.FRAMES[int(3 * sin(self.anim / 2)) + 3] actions = {1: 'animate'}
def __init__(self): super().__init__() self._anim = 0.0 self.column = None self.current_frame_list = UFO_FRAMES self.image = config.get_sprite(FRAMES[0]) self.odds = expovariate(AVG_WAIT) self.position = list(START_POS) self.rect = Rect(START_POS, self.image.get_size()) self.state = UFO.STATES.IDLE self.emitter = ParticleEmitter(color.random_color_particles, self.rect) del self.acceleration
def get_colored_objects(frames, has_alpha=True, color_blind=False, pause_frame=True): ''' @param frames: List of sprites we want to create colored versions of @param has_alpha: True if we want transparency @param color_blind: True if we want colorblind-friendly frames @param pause_frame: True if we want these to be all white @rtype: dict of {color: [frames_list]} ''' colored = { id(c): tuple(blend_color(config.get_sprite(f), c) for f in frames) for c in LIST } if pause_frame: #If we're pausing the game... colored[id(WHITE)] = tuple( blend_color(config.get_sprite(f), WHITE) for f in frames) if has_alpha: #If we want these frames to have transparency... for j in chain.from_iterable(colored.values()): j.set_colorkey(j.get_at((0, 0)), config.BLIT_FLAGS) if color_blind: #If we want a colorblind-friendly version of the sprite... for c in LIST: #For each possible game object color... for i in colored[id(c)]: #For each colored frame of this particular color... i.blit(COLOR_BLIND_SYMBOLS[id(c)], (0, 0)) return colored
class Bullet(GameObject): FRAME = Rect(227, 6, 26, 19) STATES = config.Enum(*BULLET_STATES) SPRITE = config.get_sprite(FRAME) def __init__(self): super().__init__() self.image = self.__class__.SPRITE #@UndefinedVariable self.rect = self.__class__.START_POS.copy() self.position = list(self.rect.topleft) self.state = self.__class__.STATES.IDLE self.image.set_colorkey(color.COLOR_KEY, config.BLIT_FLAGS) def start_moving(self): ''' Begins moving. ''' self.position = list(self.rect.topleft) self.velocity[1] = self.__class__.SPEED self.change_state(self.__class__.STATES.MOVING) def move(self): ''' Bullets only move vertically in Invasodado. ''' self.position[1] += self.velocity[1] self.rect.top = self.position[1] + .5 def reset(self): ''' Resets the bullet back to its initial position. ''' self.kill() self.velocity[1] = 0 self.rect = self.__class__.START_POS.copy() self.position = list(self.rect.topleft) self.change_state(self.__class__.STATES.IDLE) actions = { STATES.IDLE: None, STATES.FIRED: 'start_moving', STATES.MOVING: 'move', STATES.RESET: 'reset', }
### Functions ################################################################## def _star_appear(self): ''' Stars appear on the left edge of the screen and travel right at one of five possible speeds. ''' self.velocity[0] = randint(1, 5) def _star_move(self): ''' And this is the part where they actually move. ''' self.position[0] += self.velocity[0] self.rect.left = self.position[0] ################################################################################ STARS_GROUP = pygame.sprite.RenderUpdates() _STAR_IMAGE = config.get_sprite(Rect(4, 170, 2, 2)) EARTH = HudObject(config.EARTH, (0, 0)) GRID = HudObject(config.GRID_BG, (0, 0)) STARS = ParticleEmitter(ParticlePool(_STAR_IMAGE, _star_move, _star_appear), Rect(0, 0, 0, config.SCREEN_HEIGHT), 8, STARS_GROUP) EARTH.rect.midbottom = config.SCREEN_RECT.midbottom
whatever's in the bottom-left corner. @var COLOR_BLIND_FRAMES: The shapes that are placed over game objects in colorblind mode. @var COLOR_BLIND_SYMBOLS: Mapping of LIST plus WHITE to COLOR_BLIND_FRAMES ''' RED = Color('#FF3333') GREEN = Color('#4FFF55') BLUE = Color('#585EFF') YELLOW = Color('#EBFF55') PURPLE = Color('#FF55D5') WHITE = Color('#FFFFFF') BLACK = Color('#000000') COLOR_BLIND_FRAMES = tuple(Rect(32 * i, 32, 32, 32) for i in range(4, 10)) COLOR_BLIND_SYMBOLS = { id(RED) : config.get_sprite(COLOR_BLIND_FRAMES[0]), id(BLUE) : config.get_sprite(COLOR_BLIND_FRAMES[1]), id(GREEN) : config.get_sprite(COLOR_BLIND_FRAMES[2]), id(YELLOW) : config.get_sprite(COLOR_BLIND_FRAMES[3]), id(PURPLE) : config.get_sprite(COLOR_BLIND_FRAMES[4]), id(WHITE) : None , } COLOR_KEY = config.SPRITES.get_at((0, config.SPRITES.get_height() - 1)) LIST = (RED, BLUE, GREEN, YELLOW, PURPLE) for i in LIST: COLOR_BLIND_SYMBOLS[id(i)].set_colorkey(COLOR_KEY) ################################################################################ ### Functions ################################################################## def blend_color(surface, color):
### Functions ################################################################## def _star_appear(self): ''' Stars appear on the left edge of the screen and travel right at one of five possible speeds. ''' self.velocity[0] = randint(1, 5) def _star_move(self): ''' And this is the part where they actually move. ''' self.position[0] += self.velocity[0] self.rect.left = self.position[0] ################################################################################ STARS_GROUP = pygame.sprite.RenderUpdates() _STAR_IMAGE = config.get_sprite(Rect(4, 170, 2, 2)) EARTH = HudObject(config.EARTH , (0, 0)) GRID = HudObject(config.GRID_BG, (0, 0)) STARS = ParticleEmitter( ParticlePool(_STAR_IMAGE, _star_move, _star_appear), Rect(0, 0, 0, config.SCREEN_HEIGHT), 8, STARS_GROUP ) EARTH.rect.midbottom = config.SCREEN_RECT.midbottom
#If we've reached our target location... self.change_state(Particle.STATES.LEAVING) else: dx = (percent**2) * (3-2*percent) ddx = 1 - dx position[0] = (self.startpos[0] * ddx) + (START_POS.centerx * dx) position[1] = (self.startpos[1] * ddx) + (START_POS.centery * dx) self.rect.topleft = (position[0] + .5, position[1] + .5) ################################################################################ ### Constants ################################################################## PART_IMAGE = Rect(4, 170, 4, 4) APPEAR = config.load_sound('appear.wav') APPEAR_POOL = ParticlePool(config.get_sprite(PART_IMAGE), _radius_move, _radius_appear) DEATH = config.load_sound('death.wav') DEATH_POOL = ParticlePool(config.get_sprite(PART_IMAGE), appear_func=_burst_appear) FRAMES = tuple(config.get_sprite(Rect(32 * i, 128, 32, 32)) for i in range(5)) GRAVITY = 0.5 SHIP_STATES = ('IDLE', 'SPAWNING', 'ACTIVE', 'DYING', 'DEAD', 'RESPAWN') SPEED = 4 START_POS = Rect(config.SCREEN_WIDTH / 2, config.SCREEN_HEIGHT * .8, 32, 32) ################################################################################ ### Preparation ################################################################ for i in FRAMES: i.set_colorkey(color.COLOR_KEY, config.BLIT_FLAGS) ################################################################################ class FlameTrail(GameObject): '''
whatever's in the bottom-left corner. @var COLOR_BLIND_FRAMES: The shapes that are placed over game objects in colorblind mode. @var COLOR_BLIND_SYMBOLS: Mapping of LIST plus WHITE to COLOR_BLIND_FRAMES ''' RED = Color('#FF3333') GREEN = Color('#4FFF55') BLUE = Color('#585EFF') YELLOW = Color('#EBFF55') PURPLE = Color('#FF55D5') WHITE = Color('#FFFFFF') BLACK = Color('#000000') COLOR_BLIND_FRAMES = tuple(Rect(32 * i, 32, 32, 32) for i in range(4, 10)) COLOR_BLIND_SYMBOLS = { id(RED): config.get_sprite(COLOR_BLIND_FRAMES[0]), id(BLUE): config.get_sprite(COLOR_BLIND_FRAMES[1]), id(GREEN): config.get_sprite(COLOR_BLIND_FRAMES[2]), id(YELLOW): config.get_sprite(COLOR_BLIND_FRAMES[3]), id(PURPLE): config.get_sprite(COLOR_BLIND_FRAMES[4]), id(WHITE): None, } COLOR_KEY = config.SPRITES.get_at((0, config.SPRITES.get_height() - 1)) LIST = (RED, BLUE, GREEN, YELLOW, PURPLE) for i in LIST: COLOR_BLIND_SYMBOLS[id(i)].set_colorkey(COLOR_KEY) ################################################################################ ### Functions ##################################################################
#If we've reached our target location... self.change_state(Particle.STATES.LEAVING) else: dx = (percent**2) * (3 - 2 * percent) ddx = 1 - dx position[0] = (self.startpos[0] * ddx) + (START_POS.centerx * dx) position[1] = (self.startpos[1] * ddx) + (START_POS.centery * dx) self.rect.topleft = (position[0] + .5, position[1] + .5) ################################################################################ ### Constants ################################################################## PART_IMAGE = Rect(4, 170, 4, 4) APPEAR = config.load_sound('appear.wav') APPEAR_POOL = ParticlePool(config.get_sprite(PART_IMAGE), _radius_move, _radius_appear) DEATH = config.load_sound('death.wav') DEATH_POOL = ParticlePool(config.get_sprite(PART_IMAGE), appear_func=_burst_appear) FRAMES = tuple(config.get_sprite(Rect(32 * i, 128, 32, 32)) for i in range(5)) GRAVITY = 0.5 SHIP_STATES = ('IDLE', 'SPAWNING', 'ACTIVE', 'DYING', 'DEAD', 'RESPAWN') SPEED = 4 START_POS = Rect(config.SCREEN_WIDTH / 2, config.SCREEN_HEIGHT * .8, 32, 32) ################################################################################ ### Preparation ################################################################ for i in FRAMES: i.set_colorkey(color.COLOR_KEY, config.BLIT_FLAGS) ################################################################################