Skip to content

Instantly share code, notes, and snippets.

@andikan
Last active September 6, 2015 06:21
Show Gist options
  • Save andikan/5cd0c2376d927d5e6f55 to your computer and use it in GitHub Desktop.
Save andikan/5cd0c2376d927d5e6f55 to your computer and use it in GitHub Desktop.
Ball.pde
class Ball {
float x;
float y;
int size;
PVector v;
color c;
Ball(float x, float y, int size) {
this.x = x;
this.y = y;
this.size = size;
this.v = new PVector();
}
void draw() {
pushStyle();
fill(c);
ellipse(x, y, size, size);
popStyle();
}
void update() {
if(millis() % 1 == 0) {
this.x = x + v.x;
this.y = y + v.y;
// println(v);
if(x > 1366 || x < 0) {
v = new PVector(-v.x, v.y);
}
if(y > 768 || y < 0) {
v = new PVector(v.x, -v.y);
}
}
}
void setColor(color c) {
this.c = c;
}
void setV(PVector v) {
this.v = v;
}
void stop() {
this.v = new PVector(0, 0);
}
}
setup() {
ball1 = new Ball(60, 60, 50);
ball2 = new Ball(1300, 700, 50);
}
draw() {
ball1.update();
ball1.draw();
}
// set speed
ball1.setV(new PVector(0, 5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment