├── nyan_cat.png ├── particle_progress.py └── star.png /nyan_cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/PygameParticles/6a315714b4f2f129157b3b6d1576c1c3cc9a7560/nyan_cat.png -------------------------------------------------------------------------------- /particle_progress.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | class ParticlePrinciple: 4 | def __init__(self): 5 | self.particles = [] 6 | 7 | def emit(self): 8 | if self.particles: 9 | self.delete_particles() 10 | for particle in self.particles: 11 | particle[0][1] += particle[2][0] 12 | particle[0][0] += particle[2][1] 13 | particle[1] -= 0.2 14 | pygame.draw.circle(screen,pygame.Color('White'),particle[0], int(particle[1])) 15 | 16 | def add_particles(self): 17 | pos_x = pygame.mouse.get_pos()[0] 18 | pos_y = pygame.mouse.get_pos()[1] 19 | radius = 10 20 | direction_x = random.randint(-3,3) 21 | direction_y = random.randint(-3,3) 22 | particle_circle = [[pos_x,pos_y],radius,[direction_x,direction_y]] 23 | self.particles.append(particle_circle) 24 | 25 | def delete_particles(self): 26 | particle_copy = [particle for particle in self.particles if particle[1] > 0] 27 | self.particles = particle_copy 28 | class ParticleNyan: 29 | def __init__(self): 30 | self.particles = [] 31 | self.size = 12 32 | 33 | def emit(self): 34 | if self.particles: 35 | self.delete_particles() 36 | for particle in self.particles: 37 | particle[0].x -= 1 38 | pygame.draw.rect(screen,particle[1],particle[0]) 39 | 40 | self.draw_nyancat() 41 | 42 | def add_particles(self,offset,color): 43 | pos_x = pygame.mouse.get_pos()[0] 44 | pos_y = pygame.mouse.get_pos()[1] + offset 45 | particle_rect = pygame.Rect(int(pos_x - self.size/2),int(pos_y - self.size/2),self.size,self.size) 46 | self.particles.append((particle_rect,color)) 47 | 48 | def delete_particles(self): 49 | particle_copy = [particle for particle in self.particles if particle[0].x > 0] 50 | self.particles = particle_copy 51 | 52 | def draw_nyancat(self): 53 | nyan_rect = nyan_surface.get_rect(center = pygame.mouse.get_pos()) 54 | screen.blit(nyan_surface,nyan_rect) 55 | 56 | class ParticleStar: 57 | def __init__(self): 58 | self.particles = [] 59 | self.surface = pygame.image.load('Star.png').convert_alpha() 60 | self.width = self.surface.get_rect().width 61 | self.height = self.surface.get_rect().height 62 | 63 | def emit(self): 64 | if self.particles: 65 | self.delete_particles() 66 | for particle in self.particles: 67 | particle[0].x += particle[1] 68 | particle[0].y += particle[2] 69 | particle[3] -= 0.2 70 | screen.blit(self.surface,particle[0]) 71 | 72 | def add_particles(self): 73 | pos_x = pygame.mouse.get_pos()[0] - self.width / 2 74 | pos_y = pygame.mouse.get_pos()[1] - self.height / 2 75 | direction_x = random.randint(-3,3) 76 | direction_y = random.randint(-3,3) 77 | lifetime = random.randint(4,10) 78 | particle_rect = pygame.Rect(int(pos_x),int(pos_y),self.width,self.height) 79 | self.particles.append([particle_rect,direction_x,direction_y,lifetime]) 80 | 81 | def delete_particles(self): 82 | particle_copy = [particle for particle in self.particles if particle[3] > 0] 83 | self.particles = particle_copy 84 | 85 | pygame.init() 86 | screen = pygame.display.set_mode((500,500)) 87 | clock = pygame.time.Clock() 88 | 89 | particle1 = ParticlePrinciple() 90 | 91 | nyan_surface = pygame.image.load('nyan_cat.png').convert_alpha() 92 | particle2 = ParticleNyan() 93 | 94 | particle3 = ParticleStar() 95 | 96 | PARTICLE_EVENT = pygame.USEREVENT + 1 97 | pygame.time.set_timer(PARTICLE_EVENT,40) 98 | 99 | while True: 100 | for event in pygame.event.get(): 101 | if event.type == pygame.QUIT: 102 | pygame.quit() 103 | sys.exit() 104 | if event.type == PARTICLE_EVENT: 105 | #particle1.add_particles() 106 | # particle2.add_particles(-30,pygame.Color("Red")) 107 | # particle2.add_particles(-18,pygame.Color("Orange")) 108 | # particle2.add_particles(-6,pygame.Color("Yellow")) 109 | # particle2.add_particles(6,pygame.Color("Green")) 110 | # particle2.add_particles(18,pygame.Color("Blue")) 111 | # particle2.add_particles(30,pygame.Color("Purple")) 112 | particle3.add_particles() 113 | 114 | screen.fill((30,30,30)) 115 | #particle1.emit() 116 | #particle2.emit() 117 | particle3.emit() 118 | pygame.display.update() 119 | clock.tick(120) -------------------------------------------------------------------------------- /star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/PygameParticles/6a315714b4f2f129157b3b6d1576c1c3cc9a7560/star.png --------------------------------------------------------------------------------