Home > Blockchain >  I need to create a mosaic using the loop programming construct with 16 tiles of the same pattern and
I need to create a mosaic using the loop programming construct with 16 tiles of the same pattern and

Time:05-04

For my assignment i have to create a shape using turtle, then turn that shape into a mosaic with 16 tiles in a 4 * 4 pattern. I created the turtle shape but every attempt I've made to make a mosaic has ended in an error or no output. Here is my turtle shape i'm using for this project. How would I create a mosaic using this shape?

import turtle
wn = turtle.Screen()
wn.bgcolor('white')
wn.setup(500,500)
bob = turtle.Turtle()
bob.speed(10)
bob.pensize(2)
bob.color('purple')
for i in range(20):
    bob.left(140)
    bob.forward(100)
bob.hideturtle()

CodePudding user response:

"No output" probably means that the window is closing itself. Try adding this to the end to prevent that:

# ...
bob.hideturtle()
input('press enter to exit')

You can draw the same shape multiple times by teleporting to the spot that you want each shape to be drawn.

def shape():
    for i in range(18):
        bob.left(140)
        bob.forward(100)

# coordinates of each of the shapes
# these are completely arbitrary
# you can change these to whatever (adjust spacing, change where they are, etc)
# you can also write this using ranges if you want
for x in (-100, 0, 100, 200):
    for y in (-150, -50, 50, 150):
        bob.penup()
        bob.setposition(x, y)
        bob.pendown()
        shape()

This will loop through all 16 points, -100, -150, -100, -50, -100, 50, ..., 200, 150.

Note that I changed your shape to only looping 18 times - this makes a total rotation of a multiple of 360 degrees so the next shape is not slanted. Additionally, the shape would only have 18 of these edges, so drawing the extra 2 would be a waste. enter image description here

  • Related