Created
December 26, 2008 12:39
-
-
Save NaPs/40059 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
| #!/usr/bin/env python | |
| import pygame; import pygame.font; pygame.init() | |
| import sys | |
| from random import randint | |
| import time | |
| MAX_PARTICLES = 1000 | |
| class Particle(pygame.sprite.Sprite): | |
| def __init__(self, screen, size): | |
| pygame.sprite.Sprite.__init__(self) | |
| self.surface = pygame.Surface(size) | |
| self.rect = self.surface.get_rect(center=(randint(150, 350), 490)) | |
| self.life = randint(3, 10) | |
| self.initial_life = self.life | |
| self.speed = [randint(-8, 8), randint(-50, -10)] | |
| def make_move(self): | |
| self.rect = self.rect.move(self.speed) | |
| self.life -= 1 | |
| def is_alive(self): | |
| return bool(self.life) | |
| def make_color(self): | |
| if self.speed == [0, 0]: | |
| self.surface.fill((255, 0, 0)) | |
| self.surface.set_alpha(255) | |
| else: | |
| self.surface.fill((255, int(self.life/float(self.initial_life)*50), 0)) | |
| self.surface.set_alpha(int(self.life/float(self.initial_life)*200)) | |
| def main(): | |
| screen_size = (500, 500) | |
| screen = pygame.display.set_mode(screen_size) | |
| particles = [] | |
| obstacle_sur = pygame.Surface((100, 5)) | |
| obstacle = obstacle_sur.get_rect(center=(300, 300)) | |
| obstacle_sur.fill((255, 255, 255)) | |
| obstacle_hit = pygame.Rect((300, 300), (100, 50)) | |
| clock = pygame.time.Clock() | |
| default_font = pygame.font.Font(None, 20) | |
| fps = default_font.render('%0.1f fps' % clock.get_fps(), 1, (255, 255, 255)) | |
| fpspos = fps.get_rect(right=480, top=10) | |
| while 0xBeef: | |
| clock.tick(60) | |
| fps = default_font.render('%0.1f fps' % clock.get_fps(), 1, (255, 255, 255)) | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| sys.exit() | |
| while len(particles) < MAX_PARTICLES: | |
| particles.append(Particle(screen, [10, 10])) | |
| pos = pygame.mouse.get_pos() | |
| obstacle.midbottom = pos | |
| obstacle_hit.midbottom = pos | |
| screen.fill((0, 0, 0)) | |
| for p in particles: | |
| p.make_move() | |
| if not p.is_alive(): | |
| particles.remove(p) | |
| else: | |
| p.make_color() | |
| if p.rect.colliderect(obstacle_hit): | |
| #p.speed = [0, 0] | |
| #particles.remove(p) | |
| p.rect.top = obstacle.bottom | |
| p.speed = [0, 0] | |
| else: | |
| screen.blit(p.surface, p.rect) | |
| screen.blit(obstacle_sur, obstacle) | |
| screen.blit(fps, fpspos) | |
| pygame.display.flip() | |
| #time.sleep(0.03) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment