Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint, uniform
- import time, sys
- import pygame
- from pygame import Rect as R
- from pygame import Vector2 as V
- from pygame.locals import *
- class particle:
- def __init__(s, xy, vel): # xy = tuple(x, y)
- # position of the particle
- s.xy = V(xy)
- # particle velocity
- s.vel = 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(20, 30) # random integer between 5 & 15
- # draw particle and update position/size
- def update(s):
- global dt
- global GRAVITY
- global PDS, WHITE # PDS = primary display surface
- # draw particle
- pygame.draw.circle(PDS, (128, 128, 128), s.xy, s.radius)
- # draw glow
- s.glow()
- # add velocity to particle
- s.xy += s.vel * dt
- # add gravity to velocity. negative velocity will slowly become positive
- s.vel.y += GRAVITY * dt
- # decrease radius to give a fizzling out effect
- s.radius -= 0.1 * dt
- def glow(s):
- global PDS
- # get rectangular size of the particle. radius * 2
- r = V(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, (60, 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)
- def burst(xy, verticies, ricochet_direction):
- global parts
- for i in range(20):
- if verticies == 0: # horizontal particle burst
- v = V(uniform(-4, -2) if i // 10 else uniform(2, 4), uniform(1, 2) if ricochet_direction else uniform(-5, -4))
- else:
- v = V(uniform(1, 2) if ricochet_direction else uniform(-2, -1), uniform(-4, -2) if i // 10 else uniform(5, 4))
- parts += [particle(xy, v)]
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- pygame.init()
- PDS = pygame.display.set_mode()
- PDR = PDS.get_rect()
- FPS = 120
- GRAVITY = 0.1
- parts = []
- ball = V(PDR.center)
- rad = 80
- v = V()
- f = V()
- pressed = False
- pressed_timer = None
- dts = time.time()
- exit_demo = False
- while not exit_demo:
- for e in pygame.event.get():
- if e.type == KEYDOWN and e.key == K_ESCAPE:
- exit_demo = True
- now = time.time()
- dt = (now - dts) * FPS
- dts = now
- mp = V(pygame.mouse.get_pos())
- dist = mp.distance_to(ball)
- mb = pygame.mouse.get_pressed()
- if mb[0] and not pressed:
- if dist <= rad:
- pressed = True
- pressed_timer = time.time()
- if pressed:
- if not mb[0]:
- if dist > rad:
- deg = V().angle_to(mp - ball)
- a = V(1, 0)
- b = a.rotate(deg + 180) * (dist - rad)
- v = b / 10
- pressed = False
- if now - pressed_timer > 4:
- exit_demo = True
- ball += v * dt
- if v.x != 0 or v.y != 0:
- a = v.normalize()
- v -= (a / 10) * dt
- if ball.x + rad > PDR.w: # RIGHT
- ball.x = PDR.w - rad
- v.x = -v.x
- burst(ball + (rad, 0), 1, 0)
- if ball.x - rad < 0: # LEFT
- ball.x = rad
- v.x = -v.x
- burst(ball - (rad, 0), 1, 1)
- if ball.y + rad > PDR.h: # BOTTOM
- ball.y = PDR.h - rad
- v.y = -v.y
- burst(ball + (0, rad), 0, 0)
- if ball.y - rad < 0: # TOP
- ball.y = rad
- v.y = -v.y
- burst(ball - (0, rad), 0, 1)
- PDS.fill(0)
- # create a container to hold particles that need deleting
- kill_parts = []
- for p in parts:
- 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:
- kill_parts += [p]
- for p in kill_parts:
- parts.remove(p)
- if pressed and dist > rad:
- pygame.draw.line(PDS, RED, ball, mp)
- pygame.draw.circle(PDS, WHITE, ball, rad)
- pygame.display.update()
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement