In the spirit of the new year, this program tries to mimic white fireworks exploding in the night sky.
# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 3rd of October, 2015
# edited 1st of January, 2016
# importing needed modules
import pygame, sys, time, math, os, random, datetime
""" CLASSES """
# dummy class for global variables
class dummy():
pass
# node class
class node():
def __init__(self):
#self.pos = [main.WIDTH/2, main.HEIGHT/2]
self.pos = [random.randint(0, main.WIDTH), random.randint(0, main.HEIGHT)]
self.radius = 100
self.density = 100
def tick(self):
#self.pos[random.randint(0, 1)] += random.randint(-1, 1)
self.radius += random.randint(-1, 1)
self.density += random.randint(-1, 1)
for _ in range(0, 2):
if self.pos[_] < 0:
self.pos[_] = 0
elif self.pos[_] > main.SIZE[_]:
self.pos[_] = main.SIZE[_]
if self.radius < 1:
self.radius = 1
if self.density < 1:
self.desity = 1
def render(self, _surface):
for _ in range(0, self.density):
p = getCirclePos(self.pos, self.radius, 360./self.density*_)
pygame.draw.circle(_surface, [255, 255, 255], intpos(p), 3)
pygame.draw.line(_surface, [255, 255, 255], p, self.pos)
""" FUNCTIONS """
# gets the position on a circle
# circle center : '_pos'
# circle radius : '_radius'
# angle from center to point on the circle: '_angle'
def getCirclePos(_pos, _radius, _angle):
return [
_pos[0] + _radius * math.cos(math.radians(_angle)),
_pos[1] + _radius * math.sin(math.radians(_angle))
]
# returns an integer version of given positon
def intpos(_pos):
return [int(_pos[0]), int(_pos[1])]
# takes a screenshot
def screenshot():
path = os.getcwd() + "/out/"
try:
if not os.path.isdir(path):
os.mkdir(path)
name = "img" + str(len(os.listdir(path))) + ".png"
pygame.image.save(main.SURF, path + name)
except:
pass
# quits the program
def quit():
sys.exit()
""" TICK; RENDER """
# tick function
def tick():
# handle events
for event in pygame.event.get():
# quit
if event.type == pygame.QUIT:
quit()
# keydown
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
screenshot()
# tick nodes
for _ in main.NODES:
for __ in range(0, 100):
_.tick()
# render function
def render():
# fill
main.SURF.fill(main.COLOR)
# render nodes
for _ in main.NODES:
_.render(main.SURF)
# blit and flip
main.SCREEN.blit(main.SURF, [0, 0])
pygame.display.flip()
""" INIT """
# initialize program
def init():
main.WIDTH, main.HEIGHT = 1080, 720
main.SIZE = [main.WIDTH, main.HEIGHT]
main.CENTER = [main.WIDTH / 2., main.HEIGHT / 2.]
main.SCREEN = pygame.display.set_mode(main.SIZE)
main.SURF = pygame.Surface(main.SIZE)
main.CAPTION = "White Fireworks"
main.COLOR = [0, 0, 0]
main.TICKS = 0
main.KEYSDOWN = []
main.NODES = []
for _ in range(0, 20):
main.NODES.append(node())
# functions
pygame.display.set_caption(main.CAPTION)
""" RUN """
# run function (uses tick() and render())
def run():
ticksPerSecond = 60
lastTime = time.time() * 1000000000
nsPerTick = 1000000000.0 / float(ticksPerSecond)
ticks = 0
frames = 0
lastTimer = time.time() * 1000
delta = 0.0
while True:
now = time.time() * 1000000000
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False
while delta >= 1:
ticks += 1
main.TICKS += 1
tick()
delta -= 1
shouldRender = True
if shouldRender:
frames += 1
render()
if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000
# debug
# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
frames = 0
ticks = 0
# main variable
main = dummy()
init()
# start program
run()
Advertisements