sketchDownload
//Nicholas Wong
//Section A
//nwong1@andrew.cmu.edu
//Assignment 10
var angle = 0; //Angle for clock
var catEyes = 0; //Cat eye size
var mousePos = 0; //Mouse position
var mousedx = 5; //Mouse speed
function preload()
{
//For use in web upload
catSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/cat.wav");
mouseSound =loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/mouse1.wav");
clockSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/clock.wav");
thunderSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/thunder-2.wav");
//For use in localhost
/*
catSound = loadSound("cat.wav");
clockSound = loadSound("clock.wav");
mouseSound = loadSound("mouse1.wav");
thunderSound = loadSound("thunder.wav");
*/
}
function soundSetup()
{ // setup for audio generation
catSound.setVolume(0.5);
mouseSound.setVolume(0.4);
clockSound.setVolume(0.7);
thunderSound.setVolume(1);
}
function setup() {
// you can change the next 2 lines:
createCanvas(480, 480);
createDiv("p5.dom.js library is loaded.");
useSound();
frameRate(1);
angleMode(DEGREES);
}
function draw()
{
background(140,90,50);
//Backdrop (window, room)
drawBackdrop();
//Frame dependant events
if(frameCount % 7 == 0)
{
clockSound.play();
}
if(frameCount % 12 == 0)
{
drawLightning() //Draw lightning for 1 frame
catEyes = 6; //Cat eyes are wide open
mousedx *= -1; //Mouse reverses direction
thunderSound.play(); //Thunder sound
catSound.play(); //Meow
}
//Window
windowLines(95,200);
windowLines(95,260);
//Objects
drawDrawer(330,height/2+100);
drawPainting(400,270);
drawClock(width/2 + 20,height/2);
//Cat
drawCat(340,310);
//Mouse
drawMouse(mousePos,430);
}
function drawCat(x,y)
{
//Cat artwork
push();
noStroke();
ellipseMode(CENTER);
fill(40);
ellipse(x+13,y+17,50,30);
ellipse(x-2,y+25,17,15);
ellipse(x+30,y+25,15,15);
fill(40);
ellipse(x,y,35,30);
triangle(x-15,y-8,x-5,y-10,x-10,y-23)
translate(18,0);
triangle(x-15,y-8,x-5,y-10,x-10,y-23)
fill(255)
//Cat eyes
ellipse(x-10,y,7,catEyes);
ellipse(x-26,y,7,catEyes);
pop();
//Cats eyes decrease in size every frame
catEyes -= 1;
//Cat eyes minimum size is 1;
if(catEyes <= 0)
{
catEyes = 1;
}
}
function drawMouse(x,y)
{
//Mouse artwork
push();
noStroke();
ellipse(x,y,20,10);
ellipse(x+8,y-3,4);
triangle(x+5,y-4,x+15,y,x+5,y+4);
stroke(255);
strokeWeight(2);
line(x,y+2,x-20,y+2);
stroke(0);
strokeWeight(2);
point(x+15,y);
strokeWeight(1.5);
point(x+9,y-1);
pop();
//Play squeak when mouse reaches x=0;
if(mousePos == 0)
{
mouseSound.play();
}
//Make mouse stop moving left after x=-15
if(mousePos <= -15)
{
mousePos = -15
}
//Add dx to mouse position
mousePos += mousedx;
}
function drawLightning()
{
//Lightning artwork
push();
translate(random(25,-25),0);
stroke(0,100,255);
strokeWeight(3);
line(90,142,107,160);
line(107,160,90,190);
line(90,190,105,220);
line(105,220,95,250);
line(95,250,107,280);
line(90,317,107,280);
stroke(255);
strokeWeight(1);
line(90,141,107,160);
line(107,160,90,190);
line(90,190,105,220);
line(105,220,95,250);
line(95,250,107,280);
line(90,317,107,280);
pop();
}
function drawPainting(x,y)
{
//Painting artwork
push();
noStroke();
rectMode(CENTER);
fill(120,60,0)
rect(x,y,120,75);
fill(220);
rect(x,y,110,65);
fill(0,170,190);
circle(x,y,30)
fill(180,0,150);
circle(x+30,y+10,20)
pop();
}
function drawBackdrop()
{
push();
noStroke();
//Floor
fill(120,70,10);
rect(0,400,width,80);
//Ceiling
fill(120,60,0)
rect(0,0,width,80);
//Window
rect(15,125,160,210);
fill(170,100,0);
rect(20,130,150,200);
//Sky
fill(52, 49, 69);
rect(30,140,130,180)
pop();
}
function windowLines(x,y)
{
//Window frame stuff
push();
noStroke();
fill(170,100,0);
rectMode(CENTER);
rect(x,y,150,7);
fill(100,50,0);
rect(x,y+3,130,2)
pop();
}
function drawDrawer(x,y)
{
push();
noStroke();
fill(170,100,0);
rect(x,y,143,70);
//Shadow
fill(115,65,0)
rect(x,y+70,143,70)
//Back
fill(190,120,0);
rect(x+7,y+12,130,20);
rect(x+7,y+40,130,20);
//Drawers
fill(220,150,0);
circle(x+35,y+22,10);
circle(x+105,y+22,10);
circle(x+35,y+50,10);
circle(x+105,y+50,10);
pop();
}
function drawClock(x,y)
{
push();
noStroke();
//Shadow
fill(115,65,0);
rect(x-25,y+150,52,100)
//Light
fill(220,150,0);
circle(x+3,y+70,50);
rect(x-22,y+70,50,100)
circle(x+3,y+55,35)
//Base
fill(200,120,0);
circle(x,y+70,50);
rect(x-25,y+70,50,100)
circle(x,y+55,35)
//Shades
fill(170,100,0);
circle(x,y+55,25);
circle(x,y+70,40);
rect(x-15,y+100,30,65);
ellipse(x,y+100,30,15);
//Clock face
fill(255);
circle(x,y+70,34);
fill(0);
circle(x,y+70,2);
//Clock Static Hand
stroke(100);
strokeWeight(1);
line(x,y+70,x,y+60)
//Clock Second Hand
angle+= 6 //Add 6 degrees every frame (second)
stroke(0);
strokeWeight(0.5);
let ax = x+cos(angle) * 14; //Polar x coordinates
let ay = y+sin(angle) * 14; //Polar y coordinates
line(x,y+70, ax, ay+70); //Draw clock hand
//Detail
stroke(235,180,0);
strokeWeight(2);
//Pendulum chime things
line(x,y+94,x,y+120+abs(10*sin(angle)));
line(x+4,y+94,x+4,y+100+abs(20*sin(angle)));
pop();
}
A mouse tries to sneak past a sleeping cat, but the lightning keeps waking the cat up.