Advertisement
cookertron

Python Pygame - Glowing Particles

Oct 20th, 2021 (edited)
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. import pygame
  2. from pygame import Rect
  3. from pygame import Vector2
  4. from random import randint, uniform
  5. import time
  6.  
  7. # timer: returns number of frames since last call to timer.update()
  8. class timer:
  9.     def __init__(s, seconds):
  10.         s.ts = time.time()
  11.         s.seconds = seconds
  12.    
  13.     def update(s):
  14.         now = time.time()
  15.         duration = now - s.ts
  16.         frames = int(duration / s.seconds)
  17.         if frames >= 1:
  18.             s.ts = now
  19.         return frames
  20.  
  21. # particle
  22. class particle:
  23.     def __init__(s, xy): # xy = tuple(x, y)
  24.         # position of the particle
  25.         s.xy = Vector2(xy)
  26.  
  27.         # particle velocity
  28.         s.vel = Vector2(uniform(-1, 1), uniform(-4.5, -5)) # x vel is betweem -1 & 1 and y vel is between -4.5 and -5
  29.  
  30.         # particle radius
  31.         s.radius = randint(5, 15) # random integer between 5 & 15
  32.  
  33.     # draw particle and update position/size
  34.     def update(s):
  35.         global PDS, WHITE # PDS = primary display surface
  36.        
  37.         # draw particle
  38.         pygame.draw.circle(PDS, WHITE, s.xy, s.radius)
  39.  
  40.         # draw glow
  41.         s.glow()
  42.  
  43.         # add velocity to particle
  44.         s.xy += s.vel
  45.  
  46.         # add gravity to velocity. negative velocity will slowly become positive
  47.         s.vel.y += GRAVITY
  48.  
  49.         # decrease radius to give a fizzling out effect
  50.         s.radius -= 0.05
  51.  
  52.     def glow(s):
  53.         global PDS
  54.         # get rectangular size of the particle. radius * 2
  55.         r = Vector2(s.radius) * 2 # r = (radius * 2, radius * 2)
  56.        
  57.         # create a surface twice the size of the particle
  58.         gs = pygame.Surface(r * 2)
  59.        
  60.         # draw a circle with a radius twice the size of the particle in the center of the new surface
  61.         pygame.draw.circle(gs, (20, 20, 60), r, r.x)
  62.  
  63.         # blit the new larger particle on the primary display with the center of the smaller particle
  64.         # use BLEND_RGB_ADD to increase luminosity if particles overlap
  65.         PDS.blit(gs, s.xy - r, special_flags=pygame.BLEND_RGB_ADD)
  66.  
  67. # initialise pygame and create primary display surface PDS
  68. pygame.init()
  69. PDR = Rect(0, 0, 1280, 720)
  70. PDS = pygame.display.set_mode(PDR.size)
  71.  
  72. # define some colors
  73. BLACK = (0, 0, 0)
  74. WHITE = (255, 255, 255)
  75.  
  76. # strength of gravity
  77. GRAVITY = 0.1
  78.  
  79. # timer is set to update every 0.01 seconds
  80. # so every 0.01 we'll add a new particle
  81. addParticleTimer = timer(0.01)
  82.  
  83. # particle container
  84. particles = []
  85.  
  86. exitDemo = False
  87. pause = False # press p to pause
  88. while not exitDemo:
  89.  
  90.     # handle the events
  91.     events = pygame.event.get()
  92.     for event in events:
  93.         if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):    
  94.             exitDemo = True
  95.         if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
  96.             pause = 1 - pause
  97.    
  98.     if pause: continue
  99.  
  100.     # check if timer wants to update...
  101.     if addParticleTimer.update():
  102.         # ...yes? add particle at the mouse pointer location
  103.         particles += [particle(pygame.mouse.get_pos())]
  104.  
  105.     # clear the PDS to black
  106.     PDS.fill(BLACK)
  107.  
  108.     # create a container to hold particles that need deleting
  109.     killParticles = []
  110.     for p in particles:
  111.         p.update() # draw and update postion/size of the particle
  112.        
  113.         # if the particle is outside of the PDS or the radius is less than zero ie no longer visible
  114.         # then add it the killparticles pile
  115.         if p.xy.y >= PDR.bottom or p.radius <= 0:
  116.             killParticles += [p]
  117.    
  118.     # delete particles from particle container
  119.     for p in killParticles:
  120.         particles.remove(p)
  121.  
  122.     # update the PDS
  123.     pygame.display.update()
  124.     pygame.time.Clock().tick(120) # limit FPS to 120
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement