简体   繁体   English

带有海龟图形的 Python 中的字符 S、5 和 3?

[英]Characters S, 5, and 3 in Python with Turtle Graphics?

Currently I am trying to use commands to tell a turtle to draw the characters "S," "3," and "5" using the turtle module - without using the command turtle.write.目前我正在尝试使用命令告诉乌龟使用乌龟模块绘制字符“S”、“3”和“5”——而不使用命令turtle.write。 Currently my code looks like:目前我的代码看起来像:

import turtle

def halfcircle(parts=360, line=1, direction=1):
    for x in range(parts//2):
        turtle.forward(line)
        turtle.left(360/parts * direction)

turtle.tracer(False)

for x in range(2):
    halfcircle(20, 360/30, 1)
    halfcircle(20, 360/30, -1)

turtle.update() 

But the loop is never ending.但循环永无止境。 Additionally, I am unable to incorporate linear and nonlinear lines into one character in order to draw a 5 or 3. Any help would be much appreciated.此外,我无法将线性和非线性线合并到一个字符中以绘制 5 或 3。任何帮助将不胜感激。

Before I could actually check this out, I had to add an extra line of turtle.exitonclick() to the script to prevent it from disappearing from my screen as soon as I ran it.在我真正检查这个之前,我不得不在脚本中添加一行额外的turtle.exitonclick()以防止它在我运行后立即从我的屏幕上消失。

From what I'm seeing, it doesn't appear to actually be never ending.从我所看到的,它似乎并没有真正永无止境。 The turtle draws four half-circle shapes then stops.乌龟画了四个半圆形然后停下来。 When you say never ending, do you mean that it's repeating more than you wanted/expected?当您说永无止境时,您的意思是它的重复次数超出您的预期/预期吗? Or are you calling this code somewhere else and it's really never stopping in some other part?或者您是否在其他地方调用此代码并且它真的永远不会在其他部分停止?

If you just mean that it's drawing too many half-circles (4, when you want 2 to make an S ), that's because of the 2 in your for-loop for x in range(2) : that for-loop has two half-circles in it, so when the loop runs twice (because of range(2) ), you get 2 * 2 = 4 half-circles.如果你只是说它画了太多的半圆(4,当你想要 2 做一个S ),那是因为你的 for 循环中的2 for x in range(2) :那个 for 循环有两个半-circles 在里面,所以当循环运行两次时(因为range(2) ),你会得到 2 * 2 = 4 个半圆。 [BTW, as a convention, we generally use a single underscore _ to indicate "throw-away" variables. [顺便说一句,作为惯例,我们通常使用单个下划线_来表示“丢弃”变量。 You'll see that the var x does not actually get used in your for-loop, it's just necessary to state some var for the for-loop syntax.您将看到 var x实际上并未在您的 for 循环中使用,只需要为 for 循环语法声明一些 var 即可。 A lot of Python programmers just write for _ in range(2) as a way of saying, "Don't pay attention to this var, it's not going to get used."]很多 Python 程序员只是将for _ in range(2)写成一种方式,“不要注意这个 var,它不会被使用。”]

So if you change the for-loop from range(2) to being range(1) , you get a single pair of half-circles and the program halts on this image:因此,如果将 for 循环从range(2)更改为range(1) ,则会得到一对半圆并且程序在此图像上停止:

两个半圆形成 S 形

Which, admittedly, is not a great S shape, but it's getting there.诚然,这不是一个很好的S形,但它正在到达那里。 In general, drawing symbols is hard .一般来说,绘制符号很难 I think a really good S is probably the hardest one.我认为真正好的S可能是最难的。 I think the easiest might be a 5 ?我认为最简单的可能是5 ? The best approach to figuring out how to code symbols is to draw it very large and very carefully on graph paper, then break it down into its component parts.弄清楚如何编码符号的最佳方法是在方格纸上非常小心地绘制它,然后将其分解为各个组成部分。 Let's use 5 as an example, working from bottom to top:让我们以5为例,从下到上工作:

  • A curved section, maybe half-circle, maybe more like 2/3's of a circle, maybe more of an ellipse, kinda hard to be certain.一个弯曲的部分,也许是半圆,也许更像是一个圆的 2/3,也许更像是一个椭圆,有点难以确定。
  • A straight line going (mostly) up (or maybe just a hair to the right)一条直线(大部分)向上(或者可能只是右边的一根头发)
  • A straight line going directly right, a little longer than the previous line一条直线向右走,比前一条线长一点

Then, with that series of motions in mind, create code to draw each piece on its own .然后,记住,一系列议案,创建代码,利用其自身的每一块。 Once you've done that, just have the turtle draw the first piece, then immediately draw the next, then the next.完成后,只需让海龟绘制第一块,然后立即绘制下一块,然后再绘制下一块。 You'll have to figure out how to reset the turtle's direction between pieces, but other than that, you'll have a 5 !您必须弄清楚如何在碎片之间重置海龟的方向,但除此之外,您将有一个5 It's not so much that you want to have nonlinear and linear segments together , as a single symbol is created by drawing one segment after another, and you just write code that draws the specific segment one at a time.并不是希望将非线性段和线性段放在一起,因为一个符号是通过一个接一个地绘制来创建的,而您只需编写一次绘制特定段的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM