Created
June 29, 2014 21:32
-
-
Save tildebyte/764d2b938a9e23b03f11 to your computer and use it in GitHub Desktop.
Creative Coding, Week 3 example 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Circle(object): | |
HalfWidth = 0 | |
def __init__(self, x, y, speed=random(10), phase=random(TAU)): | |
self.x = x | |
self.y = y | |
self.speed = speed | |
self.phase = phase | |
self.holdX = self.x | |
def _calcX(self): | |
if self.speed == 0: | |
pass | |
else: | |
self.x = (Circle.HalfWidth | |
+ sin(radians(frameCount * self.speed) + self.phase) | |
* 200) | |
def render(self, x, Radius): | |
self._calcX() | |
fill(self.x - 128, self.x - 64, self.x - 32) | |
ellipse(x, self.y, Radius, Radius) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
* Creative Coding | |
* Week 3, 02 - array with sin() | |
* | |
""" | |
from circle import Circle | |
NumCircles = 20 | |
Radius = 20 | |
SpeedFloor = 0.5 | |
circles = None | |
def setup(): | |
size(500, 500, P2D) | |
Circle.HalfWidth = width / 2 | |
gap = height / (NumCircles + 1) | |
circles = [Circle(0, gap * (i + 1), | |
SpeedFloor, phase(i)) | |
for i in range(NumCircles)] | |
def draw(): | |
background(128) | |
for circle in circles: | |
if circle.speed == 0: | |
circle.render(circle.holdX, Radius) | |
else: | |
circle.render(circle.x, Radius) | |
def phase(i): | |
return sin(width / 2 + radians(i)) + i * 0.5 | |
def mouseClicked(): | |
for circle in circles: | |
if (dist(mouseX, mouseY, circle.x, circle.y) <= Radius): | |
if circle.speed > 0: | |
circle.speed = 0 | |
circle.phase = 0 | |
circle.holdX = circle.x | |
break | |
elif circle.speed == 0: | |
circle.speed = random(SpeedFloor, 3) | |
circle.phase = random(TAU) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment