如何绘制一堆正方形?
我对 python 很陌生。我需要为大学画这个形状,但我发现它真的很难,所以我在进入该图像之前尝试先画出正方形。
不管怎样,我的问题是:我怎样才能有8行? (1 行中有 8 个正方形)我不能继续添加 def start_point1():
这不是正确的方法。
#!/usr/bin/python
import turtle as t
import time
def start_point():
t.penup()
t.setpos(-200,-240)
t.pendown()
def start_point1():
t.penup()
t.setpos(-200,-180)
t.pendown()
def draw_turtle():
for a in range(4):
t.forward(60)
t.left(90)
def draw_turtlerow():
for a in range(8):
draw_turtle()
t.forward(60)
def main():
start_point()
draw_turtlerow()
start_point1()
draw_turtlerow()
I am very new to python. I need to draw this shape for college but i find it really hard so i am trying to do square before going into that image.
Anyway here is my question: How can i have 8 rows? (There are 8 square in 1 row) I can not keep adding def start_point1():
This wouldn't be the proper way to do it.
#!/usr/bin/python
import turtle as t
import time
def start_point():
t.penup()
t.setpos(-200,-240)
t.pendown()
def start_point1():
t.penup()
t.setpos(-200,-180)
t.pendown()
def draw_turtle():
for a in range(4):
t.forward(60)
t.left(90)
def draw_turtlerow():
for a in range(8):
draw_turtle()
t.forward(60)
def main():
start_point()
draw_turtlerow()
start_point1()
draw_turtlerow()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此类问题中,您应该问自己什么是“原子”操作 - 一遍又一遍地重复 - 将生成完整的解决方案。您已经找到了基本的“原子”:绘制正方形的一条边:
现在,“原子性”的下一个级别是什么?好吧,每边重复四次,你可以得到一个正方形(正如你自己正确发现的那样)
现在沿着同样的思路,你可能会得出这样的结论:如果你重复上面的 8 次,你会得到一条线,如果你一行重复 8 次,您将获得完整的检查板。它应该是这样的:
现在,您已经为自己编写了绘制正方形的函数,唯一的问题是您需要在不同的位置绘制每个正方形。这里你要关注的关键点是:你能想出一种方法从
col
和row
的值开始计算这个位置吗?如果您遇到困难,我在此处发布了示例实现,我相信您不需要检查它,但如果你这样做,这是额外的作业:而不是使用按原样代码,在对单独函数
draw_square(row, col)
的调用中转动内圆。编辑:为了获得额外的分数和自豪感,完成练习后,观察检查板上的大多数线条是如何一遍又一遍地重画的。只需很少的努力,您就可以将程序的效率提高一倍。你能想到怎么做吗?
哈!
In this kind of problems, you should ask yourself what is the "atomic" operation that - repeated over and over - will generate your complete solution. You already found your basic "atom": drawing a side of a square:
Now, what is the next level of "atomicity"? Well, repeating four times a side, you can get a square (as you correctly found out yourself)
Now along the same lines, you might come to the conclusion that if you repeat 8 times the above, you get a line, and if you repeat 8 times a line you'll get a complete check board. It should be something like:
Now, you already wrote yourself the function to draw a square, the only problem is that you would need to draw each of them at different locations. The key point you have to focus here is: can you think of a method to calculate this location starting from the values of
col
androw
?If you get stuck, I posted a sample implementation here, I'm confident you won't need to check that out, but if you do, here's the extra assignment: instead of using that code as-is, turn the inner circle in a call to a separate function
draw_square(row, col)
.EDIT: For extra points and pride, once completed the exercise, observe how most lines in the check board are redrawn over and over. With very little effort you can double the efficiency of your program. Can you think how?
HTH!
考虑向
start_point
添加参数,而不是创建新版本的功能。例如:
有了这个,您可以消除
start_pos1()
并且您可以重用代码,这很好。沿着这些思路,也可以考虑为其他功能添加一些灵活性。例如,为什么不让
draw_turtlerow
接受一个数字来告诉它在该行中绘制多少个正方形?然后,您可以创建另一个方法来获取所需的行数 - 该函数将多次调用draw_turtlerow
和start_pos
来绘制整个网格。我不确定这是否是您作业的正确方向,但我希望它能为您指明正确的方向。
Consider adding arguments to
start_point
instead of creating new versions ofthe function. For instance:
With this, you can eliminate
start_pos1()
and you are reusing code, which is good.Along these lines, consider adding some flexibility to your other functions as well. For instance, why not have
draw_turtlerow
take a number that tells it how many squares to draw in the row? Then you could make another method that takes the number of rows you want - this function would then calldraw_turtlerow
andstart_pos
several times to draw the entire grid.I'm not sure this is the right direction for you assignment, but I hope it points you in the right direction.