CClab Mini Project 5: Imaginary Creature
Links
Adopt the Kitties! : https://editor.p5js.org/AndyHyprillion/sketches/m-mNVhGJZ
Introduction
Please adopt these special kitties. Because they have no whiskers to measure the distance, they always hit the wall. They shake their heads when they lost their sense of direction.
demo
Features
In this project, cats respawn in a certain position and start to move in different directions. They will shake their heads and turn their body around when they hit the “wall”. The position, the speed of movement, and the angle of shaking heads are totally random.
You can press your mouse button to slow down the kitties.
Analysis
To start with, the quantity of the cats is defined by the user. I use the prompt() to ask for the quantity value which is the parameter of the array loop.
To deal with a tremendous amount of variables, using an array is essential. In my code, I set 8 arrays. rot[] is for the maximum rotation rate. r[] is for checking the current rotation angle. back[] is a boolean array that determines the directions of shaking heads. phase[] is another boolean array that determines the directions of the body.
It’s interesting to achieve the effect by setting a set of if conditions. I use a new variable to store the value of rotation.
In addition, the direction of the cat’s body is achieved by a similar method. If the cat’s x coordinates are larger than the width of the canvas or smaller than the left edge, the phase will shift into the opposite value and make the translate() has a new value.
I also apply functions to the program. The main kitty() function can call the sub-functions to draw the body parts.
Reflections
To deal with a tremendous amount of variables, using an array is essential. In my code, I set 8 arrays. rot[] is for the maximum rotation rate. r[] is for checking the current rotation angle. back[] is a boolean array that determines the directions of shaking heads. phase[] is another boolean array that determines the directions of the body.
When using a function and drawing something in it, I will use push() and pop(). In this case, the changes and redirections in the function won’t affect the positions of things outside the function.
The User-defined function can reduce the duplication and complication of the codes obviously. Also, it can let the codes much easier to read and understand for humans.
Parameters in my function are phase and rot. They pass the specific value into the function and let the function output become different every time.
The use of function let me can develop each part of my project separately. This makes my project easier to develop continuing on a longer time scale.
——————————————-More Info——————————————
Links
Adopt the Kitties! : https://editor.p5js.org/AndyHyprillion/sketches/m-mNVhGJZ