I have a function that draws a brick wall, with the option to set a certain pen color before the wall is drawn, however it seems that the brick is drawn as green no matter what I do:
def draw_brick(length, width):
t.color("green")
t.begin_fill()
for i in range(2):
t.forward(length)
t.right(90)
t.forward(width)
t.right(90)
t.end_fill()
def draw_row(row_type, bricks):
if row_type == "A":
for i in range(bricks):
draw_brick(50, 30)
t.penup()
t.forward(70)
t.pendown()
elif row_type == "B":
draw_brick(12, 30)
t.penup()
t.forward(35)
t.pendown()
for i in range(bricks - 1):
draw_brick(50, 30)
t.penup()
t.forward(70)
t.pendown()
t.penup()
t.pendown()
draw_brick(12, 30)
def draw_brick_wall(rows, brick, top_row, new_color):
t.pencolor(new_color)
if top_row == "A":
drawing_A = True
else:
drawing_A = False
for i in range(rows):
next_position = (t.xcor(), t.ycor() - 40)
if drawing_A:
draw_row("A", brick)
else:
draw_row("B", brick)
drawing_A = not (drawing_A)
move_no_trails(next_position[0], next_position[1])
# resets turtle to postion (x, y)
def move_no_trails(x, y):
t.penup()
t.goto(x, y)
t.pendown()
For some reason, though, the pen color of the turtle doesn't change. What could I do to remedy this?
Once I add a compatible draw_brick()
function, your code works fine for me:
from turtle import Screen, Turtle
def draw_brick(width, height):
for _ in range(2):
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
def draw_row(row_type, bricks):
if row_type == "A":
for _ in range(bricks):
draw_brick(50, 30)
turtle.penup()
turtle.forward(70)
turtle.pendown()
elif row_type == "B":
draw_brick(12, 30)
turtle.penup()
turtle.forward(35)
turtle.pendown()
for _ in range(bricks-1):
draw_brick(50, 30)
turtle.penup()
turtle.forward(70)
turtle.pendown()
draw_brick(12, 30)
# resets turtle to postion (x, y)
def move_no_trails(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
def draw_brick_wall(rows, brick, top_row, new_color):
turtle.pencolor(new_color)
drawing_A = top_row == "A"
for _ in range(rows):
next_x, next_y = turtle.xcor(), turtle.ycor() - 40
if drawing_A:
draw_row("A", brick)
else:
draw_row("B", brick)
drawing_A = not drawing_A
move_no_trails(next_x, next_y)
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
draw_brick_wall(1, 5, "A", "red")
draw_brick_wall(1, 5, "B", "blue")
draw_brick_wall(1, 5, "A", "orange")
draw_brick_wall(1, 5, "B", "green")
screen.exitonclick()
If, however, your draw_brick()
function is drawing a filled brick, then you'll need to change the call to pencolor(new_color)
in draw_brick_wall()
to either fillcolor(new_color)
to set the fill color or color(new_color)
to set both the pen and fill colors.
Please give the whole code including how you call the draw_brick_wall
function. Also you can try using a different IDE.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.