// Smiley class v2 // // Includes methods to draw and move a smiley face on screen // The smiley is scaled to the diameter // Smilies can move, also grow, shrink & spin // // In v2, Smileys are drawn to the mouse class Smiley{ // each face has an x & y position, x & y speed, and color // note array declaration syntax is different from Arduino // // objects supersede the complex type in Pascal - interesting int xPos ; int yPos ; int xSpeed ; int ySpeed ; int xDir ; int yDir ; int diameter ; int grow ; color theColor ; float angle ; float angleSpeed ; Smiley() { diameter = int(random(50)+50); xPos = int(random(theWidth-(diameter))+(diameter/2)); yPos = int(random(theHeight-(diameter))+(diameter/2)); xSpeed = int(random(5)+1); ySpeed = int(random(5)+1); if (random(1)>0.5) { xDir = 1; } else { xDir = -1; }; if (random(1)>0.5) { yDir = 1; } else { yDir = -1; }; if (random(1)>0.5) { grow = 1; } else { grow = -1; }; theColor = RandomColor(); angle = random(2*PI); angleSpeed = random(0.2)-0.1; } // Function to draw a face with given coords and color void DrawIt() { pushMatrix(); // save the coordinates translate(xPos, yPos); // move 0,0 to the smiley's position rotate(angle); // and turn int EyeSize = diameter/5 ; // scale the eyes based on diameter fill(theColor); noStroke(); ellipse(0,0,diameter,diameter); fill(0,0,0); ellipse(-EyeSize,-EyeSize,EyeSize,EyeSize); ellipse(EyeSize,-EyeSize,EyeSize,EyeSize); strokeWeight(EyeSize/2); stroke(0,0,0); noFill(); if (attracted == 1) { arc(0,0, diameter*.75, diameter*.75, PI*.15, PI*.85); } else { arc(0,diameter*.55, diameter*.75, diameter*.75, PI+PI*.3, PI+PI*.7); } popMatrix(); // return the original coordinates } void MoveIt() { xPos = xPos + (xSpeed*xDir) + attracted * ((mouseX-xPos)/30); // Test bounds, if hit edge reverse direction if (xPos >= theWidth - (diameter/2) || xPos <= (diameter/2)) { xPos = constrain(xPos, diameter/2, theWidth - (diameter/2)) ; xDir = -xDir; xSpeed = int(random(5)+1); ySpeed = int(random(5)+1); // change y speed as well theColor = RandomColor(); angleSpeed = random(0.2)-0.1; } // And the same thing vertically yPos = yPos + (ySpeed*yDir) + attracted * ((mouseY-yPos)/30); if (yPos >= theHeight - (diameter/2) || yPos <= (diameter/2)) { yPos = constrain(yPos, diameter/2, theHeight -(diameter/2)); yDir = -yDir; ySpeed = int(random(5)+1); xSpeed = int(random(5)+1); // change x speed as well theColor = RandomColor(); angleSpeed = random(0.2)-0.1; } // Update the face size diameter = diameter + grow; if (diameter < 40 || diameter > 110) { grow = -grow; }; // Update the angle angle = angle + angleSpeed % (2*PI); } }