Link to final product:
https://editor.p5js.org/tina06c/sketches/3uIi4PaI5
Description:
For this project I created the class tinaDancer which creates objects of dancing bears wearing a tutu and waving its arms up and down. Every time a new object is created the color of the bear’s tutu will be a different color.
Final Result:
Coding:
For this assignment I reused a previously created function that I created to draw the same bear. What’s different this time is that I removed the parameter s used to determine the scale of the bear and I added a rotate() function before the drawings of the arms of the bear in order to create the dancing motion of the bear. As shown below the rotate() function takes in the variables this.armR and this.armL which stores the degree at which each arm should rotate. The degree and pace at which the arms should rotate are determined by the update() function . In the update() function I used the modulo operation to have the right arm be rotated at -PI/4 and the left arm at its original position whenever the frameCount is a multiple of 10. I then included a counter variable to have the arms switch positions again when counter is a multiple of 6. This is my technique of having the arms of the bear swinging back and forth.
update() {
if (this.x < this.xMin || this.x > this.xMax) {
this.speedX = -this.speedX;
}
this.x += this.speedX;
if (frameCount % 10 == 0){
this.armR = -PI/4;
this.armL = 0;
this.counter ++;
}
if (this.counter % 6 == 0) {
this.armR = 0;
this.armL = PI/4;
}
}
display() {
//right arm
noStroke();
push();
fill(140, 108, 79);
translate(this.x, this.y);
rotate(this.armR);
ellipse(20, 40, 70, 30);
pop();
//left arm
push();
fill(140, 108, 79);
translate(this.x, this.y);
rotate(this.armL);
ellipse(-20, 40, 70, 30);
pop();
Reflections:
The benefit of the class not relying on code outside of its own definition is that I can create objects in other sketches from this class without worrying about other parameters other than the x and y coordinates. I no longer have to repeat the same long codes over and over again to retrieve the same bear with different tutu colors. What makes it challenging to write code that has to harmonize with the code other people have is the fact that the parameters have to be as concise as possible to make it easier to construct the objects in other sketches. In terms of reusability, I can reuse the file of the tinaDancer class over and over again without ever having to rewrite any of the code that I have already written. In terms of modularity, the different aspects of the bear are coded in different functions and as a whole they make up the dancing bear object. In the code we have a function that accounts for the movement of the bear (update()) and we have a code for the display of the bear (display()). The dancer is only whole when we combine and call both functions.