Skip to content
Advertisement

Creating Bricks with Turtle Python

i’m very new to programming and in here i’m supposed to create a pyramid shape of bricks where the edge of the pyramid is filled with one color and the inside of it is filled with random colors, but i can’t seem to figure it out how to move the bricks to create a new row, column and the random colors specified only to inside bricks of the pyramid. Does anyone have a suggestion? Thanks in advance. This is my code and it’s still working for one brick only:

import turtle
import math
import random

bottom_brick = 10
top_brick = 1
brick_length = 35
brick_width = 25

from turtle import *
from turtle import Screen
screen = Screen()  
screen.bgcolor('white')

turtle.speed('fastest')

penup ()
goto(0, -100)
pendown()
fillcolor('#BC4A3C')
begin_fill()

#bottom_brick and top_brick are inputs from the user 
#to indicate how many bricks are used for the bottom layer and the top layer
# brick_length and brick_width are also input from user


for i in range (0, bottom_brick):
    turtle.setposition(0.5 * (i % 2), i)
   
    for j in range(i, bottom_brick):
        forward(brick_length)
        left(90)
        forward(brick_width)
        left(90)
        forward(brick_length)
        left(90)
        forward(brick_width)
        left(90)
        end_fill()
    done()

Advertisement

Answer

This will get you most of the way there with regards to making sure your bricks are laid out correctly:

import turtle

bottom_brick = 10
top_brick = 1
brick_length = 35
brick_width = 25


turtle.Screen().bgcolor('white')
turtle.speed('fastest')

turtle.penup()
turtle.goto(0, -100)
turtle.pendown()


#bottom_brick and top_brick are inputs from the user 
#to indicate how many bricks are used for the bottom layer and the top layer
# brick_length and brick_width are also input from user


for i in range(bottom_brick - top_brick + 2):
    for j in range(bottom_brick - i + 1):
        turtle.setposition((j + i / 2) * brick_length, i * brick_width)
        turtle.fillcolor('#BC4A3C')
        turtle.begin_fill()
        turtle.forward(brick_length)
        turtle.left(90)
        turtle.forward(brick_width)
        turtle.left(90)
        turtle.forward(brick_length)
        turtle.left(90)
        turtle.forward(brick_width)
        turtle.left(90)
        turtle.end_fill()
turtle.done()

Note that the fill color needs to be set inside the loop, and that’s the spot where you’ll want to randomize the color so that each brick is different. You’ll also need to be mindful of when the pen is up and when it’s down in order to avoid extra lines showing up.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement