Files
sunny9898/task8/wk8/wk8_example5/wk8_example5.pde
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

67 lines
1.2 KiB
Plaintext

// based on an example at
// https://processing.org/tutorials/transform2d/
int armAngle = 0;
int angleChange = 5;
final int ANGLE_LIMIT = 135;
void setup()
{
size(200, 200);
smooth();
frameRate(30);
}
void draw()
{
fill(255, 40);
rect(0, 0, width, height);
pushMatrix();
translate(50, 50); // place robot so arms are always on screen
drawRobot();
armAngle += angleChange;
// if the arm has moved past its limit,
// reverse direction and set within limits.
if (armAngle > ANGLE_LIMIT || armAngle < 0)
{
angleChange = -angleChange;
armAngle += angleChange;
}
popMatrix();
}
void drawRobot()
{
noStroke();
fill(38, 38, 200);
rect(20, 0, 38, 30); // head
rect(14, 32, 50, 50); // body
drawLeftArm();
drawRightArm();
rect(22, 84, 16, 50); // left leg
rect(40, 84, 16, 50); // right leg
fill(222, 222, 249);
ellipse(30, 12, 12, 12); // left eye
ellipse(47, 12, 12, 12); // right eye
}
void drawLeftArm()
{
pushMatrix();
translate(12, 32);
rotate(radians(armAngle));
rect(-12, 0, 12, 37); // left arm
popMatrix();
}
void drawRightArm()
{
pushMatrix();
translate(66, 32);
rotate(radians(-armAngle));
rect(0, 0, 12, 37); // right arm
popMatrix();
}