Skip to content

Instantly share code, notes, and snippets.

@hak8or
Created February 19, 2015 07:51
Show Gist options
  • Select an option

  • Save hak8or/d2dd418176138f92ace5 to your computer and use it in GitHub Desktop.

Select an option

Save hak8or/d2dd418176138f92ace5 to your computer and use it in GitHub Desktop.
Week 2
// Run once to setup the scene and program
void setup(){
// Make a view of 800 by 600 pixels. Should
// be made into a global val of sorts.
size(800,600);
// Enable AA
smooth();
}
// A ball with associated data.
class Ball {
// Positions
int x;
int y;
// Size
final int width = 20;
final int height = 20;
// Ball color
int red;
int green;
int blue;
// Velocity on axis.
int x_velocity;
int y_velocity;
// Default constructor sets the member vars
// to resonable randomish values.
Ball() {
x = int(mouseX);
y = int(mouseY);
red = int(random(255));
blue = int(random(255));
green = int(random(255));
// Only move something at most 10 pixels per draw.
// Note: This will eventually result in a ball that doesn't move.
x_velocity = int(random(20)) - 10;
y_velocity = int(random(20)) - 10;
}
// Update the position of the shape based on the velocity.
void update() {
x = x + x_velocity;
y = y + y_velocity;
}
// Draw the ball at its approriate position with the
// correct color.
void draw() {
fill(red, green, blue);
ellipse(x, y, width, height);
}
}
// Make an arraylist to keep track of all our shapes.
ArrayList<Ball> balls = new ArrayList<Ball>();
// Run this draw loop continuously.
void draw(){
// Wipe old view using a nice grayish background.
background(50, 50, 50);
// Update each balls position and draw it.
for (Ball ball : balls){
ball.update();
ball.draw();
}
// TODO remove balls that are no longer in view from
// the list of balls to draw.
}
// On every left mouse click, make a new ball.
// Note: This is not called when the mouse is also moving.
void mouseClicked(){
if (mouseButton == LEFT)
balls.add(new Ball());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment