Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*Object Oriented Toy 
 ********Night Sky*******
 By Brent Pemberton
 to interact with the toy move the mouse over the large stars as the fall and enjoy the night sky */


Star star1;
Star star2;
Star star3;
Star star4;
SmallStar[] twinkle = new SmallStar[100];
color cursorColor = color(255, 255, 255);

void setup() {
  size(800, 600);
  star1=new Star(random(8, 21));
  star2=new Star(random(10, 18));
  star3=new Star(random(5, 10));
  star4=new Star(random(5, 14));
  for (int i=0; i <100; i++) {
    twinkle[i] = new SmallStar();
  }
}

void draw() {
  //*****UPDATE********
  star1.move();
  star2.move();
  star3.move();
  star4.move();

  //*******DISPLAY*******
  background(40);
  drawBackground();
  //loop that populates and displays the array
  for (int i=0; i <100; i++) {
    twinkle[i].display();
  }

  star1.display();
  star2.display();
  star3.display();
  star4.display();
  scenery();
  cursorPos();
}


void scenery() {
  noStroke();
  fill(0);
  rect(0, 550, width, 50);
  triangle(400, 500, 420, 600, 380, 600);
  ellipse(400, 500, 40, 40);
  triangle(400, 480, 350, 500, 450, 500);
  triangle(400, 460, 380, 500, 420, 500);
}

void cursorPos() {
  fill(cursorColor, 80);
  ellipse(mouseX, mouseY, 15, 15);
  fill(cursorColor, 60);
  ellipse(mouseX, mouseY, 30, 30);
  fill(cursorColor, 40);
  ellipse(mouseX, mouseY, 50, 50);
  fill(cursorColor, 25);
  ellipse(mouseX, mouseY, 75, 75);
}

void drawBackground() {
  int i = 0;
  while (i < height) {
    float r = 0.1025;
    float g = 0.1625;
    float b = 0.185;
    fill(4+i*r, 11+i*g, 15+i*b);
    rect(0, i, width, 4);
    i+=4;
  }
}
class SmallStar {  
  PVector smallStarPos=new PVector(0,0);


SmallStar() {
  smallStarPos.x=random(width);
  smallStarPos.y=random(height-50);
}
  
void display(){
  //ellipses around the small stars will change colors with the cursor
  fill(cursorColor,15);
  ellipse(smallStarPos.x,smallStarPos.y,50,50);
    beginShape();
 fill(255,255,255,random(100,200));
 noStroke();
 vertex(smallStarPos.x,smallStarPos.y-8);
 vertex(smallStarPos.x+2,smallStarPos.y-2);
 vertex(smallStarPos.x+6,smallStarPos.y);
 vertex(smallStarPos.x+2,smallStarPos.y+2);
 vertex(smallStarPos.x,smallStarPos.y+8);
 vertex(smallStarPos.x-2,smallStarPos.y+2);
 vertex(smallStarPos.x-6,smallStarPos.y);
 vertex(smallStarPos.x-2,smallStarPos.y-2);

 endShape(CLOSE);
}

}
class Star {

  PVector starPosition= new PVector(0, 0);
  float starVelocity;
  color starColor;

  Star(float speed) {
    starPosition.x=random(width);
    starPosition.y=0;
    starVelocity=speed;
    setRandomColor();
  }

  void move() {
    starPosition.y+=starVelocity;
    starPosition.x+=starVelocity/4;

    if (starPosition.y>height+40) {
       setRandomColor();
      starPosition.y=-40;
      starPosition.x=random(width);
     
    }
  }

  void setRandomColor() {
    starColor=color(random(255), random(255), random(255));
  }


  void display() {
    beginShape();
    stroke(starColor);
    fill(20);
    strokeWeight(3);
    vertex(starPosition.x, starPosition.y-50);
    vertex(starPosition.x+14, starPosition.y-20);
    vertex(starPosition.x+47, starPosition.y-15);
    vertex(starPosition.x+23, starPosition.y+7);
    vertex(starPosition.x+29, starPosition.y+40);
    vertex(starPosition.x, starPosition.y+25);
    vertex(starPosition.x-29, starPosition.y+40);
    vertex(starPosition.x-23, starPosition.y+7);
    vertex(starPosition.x-47, starPosition.y-15);
    vertex(starPosition.x-14, starPosition.y-20);
    endShape(CLOSE);

    if ((mouseX>starPosition.x-47) && (mouseX<starPosition.x+47) && (mouseY > starPosition.y-50) && (mouseY < starPosition.y+50)) {
      starPosition.x = random(width);
      starPosition.y=0;
      cursorColor = starColor;
    }
  }
}