Gradient Drawer

This is more of an artistic program. By left-clicking you set a fix point and define a random color. Where you then move your mouse, the program will draw 100 circles in between the fix point and the cursor position. By right-clicking you save those 100 circles on the canvas. Holding the right mouse button is possible.

Controls

  • Left clicking sets or removes a fix point
  • Right clicking saves current circles on canvas
  • ‘s’ saves current canvas
  • ‘c’ clears current canvas

The First Example The Second Example The Third Example The Fourth Example The Fifth Example


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 4th of July, 2015

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

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

""" FUNCTIONS """
# gets the mouse position
def getMousePos():
	p = pygame.mouse.get_pos()
	return [p[0], p[1]]

# 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

# 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]]
def vecAdd(vec1, vec2):
	return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# claculates distance between given positions
def posDistance(p1, p2):
	return math.sqrt( (p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 )

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

# returns a random color
def randomColor():
	return [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]

# saves the current surface
def saveSurf():
	try:
		if not os.path.exists(main.PATH):
			os.mkdir(main.PATH)
		
		n = 1
		f0 = "pic"
		f1 = ".png"
		f = f0 + f1
		while os.path.exists(main.PATH + "/" + f):
			n += 1
			f = f0 + str(n) + f1
		
		pygame.image.save(main.SURF, main.PATH + "/" + f)
		pygame.display.set_caption(main.CAPTION + " (" + f + ")")
	except:
		pygame.display.set_caption(main.CAPTION + " (trouble saving...)")

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
				quit()
		
		# keyup
		if event.type == pygame.KEYUP:
			# handle 'main.KEYSDOWN'
			if event.key in main.KEYSDOWN:
				main.KEYSDOWN.remove(event.key)
		
		# keydown
		if event.type == pygame.KEYDOWN:
			# handle 'main.KEYSDOWN'
			if event.key not in main.KEYSDOWN:
				main.KEYSDOWN.append(event.key)
			
			# save
			if event.key == pygame.K_s:
				saveSurf()
			
			# clear
			if event.key == pygame.K_c:
				main.SURF.fill(main.COLOR)
		
		# change the fix point and color
		if not main.COM and event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == 1:
				if main.FIXPOINT == None:
					main.FIXPOINT = getMousePos()
					main.FIXCOLOR = randomColor()
				else:
					main.FIXPOINT = None

# render function
def render():
	# render surface
	main.SCREEN.blit(main.SURF, [0, 0])
	
	# the computer controls what the user does with the mouse
	if main.COM:
		p = getCirclePos(main.FIXPOINT, main.RAD, main.ANG)
		
		main.RAD += 1
		main.ANG += 1
	else:
		p = getMousePos()
	
	# render preview and when right mouse button is pressed render on surface
	if main.FIXPOINT != None:
		vec = vecConvert(main.FIXPOINT, p)
		vecL = vecLen(vec)
		
		if vecL != 0:
			vecn = vecMultiply(vec, 1. / vecL)
			
			for _ in range(0, 100):
				l = vecL / 100. * _
				c = [
					colorValid( main.FIXCOLOR[0] + (_ * 1) ),
					colorValid( main.FIXCOLOR[1] + (_ * 2) ),
					colorValid( main.FIXCOLOR[2] + (_ * 3) )
				]
				
				if pygame.mouse.get_pressed()[2] or main.COM:
					pygame.draw.circle(main.SURF, c, intpos(vecGetPoint(vecMultiply(vecn, l), main.FIXPOINT)), 20)
				pygame.draw.circle(main.SCREEN, c, intpos(vecGetPoint(vecMultiply(vecn, l), main.FIXPOINT)), 20)
	
	# flip
	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 = "Gradient Drawer"
	main.COLOR = [20, 20, 20]
	main.TICKS = 0
	main.KEYSDOWN = []
	
	main.FIXPOINT = None#main.CENTER[:]
	main.FIXCOLOR = None#randomColor()
	main.PATH = os.getcwd() + "/screenshots"
	
	main.COM = False
	main.RAD = 100
	main.ANG = 0
	
	# functions
	main.SURF.fill(main.COLOR)
	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

One thought on “Gradient Drawer

  1. Pingback: Second Anniversary – J-Blog

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