Last active
June 29, 2020 15:02
-
-
Save Ram-N/11e6f5017d0bf8cfe7d00b38ac112062 to your computer and use it in GitHub Desktop.
Processing_py - Balls criss-crossing
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 Ball(object): | |
def __init__(self,_id, _x, _y, _vx=0, _vy=0): | |
self.x, self.y = _x, _y | |
self.vx, self.vy = _vx, _vy | |
self.id = _id | |
#store the starting coords of each ball. For relaunching. | |
self.startx, self.starty = _x, _y | |
self.active = False | |
def move(self): | |
self.x += self.vx | |
self.y += self.vy | |
#check for out of bounds | |
if self.x > width or self.y > height or self.x <0 or self.y < 0: | |
self.x=self.startx | |
self.y=self.starty | |
def display(self): | |
ellipse(self.x, self.y, 20,20) |
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
w, h = 800, 800 | |
from ball import Ball | |
num_set = 8 #half the balls are horizontal, the other half will move vertically | |
num_balls = num_set * 2 | |
hballs = [Ball(id,0,h/2,5,0) for id in range(num_set)] | |
vballs = [Ball(id+num_set, w/2,h, 0,-5) for id in range(num_set)] | |
balls = [None]*(len(hballs)+len(vballs)) #placeholder | |
balls[::2] = hballs #odd places | |
balls[1::2] = vballs #even elements | |
num_active = 0 | |
launch_gap = num_balls | |
def setup(): | |
size(w,h) | |
background(127) | |
smooth() | |
noStroke() | |
def draw(): | |
global balls, num_active | |
background(127) | |
#when to launch? | |
if num_active < num_balls: | |
if not frameCount%(launch_gap): | |
balls[num_active].active = True #launch one more ball | |
num_active+=1 | |
#move each ball and show it on the screen | |
for b in balls: | |
if b.active: | |
b.move() | |
b.display() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These files are both Python Processing scripts. Save them to as your sketches and launch Processing to see the balls moving across your screen.
https://py.processing.org/