Created
December 28, 2015 21:51
-
-
Save clonejo/93c28372151a449dce2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python2 | |
| import pygame | |
| import itertools | |
| import random | |
| import math | |
| # Initialize the game engine | |
| pygame.init() | |
| # Define some colors | |
| RED = (255, 0, 0) | |
| ORANGE = (255, 128, 0) | |
| YELLOW = (255, 255, 0) | |
| GREEN = (0, 255, 0) | |
| BLUE = (0, 0, 255) | |
| VIOLETT = (255, 0, 255) | |
| COLORS = [RED, ORANGE, YELLOW, GREEN, BLUE, VIOLETT] | |
| PIXEL = 5 | |
| #WIDTH = 1440 | |
| #HEIGHT = 900 | |
| WIDTH = 720 | |
| HEIGHT = 450 | |
| MAX_PARTICLES = 2000 | |
| MAX_SPAWNED_PARTICLES_PER_TICK = 10 | |
| VELOCITY = 0.5 | |
| FPS = 60 | |
| ARMS = 4 | |
| ARM_ANGLE = 1.0/ARMS | |
| MAX_ROTATION_VELOCITY = 0.02 | |
| PIXEL_WIDTH = WIDTH // PIXEL | |
| PIXEL_HEIGHT = HEIGHT // PIXEL | |
| source = {'x': PIXEL_WIDTH / 2.0, | |
| 'y': PIXEL_HEIGHT / 2.0, | |
| 'vx': 0.0, #math.cos(math.pi/4)*VELOCITY/2, | |
| 'vy': 0.0, #math.sin(math.pi/4)*VELOCITY/2, | |
| 'ax': 0.01, | |
| 'ay': 0.01, | |
| 'r': 0.0, | |
| 'rv': 0.0, | |
| 'ra': 0.00003} | |
| tick = 0 | |
| # Set the height and width of the screen | |
| size = (WIDTH, HEIGHT) | |
| screen = pygame.display.set_mode(size) | |
| buf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) | |
| buf.fill((0,0,0,255)) | |
| pygame.display.set_caption("Particles") | |
| # Loop until the user clicks the close button. | |
| done = False | |
| clock = pygame.time.Clock() | |
| particles = [] | |
| # Loop as long as done == False | |
| while not done: | |
| # events | |
| for event in pygame.event.get(): # User did something | |
| if event.type == pygame.QUIT: # If user clicked close | |
| done = True # Flag that we are done so we exit this loop | |
| # update state | |
| def gen_particle(): | |
| angle = random.random() | |
| color_offset = 0 | |
| if (angle+source['r']) % (2*ARM_ANGLE) < ARM_ANGLE: | |
| color_offset = 2 | |
| color = COLORS[(tick//FPS//2 + color_offset) % len(COLORS)] | |
| angle *= 2*math.pi | |
| return {'x': source['x'], | |
| 'y': source['y'], | |
| 'vx': math.cos(angle)*VELOCITY + source['vx'], | |
| 'vy': math.sin(angle)*VELOCITY + source['vy'], | |
| 'color': color} | |
| def move_particle(p): | |
| p['x'] += p['vx'] | |
| p['y'] += p['vy'] | |
| def wrap_particle(p): | |
| p['x'] %= PIXEL_WIDTH | |
| p['y'] %= PIXEL_HEIGHT | |
| particles_to_add = min(MAX_PARTICLES - len(particles), MAX_SPAWNED_PARTICLES_PER_TICK) | |
| for _ in xrange(0, particles_to_add): | |
| particles.append(gen_particle()) | |
| for i in xrange(0, len(particles)): | |
| p = particles[i] | |
| move_particle(p) | |
| if p['x'] < 0 or p['x'] >= PIXEL_WIDTH or p['y'] < 0 or p['y'] >= PIXEL_HEIGHT: | |
| particles[i] = gen_particle() | |
| source['vx'] += random.uniform(-0.5, 0.5) * source['ax'] | |
| source['vy'] += random.uniform(-0.5, 0.5) * source['ay'] | |
| move_particle(source) | |
| wrap_particle(source) | |
| source['r'] += source['rv'] | |
| source['rv'] += source['ra'] | |
| if source['rv'] < -MAX_ROTATION_VELOCITY or source['rv'] > MAX_ROTATION_VELOCITY: | |
| source['ra'] *= -1 | |
| # render | |
| buf.fill((0,0,0,40)) | |
| def draw_pixel(coords): | |
| buf.fill( | |
| coords['color'], | |
| rect=[int(coords['x'])*PIXEL, int(coords['y'])*PIXEL, PIXEL-1, PIXEL-1], | |
| special_flags=pygame.BLEND_ADD) | |
| #) | |
| for p in particles: | |
| draw_pixel(p) | |
| screen.blit(buf, (0,0)) | |
| pygame.display.flip() | |
| # sleep | |
| clock.tick(FPS) | |
| tick += 1 | |
| # Be IDLE friendly | |
| pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment