# Spinning Shapes

Drawing lines according to length and angle, which change over time, this program creates pretty shapes. The changing values for both length and angle are saved in the image’s name.

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech  4th of December, 2015
#         edited  6th of December, 2015
#         edited 25th of December, 2015``````

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

# changing variables
LENGTHMULTIPLY = .999

""" FUNCTIONS """
# gets the position on a circle with a position, radius and an 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]]

""" GAME """
# game class
class GAME():
# initialize program
def __init__(self):
self.WIDTH, self.HEIGHT = 800, 800
self.SIZE = [self.WIDTH, self.HEIGHT]
self.SURF = pygame.Surface(self.SIZE)
self.SCREEN = pygame.display.set_mode(self.SIZE)

self.TICKS = 0
self.RUNNING = True

self.angle = 0
self.length = 300

# variables which determine the shape
self.lengthmultiply = LENGTHMULTIPLY

# set position to the center
self.pos = [self.WIDTH/2 - self.length/2, self.HEIGHT/2 - self.length/2]
self.color = [255, 255, 255]

# functions
pygame.display.set_caption("Spinning Shapes")

# tick function
def tick(self):
# 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:
self.screenshot()

# draw new line
if self.TICKS % 1 == 0:
#for _ in range(0, 100):
# get next position and draw a line
p = getCirclePos(self.pos, self.length, self.angle)
pygame.draw.line(self.SURF, self.color, self.pos, p)

# increment length and angle
self.length *= self.lengthmultiply

# apply length to position
vec = vecConvert(self.pos, p)
vecl = vecLen(vec)
if vecl != 0:
vec0 = vecMultiply(vec, 1. / vecl)
vecn = vecMultiply(vec0, self.length)
self.pos = vecGetPoint(self.pos, vecn)

# print out if the shape is nearly done
#if self.length <= 5:
#	print "!"

# render function
def render(self):
# blit and flip
self.SCREEN.blit(self.SURF, [0, 0])
pygame.display.flip()

# quits
def quit(self):
self.RUNNING = False

# takes a screenshot
def screenshot(self):
path = os.getcwd() + "/out/"

try:
if not os.path.isdir(path):
os.mkdir(path)

name = "img" + str(len(os.listdir(path))) + " (length*" + str(self.lengthmultiply) + " angle+" + str(self.angleadd) + ").png"
pygame.image.save(self.SURF, path + name)
except:
pass

# run function (uses tick() and render())
def run(self):
ticksPerSecond = 60
lastTime = time.time() * 1000000000
nsPerTick =  1000000000.0 / float(ticksPerSecond)

ticks = 0
frames = 0

lastTimer = time.time() * 1000
delta = 0.0

while self.RUNNING:
now = time.time() * 1000000000
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False

while delta >= 1:
ticks += 1
self.TICKS += 1
self.tick()
delta -= 1
shouldRender = True

if shouldRender:
frames += 1
self.render()

if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000

# debug
# print("Frames: " + str(frames) + ", ticks: " + str(ticks))

frames = 0
ticks = 0

# start game
GAME().run()
``````

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