# Circle Splatter

Continuing to work with vectors (see ‘Circle Crawler’), I thought to include the mouse cursor. In this simulation there are 1×1 sized entities (400 of them) which create a circle around the center. By going near the entities with your mouse cursor, you push them away. Every entity has the urge to go back to its original position.

#### Usage

• ‘m’ toggles if entities move or stay

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

# modified version of 'Circle Walk II'
#       modified 17th of April, 2015
#                20th of April, 2015
#         edited  2nd of May  , 2015``````

``````# gets the distance between two points
def getDistance(p1, p2):
return int(math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2))

# easy vector functions
def convertVec(p1, p2):
return [p2[0] - p1[0], p2[1] - p1[1]]
def getLength(vec):
return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def multiplyVec(vec, n):
return [vec[0] * n, vec[1] * n]
def getPoint(vec, point):
return [point[0] + vec[0], point[1] + vec[1]]
return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# 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'
return [
]

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

self.number = _number
self.speed = 1

# tick function
def tick(self):
# 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] / main.ENTITIYSPEED
self.pos[1] += dist[1] / main.ENTITIYSPEED

# render function
def render(self, _surf):
pygame.draw.line(_surf, [50, 0, 0], self.pos, self.destination, main.THICKNESS)
pygame.draw.line(_surf, [255, 0, 0], self.pos, self.pos, main.THICKNESS)

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

# key handle
if event.type == pygame.KEYDOWN:
# entities moving
if event.key == pygame.K_m:
if main.MOVING:
main.MOVING = False
else:
main.MOVING = True

# screen refilling
if event.key == pygame.K_f:
if main.FILL:
main.FILL = False
else:
main.FILL = True

# tick entities
if True:
for _ in main.ENTITIES:
if main.MOVING:
_.tick()

# push entities away from cursor
p = getMousePos()
if getDistance(_.pos, p)  1:
pygame.draw.line(main.SURF, [255, 0, 0], l, main.ENTITIES[0].pos, main.THICKNESS)#"""

# 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.CENTER = [main.WIDTH / 2.0, main.HEIGHT / 2.0]
main.CENTERINT = [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 Splatter")

main.FILL = True
main.ENTITIYSPEED = 100.0

# entity init
n = 400
main.THICKNESS = 1
main.MOVING = True
main.ENTITIES = []
for _ in range(0, n):
p = getCirclePos(main.CENTER, 100, 360. / n * _)
main.ENTITIES.append(entity(p, _))

# start program
run()``````