Trippy

Using the extended pygame graphics function pygame.gfxdraw, this program creates randomly colored trippy circles. There are 100 circles, which get redrawn every 100 ticks (~ 1.6 seconds).
The first example The second example The third example


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 27th of August, 2015

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

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

""" FUNCTIONS """
# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
	newColor = math.fabs(_color)
	n = _max - _min
	if newColor > n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

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

# draw a circle
def draw(x = 0, y = 0, w = 100, wn = 30, c = [255, 255, 255], cn = 10):
	C = c[:]
	for W in range(w, w+wn):
		for _ in range(0, 3):
			C[_] = colorValid(C[_] - cn)
		pygame.gfxdraw.aacircle(main.SURF, x, y, W, C)

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

# render function
def render():
	if main.TICKS % 100 == 0:
		# fill
		main.SURF.fill(main.COLOR)
		
		for _ in range(0, 100):
			draw(random.randint(0, main.WIDTH), random.randint(0, main.HEIGHT), random.randint(0, 100), random.randint(10, 30), [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)], random.randint(5, 15))
			#draw(random.randint(0, main.WIDTH), random.randint(0, main.HEIGHT))
	
	# 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.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Trippy"
	main.COLOR = [0, 0, 0]
	main.TICKS = 0
	
	# 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