{"id":507,"date":"2009-09-29T22:36:10","date_gmt":"2009-09-29T14:36:10","guid":{"rendered":"http:\/\/test999.eblhost.com\/blog\/?p=507"},"modified":"2009-11-17T11:01:06","modified_gmt":"2009-11-17T03:01:06","slug":"processing%ef%bc%9a%e6%9d%a1%e4%bb%b6%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/learn.travelchinawith.me\/?p=507","title":{"rendered":"Processing\uff1a\u6761\u4ef6\u5f0f"},"content":{"rendered":"<p>1. \u5e03\u5c14\u8868\u8fbe\u5f0f\u901a\u5e38\u88ab\u79f0\u4e3a\u6761\u4ef6\u5f0f\uff08conditionals\uff09\u3002<\/p>\n<p>2. \u6761\u4ef6\u5f0f\u94fe\uff08\u5404\u79cdelse if\u4fdd\u51c6\u8ba9\u4f60\u6655\u5934\u8f6c\u5411\uff09\uff1a<br \/>\n<em><span style=\"color: #808080;\"> if (boolean expression #1)  {<br \/>\n\/\/ \u5982\u679c\u5e03\u5c14\u8868\u8fbe\u5f0f #1 \u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801<br \/>\n}  else if (boolean expression #2)  {<br \/>\n\/\/ <\/span><\/em><em><span style=\"color: #808080;\">\u5982\u679c\u5e03\u5c14\u8868\u8fbe\u5f0f #2 \u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801<\/span><\/em><br \/>\n<em><span style=\"color: #808080;\"> }  else if (boolean expression #n)  {<br \/>\n\/\/ <\/span><\/em><em><span style=\"color: #808080;\">\u5982\u679c\u5e03\u5c14\u8868\u8fbe\u5f0f #n \u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801<\/span><\/em><br \/>\n<em><span style=\"color: #808080;\"> }  else  {<br \/>\n\/\/ \u5982\u679c\u4ee5\u4e0a\u5e03\u5c14\u8868\u8fbe\u5f0f\u90fd\u4e3a\u5047\u65f6\u6267\u884c\u7684\u4ee3\u7801<br \/>\n} <\/span><\/em><\/p>\n<p>3. \u8bb0\u4f4f\u4e00\u70b9\u5c31ok\uff0c\u5f53\u4f60\u7684\u4e00\u5806\u6761\u4ef6\u5f0f\u4e2d\u7684\u67d0\u6761\u4e3a\u771f\uff0c\u5219\u6b64\u6761\u76f8\u5e94\u7684\u4ee3\u7801\u5c06\u88ab\u6267\u884c\uff0c\u5269\u4f59\u7684\u6761\u4ef6\u5f0f\u540c\u65f6\u88ab\u5ffd\u7565\u3002<\/p>\n<p>4. \u4e00\u4e2a\u6761\u4ef6\u5f0f\u4e2d\u53ea\u80fd\u542b\u6709\u4e00\u5bf9if\u548celse\uff0c\u4f46\u5374\u53ef\u4ee5\u5305\u542b\u65e0\u9650\u4e2aelse if\u3002<\/p>\n<p>5. \u4e00\u4e2a\u7b49\u53f7\uff1a\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff1b\u4e24\u4e2a\u7b49\u53f7\uff1a\u67d0\u67d0\u548c\u67d0\u67d0\u76f8\u7b49\u3002<\/p>\n<p>6. constrain()\u51fd\u6570\u5e94\u7528<br \/>\n<em><span style=\"color: #808080;\"> if (r &gt; 255)  {<br \/>\nr = 255;<br \/>\n}  else if (r      &lt;      0)  {                   r      =      0;            } <\/span><\/em><br \/>\n\u7b49\u540c\u4e8e<br \/>\n<em><span style=\"color: #808080;\"> r  =  constrain(r,0,255); <\/span><\/em><\/p>\n<p>7. \u53d1\u73b0\u4e00\u70b9\uff0c\u5728\u505a\u4e00\u4e2a\u4f8b\u5b50\u7684\u65f6\u5019\uff1a\u65b9\u5757\u5411\u53f3\u6ed1\u52a8\uff0c\u5230X\u8f74\u7b49\u4e8e100\u7684\u65f6\u5019\u505c\u6b62\uff0c\u6211\u8fd9\u4e48\u5199\uff1a<br \/>\n<em><span style=\"color: #888888;\"> if (x&gt;99) {<\/span><\/em><br \/>\n<span style=\"color: #808080;\"><em> x=99;<br \/>\n} else {<br \/>\nx=x+1;}<br \/>\n}<\/em><\/span><em><br \/>\n<\/em> \u65b9\u5757\u505c\u662f\u505c\u4e0b\u6765\u4e86\uff0c\u4f46\u662f\u5374\u5728\u4e0d\u65ad\u5de6\u53f3\u98a4\u6296\uff0c\u540e\u6765\u6539\u6210\u8fd9\u6837\uff1a<br \/>\n<em><span style=\"color: #808080;\"> if (x&gt;99) {<br \/>\nx=100;<br \/>\n} else {<br \/>\nx=x+1;}<br \/>\n}<\/span><\/em><br \/>\n\u6b63\u5e38\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u8fd8\u662f<em><span style=\"color: #808080;\">x= constrain(x,0,100);<\/span><\/em><\/p>\n<p>8. \u7c7b\u4f3c&amp;&amp;\u4ee5\u53ca||\u8fd9\u4e9b\u903b\u8f91\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5199\u5728if\u7684\u540c\u4e00\u884c\uff0c\u4f8b\u5982if(mouseX &lt; 100 &amp;&amp; mouseY &lt; 100)<\/p>\n<figure id=\"attachment_508\" aria-describedby=\"caption-attachment-508\" style=\"width: 258px\" class=\"wp-caption alignright\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/09\/pr3-1.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-508 \" title=\"Exercise 5-6\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/09\/pr3-1.gif\" alt=\"Exercise 5-6\" width=\"258\" height=\"151\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/09\/pr3-1.gif 430w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/09\/pr3-1-300x175.gif 300w\" sizes=\"auto, (max-width: 258px) 100vw, 258px\" \/><\/a><figcaption id=\"caption-attachment-508\" class=\"wp-caption-text\">Exercise 5-6<\/figcaption><\/figure>\n<p>9. \u7ec3\u4e605_6\uff0c\u628a\u8fd0\u884c\u7a97\u53e3\u5206\u4e3a\u56db\u4e2a\u77e9\u5f62\uff0c\u9f20\u6807\u79bb\u5f00\u4e4b\u524d\u505c\u7559\u7684\u65b9\u5757\u540e\u989c\u8272\u7531\u767d\u6e10\u53d8\u5165\u9ed1\u3002\u4f4e\u667a\u5546\u7684\u6211\u6ca1\u6709\u65e5\u51fa\u6765\uff0c\u770b\u4e86\u7b54\u6848\u540e\u53d1\u89c9\u548c\u6211\u7684\u601d\u8def\u5b8c\u5168\u4e0d\u4e00\u6837\uff0c\u800c\u4e14\u6211\u603b\u89c9\u5f97\u6709\u5176\u5b83\u5b9e\u73b0\u65b9\u6cd5\uff08\u867d\u7136Daniel\u8001\u5934\u7684\u65b9\u6cd5\u4e5f\u5e76\u4e0d\u590d\u6742\u3002\u5b83\u7684\u5173\u952e\u70b9\uff1a\u8bbe\u7f6e\u6bcf\u4e2a\u65b9\u5757\u7684\u4eae\u5ea6\u4e00\u76f4\u5728\u9012\u51cf\uff0c\u6ce8\u610f\u662f\u4e00\u76f4\uff09\u3002<a href=\"http:\/\/www.learningprocessing.com\/exercise-files\/chapter05\/exercise_5_6.zip\">\u6e90\u6587\u4ef6<\/a>.<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">10. \u6309\u94ae\u7684\u7528\u6cd5\uff1a<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>boolean button = \u00a0false;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>int x = 50;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>int y \u00a0= 50;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>int w \u00a0 = 100;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>int h \u00a0= 75;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>void setup()\u00a0 {<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>size(200,200);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>void draw()\u00a0 {<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>if (mouseX &gt;\u00a0 x &amp;&amp; mouseX &lt; x +\u00a0 w &amp;&amp; mouseY &gt;\u00a0 y &amp;&amp; mouseY &lt;\u00a0 y + h &amp;&amp; mousePressed) {<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>button \u00a0 \u00a0 = \u00a0 \u00a0 \u00a0 true;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}\u00a0 else{<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>button \u00a0 \u00a0 = \u00a0 \u00a0 \u00a0 false;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>if (button)<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>{<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>background(255);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>stroke(0);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}\u00a0 else{<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>background(0);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>stroke(255);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>fill(175);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>rect(x,y,w,h);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">11. \u70b9\u4e00\u4e0b\uff0c\u6362\u80cc\u666f\u8272\uff0c\u518d\u70b9\u4e00\u4e0b\uff0c\u6362\u56de\u6765\uff1a<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">boolean button = false;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">int x = 50;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">int y = 50;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">int w = 100;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">int h = 75;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">void setup() {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">size(200,200);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">void draw() {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">if (button) {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">background(255);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">stroke(0);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">} else {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">background(0);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">stroke(255);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">fill(175);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">rect(x,y,w,h);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">\/\/ When the mouse is pressed, the state of the button is toggled.<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">\/\/ Try moving this code to draw() like in the rollover example.\u00a0 What goes wrong?<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">void mousePressed() {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">if (mouseX &gt; x &amp;&amp; mouseX &lt; x+w &amp;&amp; mouseY &gt; y &amp;&amp; mouseY &lt; y+h) {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">button = !button;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">12.\u00a0 \u4e00\u4e2a\u8bbe\u5b9a\u5e03\u5c14\u53d8\u91cf\u7684\u4f8b\u5b50\uff0c\u9f20\u6807\u70b9\u51fb\u540e\uff0c\u7403\u4ece\u5de6\u8fb9\u79fb\u52a8\u5230\u53f3\u8fb9\u3002\u5148\u8bbe\u5b9a\u5e03\u5c14\u53d8\u91cfgoing\u4e3a\u5047\uff0c\u518d\u8bbe\u5b9a\u9f20\u6807\u70b9\u51fb\u540e\uff08void mousePressed()\uff09going\u4e3a\u771f\uff0c\u7136\u540e\u8bbe\u5b9agoing\u4e3a\u771f\u65f6circleX=circleX+1;\u3002<a href=\"http:\/\/www.learningprocessing.com\/exercise-files\/chapter05\/exercise_5_8.zip\">\u6e90\u6587\u4ef6<\/a><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">13. \u8fd9\u91cc\u8bf4\u4e00\u4e0b\u5e03\u5c14\u53d8\u91cf\uff0c\u5e03\u5c14\u53d8\u91cf\u662f\u4e00\u4e2a\u53ea\u80fd\u4e3a\u771f\u6216\u5047\u7684\u53d8\u91cf\u3002\u7533\u660e\u65b9\u5f0f\u4e3aboolean+\u540d\u79f0=false\uff1b\uff08\u6216true\uff09<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">14. \u5728\u4f8b5-6\u4e2d\uff0c\u4ed6\u63d0\u51fa\u4e86\u4e00\u4e2a\u8d1f\u7684\u901f\u5ea6\u7684\u6982\u5ff5\uff0c\u610f\u601d\u5c31\u662f\u4e0e\u6b63\u7684\u901f\u5ea6\u76f8\u53cd\u8fd0\u884c\uff08\u5176\u5b9e\u5c31\u662f\u5f53x\u5230\u8fbe\u591a\u5c11\u7684\u65f6\u5019\uff0c\u5f00\u59cb\u9012\u51cf\uff09\uff0c\u8fd9\u5c31\u51fa\u73b0\u4e86\u4e00\u4e2a\u4e0d\u505c\u5de6\u53f3\u79fb\u52a8\u7684\u7403\uff1a<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">int x = 0;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">int speed = 1;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">void setup() {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">size(200,200);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">smooth();<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">void draw() {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">background(255);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">\/\/ Add the current speed to the x location.<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">x = x + speed;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">\/\/ Remember, || means &#8220;or.&#8221;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">if ((x &gt; width) || (x &lt; 0)) {<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">\/\/ If the object reaches either edge, multiply speed by -1 to turn it around.<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">speed = speed * -1;<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><em><span style=\"color: #808080;\"><br \/>\n<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">\/\/ Display circle at x location<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">stroke(0);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">fill(175);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">ellipse(x,100,32,32);<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><em><span style=\"color: #808080;\">}<\/span><\/em><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">15. <span style=\"color: #808080;\"><em>speed = speed * -1;<\/em><\/span>\u7b49\u540c\u4e8e<span style=\"color: #808080;\"><em>speed *= -1;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">16. \u73af\u73af\u76f8\u6263\u7684\u7ecf\u5178\u4f8b\u5b505-8\uff0c\u5b9e\u73b0\u4e00\u4e2a\u77e9\u5f62\u56f4\u7ed5\u8fd0\u884c\u7a97\u53e3\u8fb9\u6846\u8fd0\u52a8\u7684\u6548\u679c\u3002\u5de7\u5999\u7684\u501f\u7528\u4e86\u4e00\u4e2a\u53d8\u91cfstate\uff0c\u503c\u5f97\u597d\u597d\u5b66\u4e60\u73a9\u5473\u3002<a href=\"http:\/\/www.learningprocessing.com\/example-files\/chapter05\/zip\/example_5_8.zip\">\u6e90\u6587\u4ef6<\/a>\u3002\u4e3a\u4ec0\u4e48\u4e0d\u8bd5\u8bd5\u8ba9\u5b83\u53cd\u65b9\u5411\u8f6c\u52a8\u8d77\u6765\uff1f<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\">\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\">17. \u4f8b5-9\u662f\u4e00\u4e2a\u6a21\u62df\u91cd\u529b\u52a0\u901f\u5ea6\u7684\u4f8b\u5b50\uff1a<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>float x = 100; \u00a0 \/\/ x location of square<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>float y = 0; \u00a0 \u00a0 \/\/ y location of square<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>float speed = 0; \u00a0 \/\/ speed of square<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u5730\u5fc3\u5f15\u529b.<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u4e00\u822c\u6211\u4eec\u90fd\u9009\u7528\u4e00\u4e2a\u8f83\u5c0f\u7684\u503c\uff080.1\uff09\u52a0\u4ee5\u91cd\u529b\u52a0\u901f\u5ea6\u4e0a\u3002<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>float gravity = 0.1;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>void setup() {<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>size(200,200);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>void draw() {<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>background(255);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u753b\u77e9\u5f62<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>fill(175);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>stroke(0);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>rectMode(CENTER);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>rect(x,y,10,10);<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u5728y\u8f74\u4e0a\u52a0\u901f<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>y = y + speed;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u4e3a\u901f\u5ea6\u52a0\u4e0a\u52a0\u901f\u5ea6<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>speed = speed + gravity;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light; min-height: 12.0px;\"><span style=\"color: #808080;\"><em><br \/>\n<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u5982\u679c\u65b9\u5757\u5230\u5e95\uff0c\u5219\u8ba9\u901f\u5ea6\u53cd\u5411<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>if (y &gt; height) {<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u4e58\u4ee5 -0.95 \u53d6\u4ee3 -1 \u4f7f\u5f97\u65b9\u5757\u6bcf\u5f39\u8df3\u4e00\u4e0b\u90fd\u5728\u51cf\u901f.<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>\/\/ \u8fd9\u53eb\u505a\u201c\u201d\u56de\u6f6e\u201c\uff0c\u662f\u4e00\u79cd\u66f4\u771f\u5b9e\u7684\u6a21\u62df\u73b0\u5b9e\u6548\u679c\uff08\u5982\u679c\u6ca1\u6709\u5b83\uff0c\u65b9\u5757\u5c06\u6c38\u8fdc\u90fd\u5728\u5f39\u8df3\u800c\u4e0d\u4f1a\u505c\u6b62\uff09<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>speed = speed * -0.95;<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<\/em><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px STHeiti Light;\"><span style=\"color: #808080;\"><em>}<br \/>\n<span style=\"font-style: normal;\"> <\/span><\/em><\/span> \u540e\u6765\u6211\u628a\u5b83\u6539\u4e86\u4e00\u4e0b\uff0c\u6539\u6210\u4ece\u9f20\u6807\u70b9\u51fb\u70b9\u653e\u4e0b\u65b9\u5757\uff0c\u5f88\u6709\u610f\u601d\uff0c\u4f60\u8bd5\u8bd5\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5e03\u5c14\u8868\u8fbe\u5f0f\u901a\u5e38\u88ab\u79f0\u4e3a\u6761\u4ef6\u5f0f\uff08conditionals\uff09\u3002 2. \u6761\u4ef6\u5f0f\u94fe\uff08\u5404\u79cdelse if\u4fdd\u51c6\u8ba9\u4f60\u6655\u5934\u8f6c\u5411\uff09\uff1a if (boolean expression #1) { \/\/ \u5982\u679c\u5e03\u5c14\u8868\u8fbe\u5f0f #1 \u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801 } else if (boolean expression #2) { \/\/ \u5982\u679c\u5e03\u5c14\u8868\u8fbe\u5f0f #2 \u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801 } else if (boolean expression #n) { \/\/ \u5982\u679c\u5e03\u5c14\u8868\u8fbe\u5f0f #n \u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801 } else { \/\/ \u5982\u679c\u4ee5\u4e0a\u5e03\u5c14\u8868\u8fbe\u5f0f\u90fd\u4e3a\u5047\u65f6\u6267\u884c\u7684\u4ee3\u7801 } 3. \u8bb0\u4f4f\u4e00\u70b9\u5c31ok\uff0c\u5f53\u4f60\u7684\u4e00\u5806\u6761\u4ef6\u5f0f\u4e2d\u7684\u67d0\u6761\u4e3a\u771f\uff0c\u5219\u6b64\u6761\u76f8\u5e94\u7684\u4ee3\u7801\u5c06\u88ab\u6267\u884c\uff0c\u5269\u4f59\u7684\u6761\u4ef6\u5f0f\u540c\u65f6\u88ab\u5ffd\u7565\u3002 4. \u4e00\u4e2a\u6761\u4ef6\u5f0f\u4e2d\u53ea\u80fd\u542b\u6709\u4e00\u5bf9if\u548celse\uff0c\u4f46\u5374\u53ef\u4ee5\u5305\u542b\u65e0\u9650\u4e2aelse if\u3002 5. \u4e00\u4e2a\u7b49\u53f7\uff1a\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff1b\u4e24\u4e2a\u7b49\u53f7\uff1a\u67d0\u67d0\u548c\u67d0\u67d0\u76f8\u7b49\u3002 6. constrain()\u51fd\u6570\u5e94\u7528 if (r &gt; 255) [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[58],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-processing","tag-processing"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=507"}],"version-history":[{"count":8,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":767,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions\/767"}],"wp:attachment":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}