[英]Python3-Turtle: Pen color not changing
我有一個繪制磚牆的 function ,可以選擇在繪制牆之前設置某種筆顏色,但是無論我做什么,磚似乎都被繪制為綠色:
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()
但是,由於某種原因,烏龜的筆顏色沒有改變。 我能做些什么來解決這個問題?
一旦我添加了兼容的draw_brick()
function,您的代碼就可以正常工作:
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()
但是,如果您的draw_brick()
function 正在繪制填充磚,那么您需要將 draw_brick_wall() 中對pencolor(new_color)
的調用更改為draw_brick_wall()
fillcolor(new_color)
以將填充顏色或color(new_color)
設置為設置筆和填充 colors。
請提供整個代碼,包括您如何調用draw_brick_wall
function。 您也可以嘗試使用不同的 IDE。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.