Simpler way to make a square and rotated square in Python Turtle graphics

喜欢而已 提交于 2019-12-11 08:24:13

问题


I'm working in turtle graphics to recreate this pattern:

This is probably a very basic question, but is there a simpler way for me to create that rotated square within a square shape? As it is, I just use one turtle to make a normal square, then slowly move a second turtle into position to draw the rotated part. Ex:

import turtle
alex = turtle.Turtle()
tess = turtle.Turtle()

for i in range(4):
    alex.fd(50)
    alex.lt(90)
tess.pu()
tess.fd(25)
tess.rt(90)
tess.fd(10)
tess.rt(225)
tess.pd()
for i in range(4):
    tess.fd(50)
    tess.lt(90)

Which to me is clunky at best, and doesn't work if I change the side-lengths (which I intend to do).

Thanks very much in advance!


回答1:


This is a great time to start using functions! Using functions you can create a reusable chunk of code which can repeat a certain task - for example, drawing a square or a square-in-square shape.

Lets take your code and add a square function which draws a square of a certain size. To do this, we'll tell the function which turtle to use, as well as the size of the square:

def square(this_turtle, side_length):
    for i in range(4):
        this_turtle.fd(side_length)
        this_turtle.lt(90)

Now let's use the new method in your code:

square(alex, 50)
tess.pu()
tess.fd(25)
tess.rt(90)
tess.fd(10)
tess.rt(225)
tess.pd()
square(tess, 50)

From here you can then think about how you could make a star function which makes a "square-in-square" shape of any given size. Good luck!

Here's a more detailed explaination on how you can use functions: http://openbookproject.net/thinkcs/python/english3e/functions.html (I suspect this is the tutorial you're already following!)




回答2:


I'm going to suggest a contrary approach to yours and the other answers which are too focused on drawing squares which will take too much work to complete. Since this is a repeating pattern, I think stamping is the way to go, just like repeated patterns in real life. Specifically:

from turtle import Turtle, Screen

BASE_UNIT = 20

def tessellate(turtle, start, stop, step):
    for x in range(start, stop + 1, step):
        for y in range(start, stop + 1, step):
            turtle.goto(x * BASE_UNIT, y * BASE_UNIT)
            turtle.stamp()
            turtle.left(45)
            turtle.stamp()

alex = Turtle(shape="square")
alex.shapesize(8)
alex.color("red")
alex.penup()

tessellate(alex, -12, 12, 12)

tess = Turtle(shape="square")
tess.shapesize(4)
tess.color("gold")
tess.penup()

tessellate(tess, -6, 6, 12)

screen = Screen()

screen.exitonclick()

OUTPUT

Turtle stamps naturally rotate and scale which turtle drawings do not!

One thing you'll notice is that my pattern isn't quite the same. In the original the two red (or yellow) squares that make up a star are not the same size! They are slightly different to make the pattern work -- I leave it as an exercise for the OP to correct this.




回答3:


Learn how to write a function; this is an excellent place to start. Write a function to draw a square of a given size, assuming that the turtle is presently at the starting point and facing the proper direction. Then put your square-drawing loop inside the function:

def draw_square(tortuga, size):
    for i in range(4):
        tortuga.fd(size)
        tortuga.lt(90)

This will remove the drawing details from your main code.

The next thing you do is to write more general code to make Tess follow Alex to the proper spot -- or to make Alex move after finishing the first square, doing the second one himself.



来源:https://stackoverflow.com/questions/41005952/simpler-way-to-make-a-square-and-rotated-square-in-python-turtle-graphics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!