def __init__(self, lifetime, color): default = pygame.Surface((2, 2)) default.fill((208, 70, 72)) super(Particle, self).__init__(default) sheet = [] dark = 20, 12, 28 steps = 8 for i in range(steps): amount = i / float(steps - 1) r = lerp(color[0], dark[0], amount) g = lerp(color[1], dark[1], amount) b = lerp(color[2], dark[2], amount) s = pygame.Surface((2, 2)) s.fill((r, g, b)) sheet.append(s) frame_time = lifetime / float(steps - 1) puff = [(image, frame_time) for image in sheet] self.animation = Animation('puff', puff) self.add(self.animation) self.add(DestroyAfter(lifetime))
def createVerticalGradient(dimension, *stops): surf = pygame.Surface(dimension) width, height = dimension num_stops = len(stops) for h in range(height): step = float(h) / (height) # Caclulate the array index i = min(int(step * (num_stops - 1)), num_stops - 1) # Adjust the step to properly blend between the gradient stops. grad_step = step * (num_stops - 1) - i r = lerp(stops[i][0], stops[i+1][0], grad_step) g = lerp(stops[i][1], stops[i+1][1], grad_step) b = lerp(stops[i][2], stops[i+1][2], grad_step) pygame.draw.line(surf, (r,g,b), (0, h), (width, h)) return surf
def update(self, milliseconds=0): self.game_object.transform.position = lerp(self.game_object.transform.position, self.__target, self.tracking_strength)
step = 0.1 # Game loop running = True while running: clock.tick(60) buffer.fill((0,0,0)) bar1.render(buffer) bar2.render(buffer) bar3.render(buffer) # Scale up buffer and draw to screen screen.blit(pygame.transform.scale(buffer,SCREEN_SIZE),pygame.Rect(ORIGIN,SCREEN_SIZE)) pygame.display.flip() # Handle input for event in pygame.event.get(): if event.type == MOUSEMOTION: x, y = event.pos[0], event.pos[1] x = x / float(SCREEN_SIZE[0]) y = y / float(SCREEN_SIZE[1]) elif event.type == QUIT: running = False bar1.current = lerp(bar1.current, x, step) bar2.current = lerp(bar2.current, y, step) bar3.current = lerp(bar3.current, (x + y) / 2.0, step)