Sunday, March 1, 2015

CSE Stars

In my previous post I mentioned that my class was learning about Arrays and how to use them. In order for us to get a better understanding of how Arrays worked my teacher had us take notes on how to create a series of stars that outward. Below is the notes on how to create  a series of stars that move outward using Arrays.

//Creating stars using arrays
//int = whole number
//float = decimal
//int size = 50;

int size = (int)random(250, 500);
float[] xStars = new float[size];
float[] yStars = new float[size];


void setup() {
 size(200, 200);
 background(0);
 stroke(255, 255, 200);

 for (int i = 0; i < size; i++) {
   xStars[i] = random(width);
   yStars[i] = random(height);
 }
}

void draw() {
 background(0);

 // draw the stars
 for (int i = 0; i < size; i++) {
   // update star position
   if (xStars[i] > width/2) {
     xStars[i]++;
   } else {
     xStars[i]--;
   }

   if (yStars[i] > height/2) {
     yStars[i]++;
   } else {
     yStars[i]--;
   }

   if (xStars[i] > width || xStars[i] < 0  ||
     yStars[i] > height || yStars[i] < 0) {
     xStars[i] = random(width);
     yStars[i] = random(height);
   }

   strokeWeight((int)random(2, 5));
   point(xStars[i], yStars[i]);
 }
}

void mousePressed() {
 for (int i = 0; i < size; i++) {
   xStars[i] = random(width);
   yStars[i] = random(height);
 }
}


No comments:

Post a Comment