문제

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()
도움이 되었습니까?

해결책

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!

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top