简体   繁体   English

Python Turtle图形简化

[英]Python Turtle Graphics Simplification

just reviewing for my upcoming midterm. 只是为我即将来的期中考试做准备。 We were given Past midterm problems but no solutions. 我们得到了过去的中期问题,但没有解决方案。 I am trying to grasp the knowledge best i can. 我正在尽我所能来掌握知识。

For this problem, it asks to define a function named equalSigns, pass it values t and length. 对于此问题,它要求定义一个名为equalSigns的函数,并将其值t和length传递给它。 So, i just need to make my program in turtle graphics, create two parellel line, simple enough i suppose. 所以,我只需要用乌龟图形制作程序,创建两条平行线,我想就足够简单了。 this is my code that i wrote just for it to correctly output an equal sign of x length. 这是我为它编写的代码,用于正确输出x长度的等号。 (then of course i would convert it to a function) My question, is there any better way to create this? (然后,我当然会将其转换为函数)我的问题是,有没有更好的方法来创建它?

    import turtle
t=turtle.Turtle()
s=turtle.Screen()

t.forward(200)
t.penup()
t.home()
t.right(90)
t.forward(50)
t.pendown()
t.left(90)
t.forward(200) 
'''i suppose i dont have to go home and then down. 
instead just continue and go down and forward left.
but either way, is this the best approach to take?
'''

Yes, I think there's a better way. 是的,我认为有更好的方法。 Most of all, I think you turned the wrong way: you need to make a second right turn to come back along the lower line. 最重要的是,我认为您走错了路:您需要再右转第二次才能沿着较低的线返回。

You could make a routine that does a half-equals, and then all it twice to get the two lines. 可以创建一个执行等号的例程,然后执行两次以获取两行。 Think of this as drawing a rectangle, except that the short sides are invisible. 可以将其视为绘制矩形,只是短边不可见。

# Draw long side
t.pendown()
t.forward(x)
t.penup()
t.right(90)

# Move along short side without drawing
t.forward(x/4)
t.right(90)

That gets you to the opposite corner of the rectangle. 那将带您到矩形的相对角。 Call this twice, and you're done ... and back at the starting point. 调用两次,您就完成了……然后回到起点。

Perhaps you could have your turtle think outside the shell: 也许您可以让乌龟在壳外思考:

import turtle
import tkinter as _

_.ROUND = _.BUTT

turtle.width(50)
turtle.forward(200)
turtle.color("white")
turtle.width(48)
turtle.backward(200)

turtle.done()

在此处输入图片说明

(The vertical gray bars at the two ends are artifacts of GIF conversion and not present when the program is run.) (两端的垂直灰色条是GIF转换的产物,在程序运行时不存在。)

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

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