Skip to content Skip to sidebar Skip to footer

Simpler Way To Make A Square And Rotated Square In Python Turtle Graphics

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 sh

Solution 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:

defsquare(this_turtle, side_length):
    for i inrange(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!)

Solution 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

enter image description here

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.

Solution 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.

Post a Comment for "Simpler Way To Make A Square And Rotated Square In Python Turtle Graphics"