Stars

Stars

For the 5th assignment for ICM we were expected to design a sketch in an object-oriented fashion, by eliminating all code from the main tab (setup() and draw()) except for the core requirements (size(), background(), etc.) and calls to objects.

I created a Star class and called stars as objects. Star class creates random colored stars and when you drag the mouse while it’s clicked the colors of the stars change randomly. You can find the video and the code of the sketch below:

Stars from Danara Sarioglu on Vimeo.


//variables

Star star1, star2, star3, star4, star5, star6, star7, star8, star9, star10;

void setup() {

size(900, 500);
background(0);
//create stars
star1=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star2=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star3=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star4=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star5=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star6=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star7=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star8=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star9=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
star10=new Star(int(random(2, width-22)), int(random(16, height-18)), int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
}

void draw() {

//erase background
background(0);
//draw the stars
star1.drawStar();
star2.drawStar();
star3.drawStar();
star4.drawStar();
star5.drawStar();
star6.drawStar();
star7.drawStar();
star8.drawStar();
star9.drawStar();
star10.drawStar();
}

void mousePressed() {
//check if star is pressed, if yes change location of the star randomly
star1.checkClicked();
star2.checkClicked();
star3.checkClicked();
star4.checkClicked();
star5.checkClicked();
star6.checkClicked();
star7.checkClicked();
star8.checkClicked();
star9.checkClicked();
star10.checkClicked();
}
void mouseDragged() {

//change color of stars
star1.changeColor();
star2.changeColor();
star3.changeColor();
star4.changeColor();
star5.changeColor();
star6.changeColor();
star7.changeColor();
star8.changeColor();
star9.changeColor();
star10.changeColor();
}

class Star {
int speedX=1;
int speedY=1;
int x,y,r,g,b;
Star(int _x, int _y, int _r, int _g, int _b){

x=_x;
y=_y;
r=_r;
g=_g;
b=_b;

}

void drawStar(){
strokeWeight(2);
stroke(r,g,b);

x=x+speedX;
y=y+speedY;
line(x,y,x+20,y-4);
line(x+20,y-4,x+4,y+16);
line(x+4,y+16,x+10,y-14);
line(x+10,y-14,x+16,y+12);
line(x+16,y+12,x,y);

if(x>=width-22||x<=2){
speedX=speedX*-1;

}
if(y>=height-18||y<=16){
speedY=speedY*-1;
}
print(speedX);
print(“,”);
println(x,y);
}

void checkClicked(){

if (mouseX<x+20&&mouseX>x&&mouseY<y+16&&mouseY>y-14){
x=int(random(2,width-22));
y=int(random(16,height-18));

}
}
void changeColor(){
r=int(random(0,255));
g=int(random(0,255));
b=int(random(0,255));

}
}