First, a link to the full sketch on p5.js Web Editor
A screen recording and screenshot of the sketch is provided below:
This sketch adapts the code created in the”Imaginary Creatures Project” and converts it into a newly defined Class Mushroom (Refer to the code in the previous post). In the old sketch, I used various functions such as drawMushroom(), drawFaceAndDots(), and drawCircleLines() in order to achieve the shape of the mushroom. In addition, in the setup function, I had to initialize two arrays — one for the mushroom’s x position, the other for its y position– and then iterate over those arrays using a loop in the display( ) function. In said loop, the variables needed to be passed into the functions drawMushroom(), drawFaceAndDots(), and drawCircleLines(), such as the scale and scale width, colors, were assigned here. Finally, the functions used to actually draw the mushrooms could then be called inside the loop.
In this way, the code for the previous sketch is rather complex, since it relies on many arrays, a very complicated loop, and a multitude of data scattered across and outside the setup(), display() and my own user-defined functions. Consequently, this can allow room for quite a bit of mistakes, errors, and confusion when reading/writing/debugging the code.
However, in this new sketch, many of the flaws in the first version are fixed using OOP. All of the mushrooms variables — color, speed, x & y position, scale — instead of being scattered throughout the program, were converted into attributes (Class variables) in the Class constructor. The constructor function for Class Mushroom( ) is shown below:

Next, instead of separating the code that actually draws the mushroom into three different functions, the code is combined in Class Mushroom’s method called display(), which shows a still image of the mushroom. A portion of the display() method is shown below:

Finally, when it came to animating the drawing, while each of the function in the original version contained built-in functions that contained variables that changed, allowing for the movement, this new version does all of the animating in the update( ) method. The update( ) method allows the mushrooms arms to move up and down, its y-scale to oscillate, its size to grow and shrink, and its position to move up and down:

The final “dancer” is demonstrated below:
– The main benefit of having this class definition not rely on any other outside code is of course its limited room for error, because code will usually not get “lost” among other code. By keeping it organized and contained within the same space and scope, it becomes less likely for you or someone else working with your code to make mistakes when programming with the objects. Moreover, if you wish to change some attribute or method, you can do so easily within the Class definition, making it a “one-and-done” type of process.
– One difficulty I noticed, since creating Class Mushroom was only but a small part of a larger program, containing code from my other classmates, is that there can be less creativity. I was only allowed to create one Class definition, and in it could only exist two methods. While this allowed for better organization and understanding of OOP, it does limit the programmer’s ability to create more methods/attributes that might provide better logistics and results.
– After creating this mushroom dancer, my understanding of modularity is that it is essentially a fundamental aspect of OOP. OOP is so useful because each object acts like a mini module that has its own methods that the programmer can use freely. Additionally, they can be defined and used within the same program, or they can be imported outside of the program into a new one, just like other modules would such as math or random. Similarly, OOP has the property of reusability, in that the programmer can create as many instances of that class as they like, and methods or attributes can be changed and used again throughout the program.