@Charanpreet/

3.07 Starter Code - Drawing a brick wall

Python (with Turtle)

No description

fork
loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import turtle

def draw_rectangle(width, height):
  turtle.setheading(0) # Face to the right
  turtle.pendown()
  for _ in range(2):
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(height)
    turtle.left(90)
  turtle.penup()

def draw_row_of_bricks(num_bricks, brick_width, brick_height):
  starting_x = turtle.xcor()
  for _ in range(num_bricks):
    draw_rectangle(brick_width, brick_height)
    turtle.setx(turtle.xcor() + brick_width)
  turtle.setx(starting_x)
    
    
def draw_brick_wall(num_rows, num_bricks_per_row, brick_width, brick_height):
  starting_x = turtle.xcor()
  starting_y = turtle.ycor()
  for column_index in range(num_rows):
    turtle.sety(starting_y + column_index * brick_height)
    if column_index % 2 == 0:
      turtle.setx(starting_x + brick_width / 2)
    else:
      turtle.setx(starting_x)
    draw_row_of_bricks(num_bricks_per_row, brick_width, brick_height)
  turtle.setx(starting_x)
  turtle.sety(starting_y)



# Drawing 3 different brick walls. Each wall should have
# a different number of rows, different number of bricks
# per row, and different sizes of bricks.
# You can also use different colors for each brick wall
# if you'd like.
turtle.speed(0)
turtle.penup()
turtle.goto(-300, -200)
turtle.color('red')
draw_brick_wall(5, 10, 20,10)
turtle.goto(-200, -100)
turtle.color('green')
draw_brick_wall(10, 10, 20,15)
turtle.goto(150, 100)
turtle.color('blue')
draw_brick_wall(10, 5, 15,10)
result
console