{"id":1161731,"date":"2025-01-13T19:19:58","date_gmt":"2025-01-13T11:19:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161731.html"},"modified":"2025-01-13T19:20:01","modified_gmt":"2025-01-13T11:20:01","slug":"python%e7%bb%98%e5%9b%be%e6%97%b6%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e6%b5%b7%e9%be%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161731.html","title":{"rendered":"python\u7ed8\u56fe\u65f6\u5982\u4f55\u663e\u793a\u6d77\u9f9f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202905\/af361b7d-f1cd-42a2-9b80-2153c99a3f2c.webp\" alt=\"python\u7ed8\u56fe\u65f6\u5982\u4f55\u663e\u793a\u6d77\u9f9f\" \/><\/p>\n<p><p> \u5728Python\u7ed8\u56fe\u65f6\u663e\u793a\u6d77\u9f9f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684turtle\u6a21\u5757\u3002<strong>\u5bfc\u5165turtle\u6a21\u5757\u3001\u521b\u5efa\u4e00\u4e2aScreen\u5bf9\u8c61\u3001\u521b\u5efa\u4e00\u4e2aTurtle\u5bf9\u8c61\u3001\u4f7f\u7528Turtle\u5bf9\u8c61\u7684\u65b9\u6cd5\u7ed8\u56fe\u3001\u663e\u793a\u7ed8\u56fe\u7ed3\u679c<\/strong>\u8fd9\u4e9b\u662f\u663e\u793a\u6d77\u9f9f\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u6b65\u9aa4\uff1a<strong>\u4f7f\u7528Turtle\u5bf9\u8c61\u7684\u65b9\u6cd5\u7ed8\u56fe<\/strong>\uff0c\u8fd9\u662f\u6838\u5fc3\u6b65\u9aa4\u4e4b\u4e00\u3002\u901a\u8fc7\u8c03\u7528Turtle\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u63a7\u5236\u6d77\u9f9f\u7684\u79fb\u52a8\u8f68\u8ff9\uff0c\u5e76\u7ed8\u5236\u51fa\u5404\u79cd\u56fe\u5f62\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0cturtle.forward()\u53ef\u4ee5\u8ba9\u6d77\u9f9f\u5411\u524d\u79fb\u52a8\u6307\u5b9a\u7684\u8ddd\u79bb\uff0c\u800cturtle.right()\u53ef\u4ee5\u8ba9\u6d77\u9f9f\u5411\u53f3\u65cb\u8f6c\u6307\u5b9a\u7684\u89d2\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165turtle\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528turtle\u6a21\u5757\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5c06\u5176\u5bfc\u5165\u3002turtle\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u6a21\u5757\u4e4b\u4e00\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u76f4\u63a5\u4f7f\u7528\u5373\u53ef\u3002\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\uff0c\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\u6765\u5bfc\u5165turtle\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5bfc\u5165turtle\u6a21\u5757\uff0c\u4f7f\u4f60\u80fd\u591f\u4f7f\u7528\u5176\u529f\u80fd\u6765\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2aScreen\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2aScreen\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3\u3002\u521b\u5efaScreen\u5bf9\u8c61\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">screen = turtle.Screen()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Screen\u5bf9\u8c61\u662f\u7ed8\u56fe\u7684\u7a97\u53e3\u73af\u5883\uff0c\u5305\u542b\u4e86\u7ed8\u56fe\u80cc\u666f\u3001\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u7b49\u5c5e\u6027\u3002\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3\u7684\u5404\u79cd\u53c2\u6570\uff0c\u4f8b\u5982\u80cc\u666f\u989c\u8272\u3001\u7a97\u53e3\u6807\u9898\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">screen.bgcolor(&quot;white&quot;)  # \u8bbe\u7f6e\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)  # \u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u4e00\u4e2aTurtle\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Screen\u5bf9\u8c61\u4e0a\u7ed8\u56fe\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aTurtle\u5bf9\u8c61\u3002Turtle\u5bf9\u8c61\u4ee3\u8868\u4e86\u4e00\u53ea\u865a\u62df\u7684\u201c\u6d77\u9f9f\u201d\uff0c\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u79fb\u52a8\u5e76\u7ed8\u5236\u56fe\u5f62\u3002\u521b\u5efaTurtle\u5bf9\u8c61\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = turtle.Turtle()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Turtle\u5bf9\u8c61t\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u6765\u63a7\u5236\u5176\u884c\u4e3a\uff0c\u4f8b\u5982\u79fb\u52a8\u3001\u65cb\u8f6c\u3001\u6539\u53d8\u989c\u8272\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Turtle\u5bf9\u8c61\u7684\u65b9\u6cd5\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u597dTurtle\u5bf9\u8c61\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u7ed8\u56fe\u3002Turtle\u5bf9\u8c61\u7684\u65b9\u6cd5\u79cd\u7c7b\u7e41\u591a\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u79fb\u52a8\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>t.forward(distance)<\/code>: \u5411\u524d\u79fb\u52a8\u6307\u5b9a\u8ddd\u79bb<\/li>\n<li><code>t.backward(distance)<\/code>: \u5411\u540e\u79fb\u52a8\u6307\u5b9a\u8ddd\u79bb<\/li>\n<li><code>t.left(angle)<\/code>: \u5411\u5de6\u65cb\u8f6c\u6307\u5b9a\u89d2\u5ea6<\/li>\n<li><code>t.right(angle)<\/code>: \u5411\u53f3\u65cb\u8f6c\u6307\u5b9a\u89d2\u5ea6<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7ed8\u56fe\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>t.penup()<\/code>: \u62ac\u8d77\u753b\u7b14\uff0c\u4e0d\u7ed8\u5236\u8f68\u8ff9<\/li>\n<li><code>t.pendown()<\/code>: \u653e\u4e0b\u753b\u7b14\uff0c\u7ed8\u5236\u8f68\u8ff9<\/li>\n<li><code>t.pensize(width)<\/code>: \u8bbe\u7f6e\u753b\u7b14\u5bbd\u5ea6<\/li>\n<li><code>t.pencolor(color)<\/code>: \u8bbe\u7f6e\u753b\u7b14\u989c\u8272<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>t.speed(speed)<\/code>: \u8bbe\u7f6e\u6d77\u9f9f\u7684\u79fb\u52a8\u901f\u5ea6<\/li>\n<li><code>t.shape(shape)<\/code>: \u8bbe\u7f6e\u6d77\u9f9f\u7684\u5f62\u72b6\uff08\u4f8b\u5982&quot;turtle&quot;, &quot;arrow&quot;, &quot;circle&quot;\u7b49\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7ed8\u5236\u4e00\u4e2a\u6b63\u65b9\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;black&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u6b63\u65b9\u5f62<\/strong><\/h2>\n<p>for _ in range(4):<\/p>\n<p>    t.forward(100)<\/p>\n<p>    t.right(90)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6d77\u9f9ft\u9996\u5148\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d\uff0c\u7136\u540e\u53f3\u8f6c90\u5ea6\uff0c\u91cd\u590d\u56db\u6b21\uff0c\u6700\u7ec8\u7ed8\u5236\u51fa\u4e00\u4e2a\u6b63\u65b9\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u793a\u7ed8\u56fe\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528turtle.done()\u65b9\u6cd5\u6765\u7ed3\u675f\u7ed8\u56fe\uff0c\u5e76\u663e\u793a\u7ed3\u679c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f7f\u7ed8\u56fe\u7a97\u53e3\u4fdd\u6301\u6253\u5f00\u72b6\u6001\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u7684turtle\u6a21\u5757\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5e76\u663e\u793a\u6d77\u9f9f\u7684\u8fd0\u52a8\u8f68\u8ff9\u3002turtle\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u7ed8\u56fe\u8fc7\u7a0b\u53d8\u5f97\u7b80\u5355\u800c\u6709\u8da3\u3002\u65e0\u8bba\u662f\u5b66\u4e60\u7f16\u7a0b\u8fd8\u662f\u8fdb\u884c\u56fe\u5f62\u521b\u4f5c\uff0cturtle\u6a21\u5757\u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed8\u5236\u590d\u6742\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u5c55\u793aturtle\u6a21\u5757\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u7ed8\u5236\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u56fe\u5f62\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528turtle\u6a21\u5757\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ed8\u5236\u591a\u8fb9\u5f62<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u6b63\u4e94\u8fb9\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;blue&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(2)<\/p>\n<h2><strong>\u7ed8\u5236\u6b63\u4e94\u8fb9\u5f62<\/strong><\/h2>\n<p>sides = 5<\/p>\n<p>length = 100<\/p>\n<p>angle = 360 \/ sides<\/p>\n<p>for _ in range(sides):<\/p>\n<p>    t.forward(length)<\/p>\n<p>    t.right(angle)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u5faa\u73af\u6765\u91cd\u590d\u7ed8\u5236\u591a\u8fb9\u5f62\u7684\u6bcf\u4e00\u6761\u8fb9\u3002\u901a\u8fc7\u8c03\u6574sides\u548clength\u7684\u503c\uff0c\u53ef\u4ee5\u7ed8\u5236\u4e0d\u540c\u7684\u591a\u8fb9\u5f62\u3002<\/p>\n<\/p>\n<p><h4>2. \u7ed8\u5236\u661f\u5f62<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u4e94\u89d2\u661f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;red&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(2)<\/p>\n<h2><strong>\u7ed8\u5236\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>for _ in range(5):<\/p>\n<p>    t.forward(100)<\/p>\n<p>    t.right(144)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u6574\u65cb\u8f6c\u89d2\u5ea6\u6765\u7ed8\u5236\u4e00\u4e2a\u4e94\u89d2\u661f\u3002\u4e94\u89d2\u661f\u7684\u6bcf\u4e2a\u89d2\u5ea6\u4e3a144\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528t.right(144)\u6765\u5b9e\u73b0\u65cb\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>3. \u7ed8\u5236\u87ba\u65cb\u5f62<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u87ba\u65cb\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;green&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(10)<\/p>\n<h2><strong>\u7ed8\u5236\u87ba\u65cb\u5f62<\/strong><\/h2>\n<p>for i in range(100):<\/p>\n<p>    t.forward(i * 2)<\/p>\n<p>    t.right(45)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u9010\u6e10\u589e\u52a0\u79fb\u52a8\u8ddd\u79bb\uff0c\u5e76\u901a\u8fc7\u65cb\u8f6c\u89d2\u5ea6\u6765\u521b\u5efa\u87ba\u65cb\u5f62\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed8\u5236\u5206\u5f62\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u5206\u5f62\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u7ed8\u5236\u5206\u5f62\u6811\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;brown&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(0)<\/p>\n<h2><strong>\u7ed8\u5236\u5206\u5f62\u6811<\/strong><\/h2>\n<p>def draw_branch(length, level):<\/p>\n<p>    if level &gt; 0:<\/p>\n<p>        t.forward(length)<\/p>\n<p>        t.right(20)<\/p>\n<p>        draw_branch(length * 0.7, level - 1)<\/p>\n<p>        t.left(40)<\/p>\n<p>        draw_branch(length * 0.7, level - 1)<\/p>\n<p>        t.right(20)<\/p>\n<p>        t.backward(length)<\/p>\n<h2><strong>\u521d\u59cb\u4f4d\u7f6e<\/strong><\/h2>\n<p>t.left(90)<\/p>\n<p>t.penup()<\/p>\n<p>t.goto(0, -200)<\/p>\n<p>t.pendown()<\/p>\n<h2><strong>\u8c03\u7528\u9012\u5f52\u51fd\u6570\u7ed8\u5236\u6811<\/strong><\/h2>\n<p>draw_branch(100, 5)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u7ed8\u5236\u5206\u5f62\u6811\u3002\u9012\u5f52\u51fd\u6570draw_branch()\u6839\u636e\u5f53\u524d\u5c42\u7ea7level\u548c\u5206\u652f\u957f\u5ea6length\u7ed8\u5236\u6811\u7684\u5206\u652f\u3002\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff0c\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u590d\u6742\u7684\u5206\u5f62\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4ea4\u4e92\u5f0f\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u8fd8\u652f\u6301\u7528\u6237\u4ea4\u4e92\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u952e\u76d8\u4e8b\u4ef6\u6765\u63a7\u5236\u6d77\u9f9f\u7684\u79fb\u52a8\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u7bad\u5934\u952e\u6765\u63a7\u5236\u6d77\u9f9f\u7684\u65b9\u5411\u548c\u79fb\u52a8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;black&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u5b9a\u4e49\u63a7\u5236\u51fd\u6570<\/strong><\/h2>\n<p>def move_forward():<\/p>\n<p>    t.forward(30)<\/p>\n<p>def turn_left():<\/p>\n<p>    t.left(45)<\/p>\n<p>def turn_right():<\/p>\n<p>    t.right(45)<\/p>\n<h2><strong>\u7ed1\u5b9a\u952e\u76d8\u4e8b\u4ef6<\/strong><\/h2>\n<p>screen.listen()<\/p>\n<p>screen.onkey(move_forward, &quot;Up&quot;)<\/p>\n<p>screen.onkey(turn_left, &quot;Left&quot;)<\/p>\n<p>screen.onkey(turn_right, &quot;Right&quot;)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u51fd\u6570move_forward()\u3001turn_left()\u548cturn_right()\uff0c\u5206\u522b\u7528\u4e8e\u63a7\u5236\u6d77\u9f9f\u7684\u524d\u8fdb\u548c\u8f6c\u5411\u3002\u7136\u540e\uff0c\u901a\u8fc7screen.onkey()\u65b9\u6cd5\u5c06\u8fd9\u4e9b\u51fd\u6570\u7ed1\u5b9a\u5230\u76f8\u5e94\u7684\u952e\u76d8\u4e8b\u4ef6\u4e0a\uff0c\u4f7f\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u952e\u6765\u63a7\u5236\u6d77\u9f9f\u7684\u79fb\u52a8\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed8\u5236\u6587\u672c\u548c\u5f62\u72b6<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u8fd8\u652f\u6301\u5728\u7ed8\u56fe\u7a97\u53e3\u4e2d\u7ed8\u5236\u6587\u672c\u548c\u5404\u79cd\u5f62\u72b6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7ed8\u5236\u6587\u672c\u548c\u5f62\u72b6\uff1a<\/p>\n<\/p>\n<p><h4>1. \u7ed8\u5236\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u7ed8\u56fe\u7a97\u53e3\u4e2d\u7ed8\u5236\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;black&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>t.penup()<\/p>\n<p>t.goto(0, 0)<\/p>\n<p>t.pendown()<\/p>\n<p>t.write(&quot;Hello, Turtle!&quot;, align=&quot;center&quot;, font=(&quot;Arial&quot;, 24, &quot;normal&quot;))<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528t.write()\u65b9\u6cd5\u5728\u7ed8\u56fe\u7a97\u53e3\u4e2d\u7ed8\u5236\u6587\u672c\u3002align\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u6587\u672c\u7684\u5bf9\u9f50\u65b9\u5f0f\uff0cfont\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u6587\u672c\u7684\u5b57\u4f53\u3001\u5927\u5c0f\u548c\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2. \u7ed8\u5236\u5706\u5f62<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;blue&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u5706\u5f62<\/strong><\/h2>\n<p>t.circle(50)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528t.circle()\u65b9\u6cd5\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\u3002\u53c2\u657050\u8868\u793a\u5706\u7684\u534a\u5f84\u3002<\/p>\n<\/p>\n<p><h4>3. \u7ed8\u5236\u692d\u5706<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u692d\u5706\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;green&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u692d\u5706<\/strong><\/h2>\n<p>t.penup()<\/p>\n<p>t.goto(0, -50)<\/p>\n<p>t.pendown()<\/p>\n<p>for _ in range(2):<\/p>\n<p>    t.circle(100, 90)<\/p>\n<p>    t.circle(50, 90)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7ec4\u5408\u4e24\u4e2a\u534a\u5706\u6765\u7ed8\u5236\u4e00\u4e2a\u692d\u5706\u3002t.circle()\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u534a\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u7ed8\u5236\u7684\u89d2\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4fdd\u5b58\u7ed8\u56fe\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u4fdd\u5b58\u7ed8\u56fe\u7ed3\u679c\u4e3a\u56fe\u50cf\u6587\u4ef6\u7684\u529f\u80fd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Python\u7684Pillow\u5e93\u4fdd\u5b58\u7ed8\u56fe\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4fdd\u5b58\u7ed8\u56fe\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;black&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u6b63\u65b9\u5f62<\/strong><\/h2>\n<p>for _ in range(4):<\/p>\n<p>    t.forward(100)<\/p>\n<p>    t.right(90)<\/p>\n<h2><strong>\u4fdd\u5b58\u7ed8\u56fe\u7ed3\u679c<\/strong><\/h2>\n<p>canvas = screen.getcanvas()<\/p>\n<p>canvas.postscript(file=&quot;turtle_graphics.eps&quot;)<\/p>\n<h2><strong>\u4f7f\u7528Pillow\u5e93\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&quot;turtle_graphics.eps&quot;)<\/p>\n<p>img.save(&quot;turtle_graphics.png&quot;)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528canvas.postscript()\u65b9\u6cd5\u5c06\u7ed8\u56fe\u7ed3\u679c\u4fdd\u5b58\u4e3aEPS\u6587\u4ef6\u3002\u7136\u540e\uff0c\u4f7f\u7528Pillow\u5e93\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u6269\u5c55\u548c\u5b9a\u5236\u6d77\u9f9f\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f46\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6269\u5c55\u548c\u5b9a\u5236\u8fd9\u4e9b\u529f\u80fd\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u6269\u5c55\u548c\u5b9a\u5236\u6d77\u9f9f\u7ed8\u56fe\uff1a<\/p>\n<\/p>\n<p><h4>1. \u81ea\u5b9a\u4e49\u6d77\u9f9f\u5f62\u72b6<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f62\u72b6\u6765\u66ff\u6362\u9ed8\u8ba4\u7684\u6d77\u9f9f\u5f62\u72b6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f62\u72b6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u5f62\u72b6<\/strong><\/h2>\n<p>screen.register_shape(&quot;custom_shape&quot;, ((-10, 0), (0, 10), (10, 0), (0, -10)))<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.shape(&quot;custom_shape&quot;)<\/p>\n<p>t.pencolor(&quot;black&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>for _ in range(4):<\/p>\n<p>    t.forward(100)<\/p>\n<p>    t.right(90)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528screen.register_shape()\u65b9\u6cd5\u6ce8\u518c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f62\u72b6\u3002\u7136\u540e\uff0c\u901a\u8fc7t.shape()\u65b9\u6cd5\u8bbe\u7f6e\u6d77\u9f9f\u7684\u5f62\u72b6\u4e3a\u81ea\u5b9a\u4e49\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><h4>2. \u6269\u5c55Turtle\u7c7b<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fTurtle\u7c7b\u6765\u521b\u5efa\u81ea\u5df1\u7684\u6d77\u9f9f\u7c7b\uff0c\u4ee5\u4fbf\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u6269\u5c55Turtle\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efa\u81ea\u5b9a\u4e49Turtle\u7c7b<\/strong><\/h2>\n<p>class CustomTurtle(turtle.Turtle):<\/p>\n<p>    def draw_star(self, size):<\/p>\n<p>        for _ in range(5):<\/p>\n<p>            self.forward(size)<\/p>\n<p>            self.right(144)<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaCustomTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = CustomTurtle()<\/p>\n<p>t.pencolor(&quot;red&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u65b9\u6cd5\u7ed8\u5236\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>t.draw_star(100)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aCustomTurtle\u7c7b\uff0c\u7ee7\u627f\u81eaTurtle\u7c7b\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2adraw_star()\u65b9\u6cd5\u7528\u4e8e\u7ed8\u5236\u4e94\u89d2\u661f\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aCustomTurtle\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u81ea\u5b9a\u4e49\u65b9\u6cd5\u7ed8\u5236\u4e94\u89d2\u661f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u52a8\u753b\u6548\u679c\u548c\u591a\u6d77\u9f9f<\/h3>\n<\/p>\n<p><p>turtle\u6a21\u5757\u8fd8\u652f\u6301\u521b\u5efa\u52a8\u753b\u6548\u679c\u548c\u4f7f\u7528\u591a\u4e2a\u6d77\u9f9f\u5bf9\u8c61\u6765\u540c\u65f6\u7ed8\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u52a8\u753b\u6548\u679c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u753b\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pencolor(&quot;blue&quot;)<\/p>\n<p>t.pensize(2)<\/p>\n<p>t.speed(1)<\/p>\n<h2><strong>\u521b\u5efa\u52a8\u753b\u6548\u679c<\/strong><\/h2>\n<p>for _ in range(36):<\/p>\n<p>    t.forward(100)<\/p>\n<p>    t.right(170)<\/p>\n<p>    time.sleep(0.1)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528time.sleep()\u65b9\u6cd5\u6765\u521b\u5efa\u52a8\u753b\u6548\u679c\u3002\u6d77\u9f9f\u5728\u6bcf\u6b21\u79fb\u52a8\u540e\u6682\u505c0.1\u79d2\uff0c\u4ece\u800c\u4ea7\u751f\u52a8\u753b\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u591a\u4e2a\u6d77\u9f9f\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u4e2a\u6d77\u9f9f\u5bf9\u8c61\u6765\u540c\u65f6\u7ed8\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efaScreen\u5bf9\u8c61<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p>screen.title(&quot;Turtle Graphics&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2aTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t1 = turtle.Turtle()<\/p>\n<p>t2 = turtle.Turtle()<\/p>\n<p>t1.pencolor(&quot;red&quot;)<\/p>\n<p>t1.pensize(2)<\/p>\n<p>t1.speed(1)<\/p>\n<p>t2.pencolor(&quot;green&quot;)<\/p>\n<p>t2.pensize(2)<\/p>\n<p>t2.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>for _ in range(4):<\/p>\n<p>    t1.forward(100)<\/p>\n<p>    t1.right(90)<\/p>\n<p>    t2.forward(100)<\/p>\n<p>    t2.left(90<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6d77\u9f9f\u6a21\u5757\u7ed8\u5236\u56fe\u5f62\uff1f<\/strong><br \/>\u6d77\u9f9f\u6a21\u5757\u662fPython\u4e2d\u7684\u4e00\u4e2a\u56fe\u5f62\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u7528\u6765\u5b66\u4e60\u7f16\u7a0b\u548c\u56fe\u5f62\u7ed8\u5236\u3002\u8981\u4f7f\u7528\u6d77\u9f9f\u7ed8\u56fe\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5Python\u548c\u6d77\u9f9f\u6a21\u5757\u3002\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u6d77\u9f9f\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>turtle<\/code>\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u7ed8\u5236\u5404\u79cd\u5f62\u72b6\uff0c\u5982\u7ebf\u6761\u3001\u5706\u5f62\u548c\u591a\u8fb9\u5f62\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3001\u7b14\u5bbd\u548c\u586b\u5145\u6837\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u4e30\u5bcc\u591a\u5f69\u7684\u56fe\u5f62\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728\u6d77\u9f9f\u7ed8\u56fe\u4e2d\u4f7f\u7528\u54ea\u4e9b\u57fa\u672c\u547d\u4ee4\uff1f<\/strong><br \/>\u6d77\u9f9f\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u57fa\u672c\u547d\u4ee4\u6765\u63a7\u5236\u6d77\u9f9f\u7684\u52a8\u4f5c\u3002\u4f8b\u5982\uff0c<code>turtle.forward(100)<\/code>\u4f7f\u6d77\u9f9f\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d\uff0c<code>turtle.right(90)<\/code>\u8ba9\u6d77\u9f9f\u5411\u53f3\u8f6c90\u5ea6\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>turtle.penup()<\/code>\u548c<code>turtle.pendown()<\/code>\u6765\u63a7\u5236\u662f\u5426\u7ed8\u5236\u7ebf\u6761\u3002\u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u547d\u4ee4\uff0c\u80fd\u591f\u521b\u9020\u51fa\u590d\u6742\u7684\u56fe\u5f62\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6d77\u9f9f\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u989c\u8272\u548c\u586b\u5145\u6548\u679c\uff1f<\/strong><br \/>\u6d77\u9f9f\u6a21\u5757\u652f\u6301\u591a\u79cd\u989c\u8272\u548c\u586b\u5145\u6548\u679c\u3002\u4f7f\u7528<code>turtle.color(&#39;red&#39;)<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u6d77\u9f9f\u7684\u7ed8\u56fe\u989c\u8272\u3002\u901a\u8fc7\u8c03\u7528<code>turtle.begin_fill()<\/code>\u548c<code>turtle.end_fill()<\/code>\uff0c\u53ef\u4ee5\u5728\u7ed8\u5236\u5f62\u72b6\u65f6\u586b\u5145\u989c\u8272\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u586b\u5145\u7ea2\u8272\u7684\u6b63\u65b9\u5f62\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">turtle.begin_fill()\nfor _ in range(4):\n    turtle.forward(100)\n    turtle.right(90)\nturtle.end_fill()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u8ba9\u4f60\u7684\u7ed8\u56fe\u66f4\u52a0\u751f\u52a8\u6709\u8da3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7ed8\u56fe\u65f6\u663e\u793a\u6d77\u9f9f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684turtle\u6a21\u5757\u3002\u5bfc\u5165turtle\u6a21\u5757\u3001\u521b\u5efa\u4e00\u4e2aScr [&hellip;]","protected":false},"author":3,"featured_media":1161735,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161731"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1161731"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161731\/revisions"}],"predecessor-version":[{"id":1161739,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161731\/revisions\/1161739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161735"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}