xxxxxxxxxx
163
/*
sound example
4/14/2024
//sounds used //tree rustle
https://freesound.org/people/le_abbaye_Noirlac/sounds/129430/
// rain and traffic https://freesound.org/people/klankschap/sounds/54765/
*/
var bgMusic;
function preload() {
bgMusic = loadSound("rain-and-traffic-through-the-night.mp3");
}
function keyPressed() {
if (keyCode == 32) {
// space bar start
if (bgMusic.isPlaying()) {
bgMusic.pause();
} else {
bgMusic.play(); //space bar stop
}
}
}
let cartX = 0;
function setup() {
createCanvas(365, 400);
}
function draw() {
//;
background("white");
text("Hit the space bar to start", 20, 100);
if (bgMusic.isPlaying()) {
background("black");
fill("green");
noStroke();
rect(0, 300, width, 100);
//moving cart
drawCart(cartX, 200);
cartX += 2;
if (cartX > width) {
cartX = -200;
}
for (var x = 50; x <= width; x += 50) {
var y = random(-20, -140);
cloud(x, y, "brown");
}
cloud(100, 100, "pink");
cloud(200, 50, "purple");
cloud(300, 100, "orange");
tree(92, 220, 80);
//tree(200,220, 100, "yellow");
tree(289, 220, 200);
tree(400, 220, 300);
tree(500, 220, 400);
}
// Face
//noStroke();
//fill(250, 128, 114);
//ellipse(260, 255, 130, 240);
// Eyes
//fill(80, 30, 10);
//ellipse(210, 220, 38, 25);
//ellipse(310, 220, 38, 25);
// Pupils
//noStroke();
//fill(0);
//ellipse(207, 222, 8, 8);
//ellipse(306, 222, 8, 8);
// Mouth found through reference page
//noStroke();
//fill(255, 0, 255); // Fuchsia for the mouth
//arc(260, 280, 40, 15, 0, PI + //QUARTER_PI, CHORD);
// Nose
//fill(255, 200, 170);
//rect(255, 235, 10, 30, 145); // Rounded //nose
//the code below is Ai Generated
// Draw the buns using the function
//drawBuns(210, 175, 30, 60, "left"); // Left bun
//drawBuns(310, 175, 30, 60, "right"); // //Right bun
//}
//}
//}
//sound volume
var v = map(mouseY, 0, height, 1, 0);
bgMusic.setVolume(v);
var r = map(mouseX, 0, width, 0.1, 3, true);
bgMusic.rate(r);
//}
//}
//}
}
function drawCart(x, y) {
// Draw the bike frame
fill(255, 165, 0); // Orange frame
rect(x, y + 110, 80, 50, 10); // Main body
// wheels
fill(0); // Black wheels
ellipse(x + 20, 365, 30, 30); // Front wheel
ellipse(x + 65, y + 165, 30, 30); // Back wheel
//face
noStroke();
fill(250, 128, 114);
ellipse(x, y, 130, 240);
// Eyes
fill(80, 30, 10);
ellipse(x + 50, y - 40, 38, 25); //right eye
ellipse(x - 50, y - 40, 38, 25); //left eye
// Pupils
noStroke();
fill(0);
ellipse(x + 50, y - 40, 8, 8);
ellipse(x - 50, y - 40, 8, 8);
// Nose
fill(255, 200, 170);
rect(x, y - 20, 10, 30, 145); // Rounded nose
// Mouth found through reference page
noStroke();
fill(255, 0, 255); // Fuchsia for the mouth
arc(x + 10, y + 30, 40, 15, 0, PI + QUARTER_PI, CHORD);
//the code below is Ai Generated
// Draw the buns using the function
drawBuns(x - 40, y - 90, 30, 60, "left"); // Left bun
drawBuns(x + 40, y - 90, 30, 60, "right"); // Right bun
}
// the code below is Ai Generated Define function to draw buns this
function drawBuns(centerX, centerY, width, height, side) {
// Set fill color for the buns (dark brown)
fill(65, 40, 14);
// Calculate base positions for left and right buns
const leftBaseX = side === "left" ? centerX - width / 2 : centerX + width / 2;
const rightBaseX =
side === "left" ? centerX + width / 2 : centerX - width / 2;
// Draw the main part of the bun (rounded rectangle)
rect(leftBaseX - width / 2, centerY - height / 2, width, height, 20); // Adjust corner radius as needed
}
function cloud(x, y, c) {
fill(c);
circle(x, y, 50);
circle(x + 20, y + 20, 50);
circle(x - 20, y + 20, 50);
}
function tree(x, y) {
fill("brown");
rect(x, y, 20, 100, 5);
fill("green");
circle(x + 10, y, 80);
}