xxxxxxxxxx
124
let snowflakes = [];
function setup() {
createCanvas(800, 800);
fill(255);
noStroke();
}
function draw() {
//Background should be the first thing drawn
//p5 draws things in order, so if a rectangle is before (i.e. on line 11)
// and a circle is after (i.e. on line 12), the circle will be on top
if (mouseIsPressed) {
background(255);
} else
background(204, 229, 255);
let t = frameCount / 500; // snowflake speed
// create random snowflakes
for (let i = 0; i < random(9); i++) {
snowflakes.push(new snowflake());
}
// flake draw loops
for (let flake of snowflakes) {
flake.update(t); // snowflake position
flake.display(); // draw snowflake
}
// snowman body
fill(255);
ellipse(500, 870, 420);
fill(255);
ellipse(500, 600, 280);
fill(255);
ellipse(500, 400, 180);
if (mouseIsPressed) {
// Hat NIGHT TIME
fill(25);
rect(435, 200, 120, 120);
fill(25);
ellipse(494, 310, 220, 56);
} else {
// hat
fill(25);
rect(435, 200, 120, 120);
fill(25);
ellipse(494, 310, 220, 56);
}
// nose, just some coordinate problems (it's quite small right now tho, you should make it bigger)
// remember that (0, 0) is on the top-left
fill(255, 102, 9)
triangle(500, 400, 500, 410, 480, 405);
//heart buttons
text('💗',450,600);
// eyes, just some coordinate problems
fill(25);
ellipse(470, 370, 10, 10);
fill(25);
ellipse(520, 370, 10, 10);
//smile
fill(25);
//text, once again since the text is after everything else, it's going on top of everything
fill(255, 0, 0);
textSize(50);
text('Happy Valentines Day! 💘', 50, 50);
}
// snowflake class, I changed to a class, it's more appropriate than a function
class snowflake {
//the constructor happens everytime you create a new snowflake
constructor() {
// coordinates
this.posX = 0;
this.posY = random(-50, 0);
this.initialangle = random(0, 4);
this.size = random(2, 7);
// where snowflakes are positioned
this.radius = sqrt(random(pow(width / 1, 2)));
}
//you just need to write the function name when creating a function inside a class
update(time) {
// x position follows a circle
let w = 0.8; // angular speed
let angle = w * time + this.initialangle;
this.posX = width / 2 + this.radius * sin(angle);
// different size snowflakes fall at slightly different y speeds
this.posY += pow(this.size, 0.6);
// delete snowflake if past end of screen
if (this.posY > height) {
let ind = snowflakes.indexOf(this);
snowflakes.splice(ind, 1);
}
}
display() {
noStroke();
fill(255);
ellipse(this.posX, this.posY, this.size);
}
}