Python 乌龟颜色
Python turtle color
嘿,我必须在 python 中编写一个程序,该程序需要用户输入行数、列数、正方形边长和三种颜色。然后程序必须根据行数和列数制作一个网格,并以交替的棋盘格图案填充方块。我已将其编码到填充颜色的程度,我想知道是否有人可以帮助我。这是我目前所拥有的:
from turtle import *
t = Turtle()
screen = t.getscreen()
rows = screen.numinput('Number of rows',
'How many rows shall there be?', 5, 1, 10)
columns = screen.numinput('Number of columns',
'How many columns shall there be?', 5, 1, 10)
side_length = screen.numinput('Length of square side',
'How long shall the square sides be?', 30, 10, 50)
first_color = screen.textinput('First color',
'What shall the first color be?')
second_color = screen.textinput('Second color',
'What shall the second color be?')
third_color = screen.textinput('Third color',
'What shall the third color be?')
square_color = ''
def draw_square():
t.begin_fill()
t.pendown()
t.forward(side_length)
t.left(90)
t.forward(side_length)
t.left(90)
t.forward(side_length)
t.left(90)
t.forward(side_length)
t.color(square_color)
t.end_fill()
t.penup()
t.color('black')
t.left(90)
t.forward(side_length)
def draw_board():
n = 1
for i in range(int(columns)):
draw_square()
for x in range(int(rows - 1)):
t.goto(0,side_length * n)
for i in range(int(columns)):
draw_square()
n += 1
for i in range(int(columns)):
for x in range(int(rows)):
if x + i % 3 == 0:
square_color = first_color
elif x + i % 3 == 1:
square_color = second_color
elif x + i % 3 == 2:
square_color = third_color
draw_board()
done()
您拥有所需的所有代码,实际上太多了。你只是没有正确组装它。您需要将您的程序视为一个故事,并以正确的事件顺序讲述该故事,使该故事有意义。以下是我对您的代码进行的修改,以更好地排列顺序以及一些样式调整和代码清理:
from turtle import Turtle, Screen
def draw_square(turtle, length, color):
turtle.color(color)
turtle.pendown()
turtle.begin_fill()
for _ in range(4):
turtle.forward(length)
turtle.left(90)
turtle.end_fill()
turtle.penup()
turtle.forward(length)
def draw_board(turtle, length, colors):
n = 0
for row in range(int(rows)):
turtle.goto(0, length * n)
for column in range(int(columns)):
square_color = colors[(column + row) % len(colors)]
draw_square(turtle, length, square_color)
n += 1
screen = Screen()
rows = screen.numinput('Number of rows', 'How many rows shall there be?', 5, 1, 10)
columns = screen.numinput('Number of columns', 'How many columns shall there be?', 5, 1, 10)
side_length = screen.numinput('Length of side', 'How long shall the square sides be?', 30, 10, 50)
first_color = screen.textinput('First color', 'What shall the first color be?')
second_color = screen.textinput('Second color', 'What shall the second color be?')
third_color = screen.textinput('Third color', 'What shall the third color be?')
colors = [first_color, second_color, third_color]
turtle = Turtle()
turtle.penup()
draw_board(turtle, side_length, colors)
turtle.hideturtle()
screen.exitonclick()
产品:
嘿,我必须在 python 中编写一个程序,该程序需要用户输入行数、列数、正方形边长和三种颜色。然后程序必须根据行数和列数制作一个网格,并以交替的棋盘格图案填充方块。我已将其编码到填充颜色的程度,我想知道是否有人可以帮助我。这是我目前所拥有的:
from turtle import *
t = Turtle()
screen = t.getscreen()
rows = screen.numinput('Number of rows',
'How many rows shall there be?', 5, 1, 10)
columns = screen.numinput('Number of columns',
'How many columns shall there be?', 5, 1, 10)
side_length = screen.numinput('Length of square side',
'How long shall the square sides be?', 30, 10, 50)
first_color = screen.textinput('First color',
'What shall the first color be?')
second_color = screen.textinput('Second color',
'What shall the second color be?')
third_color = screen.textinput('Third color',
'What shall the third color be?')
square_color = ''
def draw_square():
t.begin_fill()
t.pendown()
t.forward(side_length)
t.left(90)
t.forward(side_length)
t.left(90)
t.forward(side_length)
t.left(90)
t.forward(side_length)
t.color(square_color)
t.end_fill()
t.penup()
t.color('black')
t.left(90)
t.forward(side_length)
def draw_board():
n = 1
for i in range(int(columns)):
draw_square()
for x in range(int(rows - 1)):
t.goto(0,side_length * n)
for i in range(int(columns)):
draw_square()
n += 1
for i in range(int(columns)):
for x in range(int(rows)):
if x + i % 3 == 0:
square_color = first_color
elif x + i % 3 == 1:
square_color = second_color
elif x + i % 3 == 2:
square_color = third_color
draw_board()
done()
您拥有所需的所有代码,实际上太多了。你只是没有正确组装它。您需要将您的程序视为一个故事,并以正确的事件顺序讲述该故事,使该故事有意义。以下是我对您的代码进行的修改,以更好地排列顺序以及一些样式调整和代码清理:
from turtle import Turtle, Screen
def draw_square(turtle, length, color):
turtle.color(color)
turtle.pendown()
turtle.begin_fill()
for _ in range(4):
turtle.forward(length)
turtle.left(90)
turtle.end_fill()
turtle.penup()
turtle.forward(length)
def draw_board(turtle, length, colors):
n = 0
for row in range(int(rows)):
turtle.goto(0, length * n)
for column in range(int(columns)):
square_color = colors[(column + row) % len(colors)]
draw_square(turtle, length, square_color)
n += 1
screen = Screen()
rows = screen.numinput('Number of rows', 'How many rows shall there be?', 5, 1, 10)
columns = screen.numinput('Number of columns', 'How many columns shall there be?', 5, 1, 10)
side_length = screen.numinput('Length of side', 'How long shall the square sides be?', 30, 10, 50)
first_color = screen.textinput('First color', 'What shall the first color be?')
second_color = screen.textinput('Second color', 'What shall the second color be?')
third_color = screen.textinput('Third color', 'What shall the third color be?')
colors = [first_color, second_color, third_color]
turtle = Turtle()
turtle.penup()
draw_board(turtle, side_length, colors)
turtle.hideturtle()
screen.exitonclick()
产品: