{"id":204899,"date":"2025-05-29T09:41:05","date_gmt":"2025-05-29T01:41:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204899\/"},"modified":"2025-05-29T09:41:05","modified_gmt":"2025-05-29T01:41:05","slug":"%e7%94%a8-python-%e7%9a%84-turtle-%e6%a8%a1%e5%9d%97%e7%bb%98%e5%88%b6%e6%98%9f%e5%8f%b7%e7%bb%84%e6%88%90%e7%9a%84%e6%ad%a3%e6%96%b9%e5%bd%a2%ef%bc%8c%e5%ba%94%e8%af%a5%e6%80%8e%e4%b9%88%e5%81%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204899\/","title":{"rendered":"\u7528 Python \u7684 Turtle \u6a21\u5757\u7ed8\u5236\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7528 Python \u7684 Turtle \u6a21\u5757\u7ed8\u5236\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u7528 Python \u7684 Turtle \u6a21\u5757\u7ed8\u5236\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731976070673bdb86e8aea.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7528 python \u753b\u4e00\u4e2a\u7531\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62<\/strong><\/p>\n<p>\u5728 python \u7684 turtle \u6a21\u5757\u4e2d\uff0c\u8be5\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u7531\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62\u800c\u4e0d\u662f\u7ebf\u6761\u5462\uff1f<\/p>\n<p>\u4f7f\u7528\u9ed8\u8ba4\u7684 shape() \u51fd\u6570\u53ea\u80fd\u8bbe\u7f6e\u4e4c\u9f9f\u7684\u5f62\u72b6\u4e3a\u9884\u5b9a\u4e49\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\u7bad\u5934\u3001\u4e4c\u9f9f\u6216\u5708\uff0c\u65e0\u6cd5\u7ed8\u5236\u81ea\u5b9a\u4e49\u5f62\u72b6\u3002<\/p>\n<p>\u4e3a\u4e86\u7ed8\u5236\u4e00\u4e2a\u7531\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62\uff0c\u53ef\u4ee5\u81ea\u5df1\u624b\u52a8\u7ed8\u5236\u661f\u53f7\u6216\u76f4\u63a5\u5728\u6307\u5b9a\u4f4d\u7f6e\u5199\u4e0a\u661f\u53f7\u3002\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import turtle\nturtle.shape('classic')\nturtle.speed(10)\nturtle.setpos(0,0)\n\ndef make_star(pos, n):\n    turtle.penup()\n    turtle.setpos(pos)\n    turtle.pendown()\n    turtle.right(30)\n    turtle.forward(n)\n    turtle.back(n*0.5)\n    turtle.right(120)\n    turtle.forward(n*0.5)\n    turtle.back(n)\n    turtle.forward(n*0.5)\n    turtle.right(120)\n    turtle.forward(n*0.5)\n    turtle.back(n)\n    turtle.forward(n*0.5)\n    turtle.right(90)\n\ndef make_square(left_bottom, right_top, size = 10):\n    # left_top: \u5bf9\u89d2\u5de6\u4e0b\u9876\u70b9\u5750\u6807\n    # right_bottom: \u5bf9\u89d2\u53f3\u4e0a\u9876\u70b9\u5750\u6807\n    # size: *\u7684\u5927\u5c0f\n    x1, y1 = left_bottom\n    x2, y2 = right_top\n\n    if x1 &gt; x2:\n        raise Exception('left_bottom\u53c2\u6570\u9519\u8bef')\n    if y1 &gt; y2:\n        raise Exception('right_top\u53c2\u6570\u9519\u8bef')\n    if x2-x1 &lt; size*3:\n        raise Exception('size\u53c2\u6570\u9519\u8bef\uff0csize\u81f3\u591a\u662f\u8fb9\u957f\u7684\u4e09\u5206\u4e4b\u4e00\u957f\u5ea6')\n    if y2-y1 &lt; size*3:\n        raise Exception('size\u53c2\u6570\u9519\u8bef\uff0csize\u81f3\u591a\u662f\u8fb9\u957f\u7684\u4e09\u5206\u4e4b\u4e00\u957f\u5ea6')\n\n    # \u7b2c\u4e00\u6761\u8fb9 \u4e00\n    for i in range(x1, x2, size):\n        turtle.penup()\n        turtle.setpos((x1 + i, y1))\n        turtle.pendown()\n        turtle.write('*', font=size)\n\n    # \u7b2c\u4e8c\u6761\u8fb9 \u4e00 |\n    for i in range(y1, y2, size):\n        turtle.penup()\n        turtle.setpos((x2, y1 + i))\n        turtle.pendown()\n        turtle.write('*', font=size)\n\n    # \u7b2c\u4e09\u6761\u8fb9 \u4e00 | \u4e00\n    for i in range(x1, x2, size):\n        turtle.penup()\n        turtle.setpos((x2 - i, y2))\n        turtle.pendown()\n        turtle.write('*', font=size)\n\n    # \u7b2c\u56db\u6761\u8fb9 \u4e00 | \u4e00 |\n    for i in range(y1, y2, size):\n        turtle.penup()\n        turtle.setpos((x1, y2 - i))\n        turtle.pendown()\n        turtle.write('*', font=size)\n    \nmake_square((0,0), (100, 100), 10)\nturtle.done()<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u7528 Python \u7684 Turtle \u6a21\u5757\u7ed8\u5236\u661f\u53f7\u7ec4\u6210\u7684\u6b63\u65b9\u5f62\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 Python \u7684 Turtl&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204899","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204899"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204899\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}