Skip to content

Instantly share code, notes, and snippets.

@todocono
Last active April 30, 2020 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todocono/7b48ba9b31547eb1320e60273920bbfb to your computer and use it in GitHub Desktop.
Save todocono/7b48ba9b31547eb1320e60273920bbfb to your computer and use it in GitHub Desktop.
/* IxLab 2020S - NYU Shanghai
* coded in class
* Ideas to practice:
*
* - make it move
* - add several other balls
* - have different ball colors
*/
class Ball {
// PROPERTIES = variables
float x, y;
float spdX, spdY;
int r, g, b; //color of the ball
// constructor
Ball () {
r = 125;
g = 125;
b = 125;
x = random(width);
y = random(height);
spdX = random(-10, 10);
spdY = random(-10, 10);
}
// METHOD = functions inside of an object
void display() {
fill(r, g, b);
ellipse (x, y, 20, 20);
}
void update() {
}
}
/*
float x, y;
float spdX, spdY;
int r, g, b; //color of the ball
float x2, y2;
float spdX2, spdY2;
int r2, g2, b2; //color of the ball 2
*/
Ball ball1;
void setup() {
size(400, 400);
noStroke();
ball1 = new Ball();
}
void draw() {
background(0);
ball1.display();
ball1.update();
}
/* this was for ball 1
void initialize() {
r = 125;
g = 125;
b = 125;
x = random(width);
y = random(height);
spdX = random(-10, 10);
spdY = random(-10, 10);
}
void update() {
x = x + spdX;
y = y + spdY;
//println(x); //this might come handy when "debugging"
if ((x > width) || (x < 0)) { // x is too big
spdX *= -1; // spdX = -spdX;
}
if ((y > height) || (y < 0)) { // x is too big
spdY *= -1;
}
}
void display() {
fill(r, g, b);
ellipse (x, y, 20, 20);
}
*/
/* this was for ball 2
void initialize2() {
r2 = 125;
g2 = 125;
b2 = 125;
x2 = random(width);
y2 = random(height);
spdX2 = random(-10, 10);
spdY2 = random(-10, 10);
}
void update2() {
x2 = x2 + spdX2;
y2 = y2 + spdY2;
//println(x); //this might come handy when "debugging"
if ((x2 > width) || (x2 < 0)) { // x is too big
spdX2 *= -1; // spdX = -spdX;
}
if ((y2 > height) || (y2 < 0)) { // x is too big
spdY2 *= -1;
}
}
void display2() {
fill(r2, g2, b2);
ellipse (x2, y2, 20, 20);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment