Skip to content

Instantly share code, notes, and snippets.

@dinarname
Created November 8, 2018 11:52
Show Gist options
  • Save dinarname/cedd7a48342c0600bafa86dcca285d65 to your computer and use it in GitHub Desktop.
Save dinarname/cedd7a48342c0600bafa86dcca285d65 to your computer and use it in GitHub Desktop.
example of using classes
Ball b1, b2, b3, b4;
void setup() {
size(400, 400);
b1 = new Ball();
b2 = new Ball();
b3 = new Ball();
b4 = new Ball();
}
void draw() {
background(255);
b1.display();
b1.go();
b1.reflect();
b2.display();
b2.go();
b2.reflect();
b3.display();
b3.go();
b3.reflect();
b4.display();
b4.go();
b4.reflect();
}
class Ball {
float x, y, diametr;
float speedX, speedY;
Ball() {
x = random(0, width);
y = random(0, height);
diametr = random(5, 60);
speedX = random(1, 3);
speedY = random(1, 3);
}
void display () {
ellipse(x, y, diametr, diametr);
}
void go() {
x = x + speedX;
y = y + speedY;
}
void reflect() {
if (x < 0 || x > width) speedX = - speedX;
if (y < 0 || y > height) speedY = - speedY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment