Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating mosaic tile grid with turtle (intro level programming)

below is the assignment for my intro level programming class

For this assignment, you will create your own intricate mosaic tile pattern using turtle graphics. Your mosaic must meet the following requirements:

  1. must make use of the loop programming construct
  2. 16 tiles of the same pattern
  3. 3 or more different colors

Im trying to keep this super simple because I'm very much a beginner. Here is my code so far, and I'm getting stuck on using a for loop to have a turtle draw a pattern, move to the right across the screen and draw that pattern again 4 separate times using a for loop. I'm testing this on one turtle and it moves the first time, but then draws the pattern in the same spot the other 3 times.

# Creating a mosaic using greg the turlte from the Turtle Graphics module

import turtle 

#setting up the screen environment 
myscreen = turtle.Screen()
myscreen.bgcolor("black")
myscreen.screensize(400, 400)

# Initializing turtles, their writing speed, and pen color
greg = turtle.Turtle()
greg.speed(0.5)
greg.color('red')

# Set starting locations for each turtle
greg.up()
greg.goto(-250, 250)
greg.down()

#create loop function for each turtle 
y = -250
angle = 91
for y in range(4):
    for x in range(50):
        greg.forward(x)
        greg.left(angle)
    greg.up()
    greg.goto(y, 250)
    greg.down()
    y = y + 100

myscreen.exitonclick()

image of the output image of the output

What my output should look like in structure What my output should look like in structure

I've tried rearranging the for loops but I think the logic of for statements hasn't quite clicked in my brain yet.

like image 469
Richelle Avatar asked Jun 07 '26 11:06

Richelle


1 Answers

One of the most critical skills in programming is decomposing problems into smaller chunks until they're solvable, then recomposing the small solutions to solve large problems. Trying to do too much at once will lead to a confused state, like a ball of yarn with no obvious entry point for untangling it.

Applying that technique here, this problem can be decomposed into many smaller sub-problems, the simplest of which is drawing a rhombus, which is the shape the tiling is composed of:

import turtle 

size = 80
t = turtle.Turtle()
t.forward(size)
t.left(60)
t.forward(size)
t.left(120)
t.forward(size)
t.left(60)
t.forward(size)

turtle.exitonclick()

At each sub-step, some trial and error will be necessary to get things right, but at least the goal will be simple enough that it's achievable. Run the code frequently to get feedback about each change.

This rhombus can be rotated around the circle to draw the inner portion of the shape, making a turn to prepare the turtle for the next shape:

# ...

for _ in range(6):
    t.forward(size)
    t.left(60)
    t.forward(size)
    t.left(120)
    t.forward(size)
    t.left(60)
    t.forward(size)

    # prepare to draw the next shape
    t.left(60)

# ...

Repeat the process for the outer shape. First, draw one rhombus:

# ...

# move to the outside
t.penup()
t.forward(size)
t.left(60)
t.forward(size)
t.pendown()

# draw the first outer rhombus
t.right(120)
t.forward(size)
t.right(60)
t.forward(size)

# ...

With this code to draw one outer rhombus, add a loop and a preparatory turn to draw 6 outer rhombi:

# move to the outside
t.penup()
t.forward(size)
t.left(60)
t.forward(size)
t.pendown()

for _ in range(6):
    t.right(120)
    t.forward(size)
    t.right(60)
    t.forward(size)

    # prepare to draw the next shape
    t.left(120)

With the main pattern completed, the code can be abstracted into a function called draw_tiled_hexagon and used 16 times in a grid pattern using nested loops (or with relative movement commands line forward()/left(), which are a bit more in the spirit of turtle):

import turtle

def draw_tiled_hexagon(t):
   # add all the drawing code so far
   ...

# ...

size = 60
t = turtle.Turtle()
t.speed("fastest")

for y in range(-2, 2):
    for x in range(-2, 2):
        t.penup()
        t.goto(
            x * size * 4 + size * 2,
            y * size * 3.47 + size * 2 # 3.47 is kinda hacky
        )
        t.pendown()
        draw_tiled_hexagon(t)

turtle.exitonclick()

Lastly, add color. Doing so was a bit tricky, since filling only works when the path completely closes the shape. I had to adjust my earlier code to account for that, which is normal during the course of development.

import turtle 
from random import random


def random_color():
    return random(), random(), random()


def draw_tiled_hexagon(t):
    for _ in range(6):
        t.begin_fill()
        t.forward(size)
        t.left(60)
        t.forward(size)
        t.left(120)
        t.forward(size)
        t.left(60)
        t.forward(size)
        t.end_fill()

        # prepare to draw the next shape
        t.left(60)

    # move to the outside
    t.penup()
    t.forward(size)
    t.left(60)
    t.pendown()

    for _ in range(6):
        t.begin_fill()
        t.forward(size)
        t.right(120)
        t.forward(size)
        t.right(60)
        t.forward(size)
        t.right(120)
        t.forward(size)
        t.end_fill()

        # prepare to draw the next shape
        t.penup()
        t.forward(size)
        t.pendown()


turtle.bgcolor("black")
size = 60
t = turtle.Turtle()
t.speed("fastest")
t.color("blue")
t.pensize(4)

for y in range(-2, 2):
    for x in range(-2, 2):
        t.penup()
        t.goto(
            x * size * 4 + size * 2,
            y * size * 3.47 + size * 2 # 3.47 is kinda hacky
        )
        t.pendown()
        t.fillcolor(random_color())
        draw_tiled_hexagon(t)

turtle.exitonclick()

This is not the most elegant solution, but it works, and I was able to apply a methodical problem solving strategy to get there. After achieving correctness, there are usually opportunities to optimize and simplify, left as an exercise here.

See also Completing Code to Print a shape using Turtle Library, which is a very similar pattern. I solved that problem with the same basic strategy as this.

like image 54
ggorlen Avatar answered Jun 10 '26 12:06

ggorlen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!