Advertisement
trodland

particles

Oct 11th, 2020
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. import pygame, random
  2.  
  3. pygame.init()
  4. clock = pygame.time.Clock()
  5. WIDTH = 800
  6. HEIGHT = 600
  7. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  8.  
  9. particles = []
  10.  
  11. # Creating a semi-transparent surface to create fade-effect.
  12. bg = pygame.Surface((WIDTH,HEIGHT))
  13. bg.set_alpha(70)
  14. bg.fill((0,0,0))
  15.  
  16. class Particle():
  17.     def __init__(self,x,y):
  18.         self.x = x
  19.         self.y = y
  20.         self.speed_x = random.randint(-12,12)
  21.         self.speed_y = random.randint(-12,2)
  22.         self.life = random.randint(3,9)
  23.         self.farge = 255
  24.    
  25.     def update(self):
  26.         self.x += self.speed_x
  27.         self.y += self.speed_y
  28.         self.speed_y += 0.5
  29.         self.life -= 0.5
  30.         self.farge -= 7
  31.        
  32.         if self.x > WIDTH or self.x < 0: self.speed_x *= -1
  33.        
  34.         if self.y > HEIGHT: self.speed_y *= -1
  35.        
  36.         if self.life < 1:
  37.             particles.remove(self)
  38.             del self
  39.    
  40.     def draw(self):
  41.         pygame.draw.circle(screen,(255,self.farge,0),(int(self.x),int(self.y)),int(self.life))
  42. # END OF PARTICLE CLASS
  43.  
  44. def explotion(mx,my):
  45.     for i in range(60):
  46.         minPartikkel = Particle(mx,my)
  47.         particles.insert(0,minPartikkel)
  48.  
  49. while True:
  50.     screen.blit(bg,(0,0))
  51.     for particle in particles[::-1]:
  52.         particle.update()
  53.         particle.draw()
  54.  
  55.     clock.tick(30)
  56.     pygame.display.update()
  57.    
  58.     for event in pygame.event.get():
  59.         if event.type == pygame.QUIT:
  60.             running = False
  61.            
  62.         # create explotion everywhere the mouse moves
  63.         if event.type == pygame.MOUSEMOTION:
  64.             mx,my = pygame.mouse.get_pos()
  65.             explotion(mx,my)            
  66. pygame.quit()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement