如何绘制一堆正方形?

发布于 2024-12-18 14:18:32 字数 719 浏览 0 评论 0原文

我对 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.

enter image description here

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2024-12-25 14:18:32

在此类问题中,您应该问自己什么是“原子”操作 - 一遍又一遍地重复 - 将生成完整的解决方案。您已经找到了基本的“原子”:绘制正方形的一条边:

t.forward(60)

现在,“原子性”的下一个级别是什么?好吧,每边重复四次,你可以得到一个正方形(正如你自己正确发现的那样)

for a in range(4):
    t.forward(60)
    t.left(90)

现在沿着同样的思路,你可能会得出这样的结论:如果你重复上面的 8 次,你会得到一条线,如果你一行重复 8 次,您将获得完整的检查板。它应该是这样的:

for col in range(8):
    for row in range(8):
        # draw a square here

现在,您已经为自己编写了绘制正方形的函数,唯一的问题是您需要在不同的位置绘制每个正方形。这里你要关注的关键点是:你能想出一种方法从colrow的值开始计算这个位置吗?

如果您遇到困难,我在此处发布了示例实现,我相信您不需要检查它,但如果你这样做,这是额外的作业:而不是使用按原样代码,在对单独函数 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:

t.forward(60)

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)

for a in range(4):
    t.forward(60)
    t.left(90)

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:

for col in range(8):
    for row in range(8):
        # draw a square here

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 and row?

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!

浅浅 2024-12-25 14:18:32

考虑向 start_point 添加参数,而不是创建新版本
的功能。例如:

def start_point(x,y):
    t.penup()
    t.setpos(x,y)
    t.pendown()

有了这个,您可以消除 start_pos1() 并且您可以重用代码,这很好。

沿着这些思路,也可以考虑为其他功能添加一些灵活性。例如,为什么不让 draw_turtlerow 接受一个数字来告诉它在该行中绘制多少个正方形?然后,您可以创建另一个方法来获取所需的行数 - 该函数将多次调用 draw_turtlerowstart_pos 来绘制整个网格。

我不确定这是否是您作业的正确方向,但我希望它能为您指明正确的方向。

Consider adding arguments to start_point instead of creating new versions of
the function. For instance:

def start_point(x,y):
    t.penup()
    t.setpos(x,y)
    t.pendown()

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 call draw_turtlerow and start_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文