Created
October 8, 2013 07:59
-
-
Save yoggy/6881185 to your computer and use it in GitHub Desktop.
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 { | |
| int x, y, dx, dy, r; | |
| Ball() { | |
| x = (int)random(0, width); | |
| y = (int)random(0, height); | |
| dx = (int)random(-10, 10); | |
| dy = (int)random(-10, 10); | |
| r = 10; | |
| } | |
| void update() { | |
| int nx = x + dx; | |
| int ny = y + dy; | |
| if (nx < 0 || width <= nx) dx = -dx; | |
| if (ny < 0 || width <= ny) dy = -dy; | |
| x = x + dx; | |
| y = y + dy; | |
| } | |
| void draw() { | |
| fill(#ffffff); | |
| stroke(#ffffff); | |
| ellipse(x, y, r, r); | |
| } | |
| } | |
| ArrayList<Ball> list; | |
| void setup() { | |
| size(400, 400); | |
| list = new ArrayList<Ball>(); | |
| for (int i = 0; i < 200; ++i) { | |
| list.add(new Ball()); | |
| } | |
| } | |
| void draw() { | |
| background(#000044); | |
| for (Ball b : list) { | |
| b.update(); | |
| b.draw(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment