# Circle Walk II

Continuing the idea from ‘Circle Walk’, I created ‘Circle Walk II’. In this program the entities get – based on their spawn time – a number (just to see the exact spawn time). As they spawn, they get put in a list. From this list their position around the center is calculated (in a similar way as in ‘Polygons’). Their distance to the center equals five times the number of entities ($\text{distance to the center} = 5 \cdot \text{number of entities}$), but cannot reach outside the screen. Their color is calculated based on their angle.

#### Usage

• ‘Space’ to toggle if text is shown

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of March, 2015
#         edited 30th of March, 2015
#     version II 1st  of April, 2015

# gets the scaled mouse position
def getMousePos():
return [pygame.mouse.get_pos()[0] / main.SCALE, pygame.mouse.get_pos()[1] / main.SCALE]

# 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'
def getCirclePos(_pos, _radius, _angle):
return [
]

# entity class
class entity():
def __init__(self, _startPos):
self.startPos = _startPos
self.pos = self.startPos[:]

self.angle = 0
self.distance = 200
self.destination = main.MIDDLEINT

self.color = [200, 200, 50]
self.num = main.TICKS

# render function
def render(self, _surface):
# render circle
p = [
int(self.pos[0]),
int(self.pos[1])
]
pygame.draw.circle(_surface, self.color, p, self.radius)

# render text
if main.SHOWTEXT:
text = main.FONT.render(str(self.num), 1, self.color)
rect = text.get_rect()
pos = [
1 + self.pos[0] + self.radius*.5,
1 + self.pos[1] + self.radius*.5
]
_surface.blit(text, pos)

# tick function
def tick(self):
# makes sure no entity gets outside the display
if self.distance + self.radius > main.WIDTH / 2.:
self.distance = main.WIDTH / 2. - self.radius
if self.distance + self.radius > main.HEIGHT / 2.:
self.distance = main.HEIGHT / 2. - self.radius

# calculate color
self.color = [
colorValid(2*self.angle),
0,
0
]

# update angle (to move along the path)
if self.angle >= 360:
self.angle = self.angle % 360

# update destination
self.destination = getCirclePos(main.MIDDLE, self.distance, self.angle)

# calculate distance to destination (for smooth movement)
dist = [
self.destination[0] - self.pos[0],
self.destination[1] - self.pos[1]
]
# move to destination (/ 5.0 defines the speed)
self.pos[0] += dist[0] / 5.0
self.pos[1] += dist[1] / 5.0

# tick function
def tick():

# handle events
for event in pygame.event.get():
# quit program
if event.type == pygame.QUIT:
sys.exit()

# keys
if event.type == pygame.KEYDOWN:
# toggle text show
if event.key == pygame.K_SPACE:
if main.SHOWTEXT:
main.SHOWTEXT = False
else:
main.SHOWTEXT = True

# spawn in new entities
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
main.ENTITIES.append(entity(getMousePos()))

# handle entities
# (and checks if two points are too near to each other)
if len(main.ENTITIES) > 0:
angleth = 360. / len(main.ENTITIES)

w = -1
for _ in main.ENTITIES:
_.distance = len(main.ENTITIES) * 5
w += 1
_.angle = (angleth * w) + (main.TICKS / 10.)
_.tick()

# render function
def render():
# fill
main.SCREEN.fill([0, 0, 0])
main.SURF.fill([50, 50, 100])

# render entities
for _ in main.ENTITIES:
_.render(main.SURF)

# scale and flip
main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
pygame.display.flip()

# 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

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

# init
import pygame, sys, time, math, os, random
pygame.font.init()

main = dummy()

main.TICKS = 0
main.WIDTH, main.HEIGHT = 1080, 720
main.MIDDLE = [main.WIDTH / 2.0, main.HEIGHT / 2.0]
main.MIDDLEINT = [main.WIDTH / 2, main.HEIGHT / 2]
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SURF = pygame.Surface(main.SIZE)

main.SCALE = 1
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]
main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Circle Walk II")

main.ENTITIES = []
run()