# Prime Circle

Using circles to visualize prime appearance. The picture below is 1080 x 720 pixels, thus showing numbers up to 540 (on the far left and right).

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 26th of December, 2015``````

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

""" FUNCTIONS """
# checks if n is prime
def prime(n):
if n <= 1:
return False
else:
for _ in range(2, n-1):
if n % _ == 0:
return False

return True

""" GAME """
# game class
class GAME():
# initialize program
def __init__(self):
self.WIDTH, self.HEIGHT = 1080*3, 720*3
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.N = 1

# functions
pygame.display.set_caption("Prime Circle")

# tick function
def tick(self):
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.screenshot()

# draw circles
"""c = [255, 255, 255]
if prime(self.N):
c = [255, 0, 0]
pygame.draw.circle(self.SURF, c, [self.WIDTH/2, self.HEIGHT/2], self.N, 1)"""

if prime(self.N):
pygame.draw.circle(self.SURF, [255, 0, 0], [self.WIDTH/2, self.HEIGHT/2], self.N, 1)

self.N += 1

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