xxxxxxxxxx
64
let img;
let sound;
// Class for snowflakes
class Snowflake {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.speed = random(1, 3);
}
// Draw the snowflake
draw() {
noStroke();
fill(255);
ellipse(this.x, this.y, this.radius);
}
// Move the snowflake down the screen
move() {
this.y += this.speed;
// If the snowflake goes off the bottom of the screen, wrap it around to the top
if (this.y > height + this.radius) {
this.y = -this.radius;
}
}
}
// Define an array to hold the snowflakes
let snowflakes = [];
function setup() {
createCanvas(400, 400);
// Create 100 snowflakes and add them to the array
for (let i = 0; i < 100; i++) {
let x = random(width);
let y = random(height);
let radius = random(2, 8);
snowflakes.push(new Snowflake(x, y, radius));
}
}
function draw() {
background(img);
// Update and draw snowflake
for (let i = 0; i < snowflakes.length; i++) {
snowflakes[i].move();
snowflakes[i].draw();
}
}
//function to load background image and sound
function preload(){
soundFormats("mp3")
img = loadImage("bg.jpg")
sound= loadSound("snowsound.mp3")
}