emailconfirmed
1,824
edits
|  (add thumb) |  (syntax hilighting) | ||
| (2 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
| [[File:My_robot.png|100x100px|thumb|left]] | [[File:My_robot.png|100x100px|thumb|left]] | ||
| <br style="clear:both"> | |||
| ==== Robot Code ==== | |||
| <source lang="python"> | |||
| """ | |||
| My little robot | |||
| """ | |||
| # Parameters | |||
| diameter = 400 | |||
| centerx = diameter / 2 | |||
| centery = diameter / 2 | |||
| boxsize = 40 | |||
| # Drawing style | |||
| def setup(): | |||
|     # Set up Canvas | |||
|     colorMode(RGB, 1) | |||
|     size(diameter, diameter) | |||
| def draw(): | |||
|     # increase boxsize with every single frame | |||
|     boxsize = 100 + 50 * sin(frameCount * 0.1) | |||
|     angle = PI/8 * sin(frameCount * 0.1) | |||
|     background(1, .8, .8) | |||
|     # define style | |||
|     fill(1, .5) | |||
|     rectMode(CENTER) | |||
|     fill(1, .5) | |||
|     stroke(1) | |||
|     strokeWeight(10) | |||
|     # Body | |||
|     rect(centerx, centery, boxsize, boxsize) | |||
|     # Arm positions | |||
|     position1x = centerx - boxsize / 2 | |||
|     position1y = centery - boxsize / 2 | |||
|     position2x = centerx + boxsize / 2 | |||
|     position2y = centery - boxsize / 2 | |||
|     # Right Arms | |||
|     drawArm(position2x, position2y, +1, angle - TWO_PI * 3/8) | |||
|     drawArm(position2x, position2y + boxsize/2, +1, angle) | |||
|     # Left Arms | |||
|     drawArm(position1x, position1y, -1, angle -TWO_PI * 3/8) | |||
|     drawArm(position1x, position1y + boxsize/2, -1, angle) | |||
|     # Head | |||
|     pushStyle() | |||
|     fill(.5, .5 ,1, .5) | |||
|     ellipse(centerx, centery - boxsize, boxsize, boxsize) | |||
|     popStyle() | |||
|     # saveFrame("my_robot_######.gif") | |||
|     # print(frameRate) | |||
| def drawArm(xpos, ypos, direction, angle = -TWO_PI /8): | |||
|     pushMatrix() | |||
|     translate(xpos, ypos) | |||
|     scale(direction, 1) | |||
|     rotate(angle) | |||
|     rectMode(CORNER) | |||
|     rect(0, 0, 20, 150) | |||
|     popMatrix() | |||
| </pre> | |||
| ==== While and If Else Code ==== | |||
| <pre> | |||
| d = 5 | |||
| size(200, 200) | |||
| colorMode(HSB, 1) | |||
| background(0) | |||
| strokeWeight(d-1) | |||
| x = 0 | |||
| # go all the way | |||
| while x < 200: | |||
|     # increase by strokeweight | |||
|     x = x + d | |||
|     # color based on x-position | |||
|     if x < 50: | |||
|         stroke(0, 1, 1) | |||
|     elif x < 100: | |||
|         stroke(0.2, 1, 1) | |||
|     elif x < 150: | |||
|         stroke(0.3, 1, 1) | |||
|     else: | |||
|         stroke(0.4, 1, 1) | |||
|     line(x, 0, x, 200) | |||
| </source> | |||