# Pastel

This program uses two positions and a color to create beautiful images.

#### Usage

• ‘q’ saves the current image
• Space pauses and resumes the image’s creation

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 21st of September, 2015
#         edited 23rd of September, 2015
#         edited 24th of September, 2015``````

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

""" 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'
# angle from center to point on the circle: '_angle'
return [
]

# 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]]
return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# calculates 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()

""" 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)
if event.key == pygame.K_SPACE:
main.RUNNING = not main.RUNNING

# save surface
if event.key == pygame.K_q:
# try/catch loop
try:
# create 'out' directory
if not os.path.exists(os.getcwd() + "/out"):
os.mkdir(os.getcwd() + "/out")

# get highest, not used image number
n = 1
while os.path.exists(os.getcwd() + "/out/img" + str(n) + ".png"):
n += 1

# save
pygame.image.save(main.SURF, os.getcwd() + "/out/img" + str(n) + ".png")

except:
print "Trouble saving!"

for _ in range(0, 1000):
if main.RUNNING:
main.P1[random.randint(0, 1)] += random.randint(-1, 1)
main.P2[random.randint(0, 1)] += random.randint(-1, 1)
main.C[random.randint(0, 2)] += random.randint(-1, 1)

for _ in range(0, 2):
if main.P1[_] < 0: main.P1[_] = 0 if main.P1[_] > main.SIZE[_]:
main.P1[_] = main.SIZE[_]
for _ in range(0, 2):
if main.P2[_] < 0: main.P2[_] = 0 if main.P2[_] > main.SIZE[_]:
main.P2[_] = main.SIZE[_]
for _ in range(0, 3):
if main.C[_] < main.CMIN[_]: main.C[_] = main.CMIN[_] if main.C[_] > main.CMAX[_]:
main.C[_] = main.CMAX[_]

c = [int(main.C[0]), int(main.C[1]), int(main.C[2])]
pygame.draw.line(main.SURF, c, main.P1, main.P2)

# render function
def render():
# 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 = "Pastel"
main.TICKS = 0
main.KEYSDOWN = []

main.P1 = main.CENTER[:]
main.P2 = main.CENTER[:]
main.C = [0, 0, 0]
main.CMIN = [0, 0, 0]
main.CMAX = [255, 0, 255]
main.RUNNING = True

# 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()``````

## One thought on “Pastel”

1. Pingback: Second Anniversary – J-Blog

This site uses Akismet to reduce spam. Learn how your comment data is processed.