Dancing Mr. Halloween Pumpkin
Description
In this mini project, I created a dancing halloween pumpkin that moves side to side while he do waves with his arms. The main goal of the project is to create object dancer by using class. Within the class, I used noise() function to make Mr.Pumpkin move side to side and created a loop with sin() and cos() functions to make the waving arms.
Link to my project: https://editor.p5js.org/SarahDing/sketches/j4FgIugpZ
Code
let dancer;
function setup() {
createCanvas(windowWidth, windowHeight);
dancer = new MrPumpkin(width/2, height/2);
}
function draw() {
background(0);
drawFloor();
dancer.update();
dancer.display();
}
class MrPumpkin{
constructor(startX, startY){
this.x = startX;
this.y = startY;
this. speed = random(0.003, 0.01);
this.s = 100;
}
update(){
//move left and right
this. x= noise(frameCount*this. speed)*height;
}
display(){
push();
translate(this.x, this.y);
// ******** //
//pumpkin body
noStroke();
fill(176, 86, 7);
ellipse(0, 0, this.s+25, this.s);
//eyes
fill(255, 239, 13);
triangle(-40,-15,-10,-15,-22.5, 10);//left
triangle(40, -15, 10, -15, 22.5, 10);//right
//nose
triangle(0, 8, -8, 20, 8, 20);
//mouth
arc(0, 27, 45, 30, 2*PI, PI);
//hat
//fill(83, 4, 105);
fill(4, 82, 4);
rect(-65, -40, this.s+30, 10, 20 );
rect(-40, -80, 80, 50, 10);
fill(0);
rect(-40,-60, 80, 10);
//arms
for(let i = 0; i<14; i++){
fill(176, 86, 7);
let a = 20* sin(frameCount*0.1 + i *0.2);
let b= 20* cos(frameCount*0.1 + i *0.2);
circle(this.s*0.6+i*2.5,a, 5+i/4);
push();
scale(-1,1);
circle(this.s*0.6+i*2.5, b, 5+i/4);
pop();
//legs
rect(-30, 40, 10, 40);
rect(25, 40, 10, 40);
ellipse(-33, 75, 25,10);
ellipse(38, 75, 25, 10);
}
pop();
}
}
Reflection
- What is the benefit of your class not relying on any code outside of its own definition?
- The code is more organized when the class does not rely on any code outside of it. All the code of an object is located in one block, which makes it easier for people to reuse the code within the same sketch or even in other places. Moreover, as all the variable are within the class, changing the variables will not affect other parts outside of the class.
- What make it challenging to write code that has to harmonize with the code other people have written?
- When we write code that has to harmonize with the code other people have written, we will have several limitations. For example in this project, we are limited to only use two parameters, this. x and this. y within our constructor. Beside these two parameters that initialize the central position, other possible parameters cannot be added to create a more complicated code. With the rules and limitation, we need to think of other ways to code our imagination while satisfying the rules.
- Describe the terms modularity and reusability using the example of your dancer class.
- Modularity is the quality of having smaller parts that combine into a whole, and changing one of the parts will not effect the entire functioning. For example, if I delete the code
this. x= noise(frameCount*this. speed)*height;
from the method update(), the rest of the code will still function and remain unchanged except for the animation part that is removed, and the code outside of my class still works the same way as it is. Moreover, if I have another class thanclass
MrPumpkin{}
, changing any part of the code within the class of the dancer will not affect codes outside the class. On the other hand, reusability is the quality of being able to use again or repeatedly. For example in this project, our professor can simply copy the code within and including the class of our dancer and put them all together to create multiple dancers on a single dance floor. The code within the class can be reused in any other projects as all the variables and methods are grouped in the same location.
- Modularity is the quality of having smaller parts that combine into a whole, and changing one of the parts will not effect the entire functioning. For example, if I delete the code