For my project this week, I decided to have my generative landscape be a sky with various sized stars. The stars are continuously generated and disappear at the end of the canvas in various sizes.
// Isabella Hong
// Section A
// ijhong@andrew.cmu.edu
// Project 10
var stars = [];
function setup() {
createCanvas(600, 400);
// create an initial collection of stars
for (var i = 0; i < 10; i++){
var rst = random(width);
stars[i] = makeStars(rst);
}
frameRate(30);
}
function draw() {
background(0);
updateandgeneratestars();
takeawaystars();
addnewstars();
basicForeground();
showStarNumber();
}
function updateandgeneratestars(){
for (var i = 0; i < stars.length; i++){
stars[i].move();
stars[i].display();
}
}
function takeawaystars(){
var starsToKeep = [];
for (var i = 0; i < stars.length; i++){
if (stars[i].x + stars[i].breadth > 0) {
starsToKeep.push(stars[i]);
}
}
stars = starsToKeep; // remember the remaining stars
}
function addnewstars() {
// With a very tiny probability, add a new star to the end.
var newStarProbability = 0.05;
if (random(0, 1) < newStarProbability) {
stars.push(makeStars(width));
}
}
// method to update position of stars every frame
function starsmove() {
this.x += this.speed;
}
// draw the stars
function showstars() {
var starHeight = 20;
fill(245);
noStroke();
push();
translate(this.x, height - 375);
rect(0, starHeight, 10, 10);
translate(this.x, height - 325);
ellipse(50, starHeight, 20, 20);
translate(this.x, height - 290);
rect(300, starHeight, 15, 15);
translate(this.x, height - 285);
ellipse(100, starHeight, 10, 10);
pop();
}
function makeStars(birthLocationX) {
var str = {x: birthLocationX,
breadth: 50,
speed: -1.0,
nstars: round(random(2,8)),
move: starsmove,
display: showstars}
return str;
}
function basicForeground() {
//creating the window frame
noFill();
strokeWeight(15);
stroke(101, 67, 33);
rectMode(CENTER);
rect(width / 2, height / 2, 585, 385);
line(width / 2, 0, width / 2, 400);
line(0, height / 2, 600, height / 2);
//little person looking out
strokeWeight(1);
stroke(0, 0, 128);
fill(0, 0, 128);
ellipse(width / 4, 380, 85, 100);
noStroke();
fill(255, 223, 180);
ellipse(width / 4, 300, 100, 100);
//speech bubble
stroke(0);
fill(255);
triangle(250, 280, 250, 305, 200, 305);
rect(345, 300, 250, 50);
}
function showStarNumber() {
noStroke();
fill(216, 182, 0);
var starNumber = "I see " + stars.length + " stars tonight, wow!"
textSize(16);
textFont("Georgia");
text(starNumber, 250, 305);
}