Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame import Rect
- from pygame import Vector2
- from random import randint, uniform
- import time
- # timer: returns number of frames since last call to timer.update()
- class timer:
- def __init__(s, seconds):
- s.ts = time.time()
- s.seconds = seconds
- def update(s):
- now = time.time()
- duration = now - s.ts
- frames = int(duration / s.seconds)
- if frames >= 1:
- s.ts = now
- return frames
- # particle
- class particle:
- def __init__(s, xy): # xy = tuple(x, y)
- # position of the particle
- s.xy = Vector2(xy)
- # particle velocity
- 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
- # particle radius
- s.radius = randint(5, 15) # random integer between 5 & 15
- # draw particle and update position/size
- def update(s):
- global PDS, WHITE # PDS = primary display surface
- # draw particle
- pygame.draw.circle(PDS, WHITE, s.xy, s.radius)
- # draw glow
- s.glow()
- # add velocity to particle
- s.xy += s.vel
- # add gravity to velocity. negative velocity will slowly become positive
- s.vel.y += GRAVITY
- # decrease radius to give a fizzling out effect
- s.radius -= 0.05
- def glow(s):
- global PDS
- # get rectangular size of the particle. radius * 2
- r = Vector2(s.radius) * 2 # r = (radius * 2, radius * 2)
- # create a surface twice the size of the particle
- gs = pygame.Surface(r * 2)
- # draw a circle with a radius twice the size of the particle in the center of the new surface
- pygame.draw.circle(gs, (20, 20, 60), r, r.x)
- # blit the new larger particle on the primary display with the center of the smaller particle
- # use BLEND_RGB_ADD to increase luminosity if particles overlap
- PDS.blit(gs, s.xy - r, special_flags=pygame.BLEND_RGB_ADD)
- # initialise pygame and create primary display surface PDS
- pygame.init()
- PDR = Rect(0, 0, 1280, 720)
- PDS = pygame.display.set_mode(PDR.size)
- # define some colors
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- # strength of gravity
- GRAVITY = 0.1
- # timer is set to update every 0.01 seconds
- # so every 0.01 we'll add a new particle
- addParticleTimer = timer(0.01)
- # particle container
- particles = []
- exitDemo = False
- pause = False # press p to pause
- while not exitDemo:
- # handle the events
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
- exitDemo = True
- if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
- pause = 1 - pause
- if pause: continue
- # check if timer wants to update...
- if addParticleTimer.update():
- # ...yes? add particle at the mouse pointer location
- particles += [particle(pygame.mouse.get_pos())]
- # clear the PDS to black
- PDS.fill(BLACK)
- # create a container to hold particles that need deleting
- killParticles = []
- for p in particles:
- p.update() # draw and update postion/size of the particle
- # if the particle is outside of the PDS or the radius is less than zero ie no longer visible
- # then add it the killparticles pile
- if p.xy.y >= PDR.bottom or p.radius <= 0:
- killParticles += [p]
- # delete particles from particle container
- for p in killParticles:
- particles.remove(p)
- # update the PDS
- pygame.display.update()
- pygame.time.Clock().tick(120) # limit FPS to 120
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement