{"id":20,"date":"2024-07-18T16:21:06","date_gmt":"2024-07-18T08:21:06","guid":{"rendered":"https:\/\/www.dyeea.com\/?p=20"},"modified":"2024-07-18T16:45:18","modified_gmt":"2024-07-18T08:45:18","slug":"javascript%e5%85%a5%e9%97%a8-%e4%bd%bf%e7%94%a8javascript%e7%bc%96%e5%86%99%e7%ae%80%e5%8d%95%e8%b4%aa%e5%90%83%e8%9b%87%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/www.dyeea.com\/?p=20","title":{"rendered":"JavaScript\u5165\u95e8\u2014\u2014\u4f7f\u7528JavaScript\u7f16\u5199\u7b80\u5355\u8d2a\u5403\u86c7\u6e38\u620f"},"content":{"rendered":"\n<p>\u95f2\u6765\u65e0\u804a\uff0c\u7528JavaScript\u5199\u4e86\u4e2a\u8d2a\u5403\u86c7\uff0c\u5e0c\u671b\u5bf9\u840c\u65b0\u7684\u5b66\u4e60\u80fd\u591f\u6709\u6240\u5e2e\u52a9\u3002<br>\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u5f00\u59cb\uff01<\/p>\n\n\n\n<p>\u529f\u80fd\u5206\u6790\uff1a<br>1.\u663e\u793a\u86c7\u548c\u98df\u7269<br>2.\u86c7\u7684\u79fb\u52a8<br>3.\u86c7\u5403\u5230\u98df\u7269\u540e\u8eab\u4f53\u52a0\u957f\uff0c\u5e76\u4ea7\u751f\u65b0\u7684\u98df\u7269<\/p>\n\n\n\n<p>\u9996\u5148\u5728HTML\u7684\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5927\u5c0f\u4e3a400*400\u7684\u753b\u5e03,\u4f7f\u7528canvas\u753b\u5e03\u76842D\u7ed8\u56fe\u65b9\u5f0f\u7ed8\u5236\u65b9\u5757\uff0c\u65b9\u5757\u8fde\u8d77\u6765\u8868\u793a\u86c7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;canvas id=\"myCanvas\" width=\"400\" height=\"400\" style=\"border:1px solid #000000;\"&gt;\n&lt;\/canvas&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">var c=document.getElementById(\"myCanvas\");\nvar ctx=c.getContext(\"2d\");<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u94fe\u8868\uff0c\u94fe\u8868\u6bcf\u4e2a\u8282\u70b9\u5b58\u50a8\u7684\u5185\u5bb9\u4e3a\u4e00\u7ec4(x,y)\u5750\u6807\u503c\uff0c\u6bcf\u4e2a\u8282\u70b9\u7528\u6765\u4fdd\u5b58\u86c7\u7684\u4e00\u4e2a\u8282\u70b9\u5728\u753b\u5e03\u4e0a\u7684\u4f4d\u7f6e\uff0c\u7ed8\u5236\u86c7\u4f53\u65f6\u53ea\u9700\u8981\u904d\u5386\u6b64\u94fe\u8868\uff0c\u4fbf\u53ef\u4ee5\u5c06\u6574\u6761\u86c7\u663e\u793a\u5728\u753b\u5e03\u4e0a\u3002<br>\u89e3\u91ca\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u8981\u7528\u94fe\u8868\u7ed3\u6784\u6765\u5b58\u50a8\uff0c\u56e0\u4e3a\u86c7\u5411\u524d\u79fb\u52a8\u4e00\u4e2a\u5355\u4f4d\uff0c\u5728\u6570\u636e\u5c42\u6b21\u53ef\u4ee5\u7406\u89e3\u4e3a\u5220\u9664\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u8868\u5934\u4e4b\u524d\u63d2\u5165\u4e00\u4e2a\u5b58\u50a8\u4fe1\u606f\u4e3a\u8868\u5934+1\u4e2a\u79fb\u52a8\u5355\u4f4d\u7684\u8282\u70b9\uff0c\u4e2d\u95f4\u8282\u70b9\u4e0d\u9700\u505a\u51fa\u6539\u53d8\uff0c\u4f7f\u7528\u94fe\u8868\u53ef\u4ee5\u5f88\u597d\u9002\u5e94\u8fd9\u6837\u7684\u64cd\u4f5c\u3002\u56e0\u4e3a\u94fe\u8868\u7684\u63d2\u5165\u548c\u5220\u9664\u662f\u5f88\u5bb9\u6613\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">node = function(x,y){\n\tthis.x=x;\n\tthis.y=y;\n\tthis.next=null;\n}\n\nvar head =new node(0,0);    \/\/\u94fe\u8868\u7684\u8868\u5934\uff0c\u4fdd\u5b58\u86c7\u5934\u7684\u4f4d\u7f6e\n\nsetLocation=function(node,nx,ny){ \/\/\u6539\u53d8\u8282\u70b9node\u4fdd\u5b58\u7684(x,y),\u4f4d\u7f6e\u503c\n\n    node.x=nx;\n    node.y=ny;\n\n}\nsetNext=function (node,next){ \/\/\u6539\u53d8\u5f53\u524d\u8282\u70b9\u6307\u5411\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n\n    node.next=next;\n\n}<\/code><\/pre>\n\n\n\n<p>\u60f3\u8981\u663e\u793a\u86c7\u4f53\uff0c\u53ea\u9700\u8981\u904d\u5386\u4fdd\u5b58\u4fe1\u606f\u7684\u94fe\u8868\uff0c\u7136\u540e\u518d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u7684\u4f4d\u7f6e\u753b\u4e00\u4e2a\u65b9\u5757<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">clearScreen = function(){ \/\/\u6e05\u7a7a\u5c4f\u5e55\uff0c\u91cd\u65b0\u7ed8\u5236\n\n    ctx.fillStyle=\"#FFFFFF\";\n    ctx.fillRect(0,0,400,400);\n\n}\n\ndrawSnake = function (){ \n\n    var temp = head; clearScreen(); \n    ctx.fillStyle=\"#0000FF\"; \n    ctx.fillRect(foodx,foody,19,19); \n    temp=temp.next; \/\/\u5148\u4e0d\u753b\u5934\uff0c\u5934\u8981\u6539\u53d8\u989c\u8272\u4fbf\u4e8e\u533a\u5206 \n    ctx.fillStyle=\"#00FF00\"; \n    while(temp.next!=null){ \n\n        ctx.fillRect(temp.x,temp.y,19,19); \n        temp=temp.next; \n\n    } \n    ctx.fillRect(temp.x,temp.y,19,19); \n    ctx.fillStyle=\"#FF0000\"; \/\/\u86c7\u5934\u662f\u7ea2\u8272\u7684 \n    ctx.fillRect(head.x,head.y,19,19); \n}<\/code><\/pre>\n\n\n\n<p>\u4e3a\u4e86\u5b9e\u73b0\u86c7\u4f53\u7684\u79fb\u52a8\uff0c\u8981\u5bf9\u94fe\u8868\u5b9e\u73b0\u4e24\u4e2a\u65b9\u6cd5\uff1a<br>1.\u5728\u8868\u5934\u4e4b\u524d\u63d2\u5165\u4e00\u4e2a\u5143\u7d20<br>2.\u5220\u9664\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20<br>\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">remove = function (){    \/\/\u5220\u9664\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n\tvar temple = head;\n\twhile(temple.next.next!=null){\n\t\ttemple=temple.next;\n\t}\n\t\n\ttemple.next=null;\n}\n\naddHead = function (x,y){    \/\/\u5728\u8868\u5934\u4e4b\u524d\u589e\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u5176\u4fdd\u5b58\u7684\u4f4d\u7f6e\u4e3ax,y\n\n\tvar temp = new node(head.x,head.y);\n\t\n\tsetNext(temp,head.next);\n\t\n\tsetNext(head ,temp);\n\t\n\tsetLocation(head,x,y);\n\t\n}<\/code><\/pre>\n\n\n\n<p>\u81f3\u6b64\uff0c\u5927\u529f\u544a\u6210\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95f2\u6765\u65e0\u804a\uff0c\u7528JavaScript\u5199\u4e86\u4e2a\u8d2a\u5403\u86c7\uff0c\u5e0c\u671b\u5bf9\u840c\u65b0\u7684\u5b66\u4e60\u80fd\u591f\u6709\u6240\u5e2e\u52a9\u3002\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u5f00\u59cb\uff01 \u529f\u80fd\u5206\u6790\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[8,10,9],"_links":{"self":[{"href":"https:\/\/www.dyeea.com\/index.php?rest_route=\/wp\/v2\/posts\/20"}],"collection":[{"href":"https:\/\/www.dyeea.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyeea.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyeea.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyeea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":4,"href":"https:\/\/www.dyeea.com\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":28,"href":"https:\/\/www.dyeea.com\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions\/28"}],"wp:attachment":[{"href":"https:\/\/www.dyeea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyeea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyeea.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}