Flare

Tinkering around with vectors, I created this fancy program. By left-clicking you spawn a white circle at your current mouse position. This circle then will emit a specific quantity of red pixels (\text{which is }c = 2 \pi r \text{ with } r = 50). Those red pixels then get pushed across the screen until they eventually die out.

Controls

  • Left click spawns in a circle
  • Space saves a screenshot

JBlog! Nameception A glowing flower


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 14th of October, 2015
#         edited 15th of October, 2015

# importing needed modules
import pygame, sys, time, math, os, random, datetime

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

class charge():
	def __init__(self, _pos):
		self.pos = _pos
		self.radius = 10
		self.entities = []

		n = int(self.radius*math.pi*2)
		for _ in range(0, n):
			p = getCirclePos(self.pos, self.radius, 360. / n * _)
			self.entities.append(p)

	def tick(self):
		_n = -1
		for _ in self.entities:
			main.SURF.set_at(intpos(_), [255, 0, 0])

			_n += 1
			vec = vecConvert(_, self.pos)
			vecl = vecLen(vec)
			if vecl != 0 and vecl < 1000:
				vec0 = vecMultiply(vec, 1. / vecl)
				vecn = vecMultiply(vec0, vecl*1.1)
				self.entities[_n] = vecGetPoint(self.pos, vecn)
			else:
				self.entities.remove(_)

	def render(self, _surface):
		pygame.draw.circle(_surface, [255, 255, 255], self.pos, self.radius)

""" 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])]

# basic vector functions
def vecConvert(p1, p2):
	return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
	return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
	return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
	return [point[0] + vec[0], point[1] + vec[1]]

# saves the current surface
def saveSurface():
	try:
		path = os.getcwd() + "/out/"
		if not os.path.isdir(path):
			os.mkdir(path)

		name = path + "img" + str(len(os.listdir(path))) + ".png"
		pygame.image.save(main.SURF, name)
	except:
		pass

# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			quit()

		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_SPACE:
				saveSurface()

		if event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == 3:
				main.CHARGES.append(charge(list(pygame.mouse.get_pos())))

	for _ in main.CHARGES:
		_.tick()

# render function
def render():
	# render white bobbles
	for _ in main.CHARGES:
		_.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 = "Flare"
	main.COLOR = [0, 0, 0]
	main.TICKS = 0

	main.CHARGES = []
	
	# 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s