我可能会得到很多不喜欢的东西,但我真的需要知道这一点。
我试过了turtle.begin_fill()
等等,但没有什么对我有用。
我想在Python中制作填充的三角形/矩形,但我不知道如何在我的代码中实现它。
当前代码:
import turtle, math
def theme():
turtle.speed(10)
column_1()
reset_column()
column_2()
reset_column()
column_1()
def reset_column():
turtle.up()
turtle.forward(160)
turtle.left(90)
turtle.forward(480)
turtle.right(90)
turtle.down()
def reset_box():
turtle.up()
turtle.forward(3 * 32)
turtle.right(90)
turtle.forward(2 * 32)
turtle.right(180)
turtle.down()
def column_2():
empty()
reset_box()
filled()
reset_box()
empty()
reset_box()
def column_1():
filled()
reset_box()
empty()
reset_box()
filled()
reset_box()
def filled():
size = 480
box = size / 3
for index in range(4):
turtle.forward(box)
turtle.right(90)
turtle.forward(32)
turtle.right(135)
turtle.forward(32 * math.sqrt(2))
for index in range(4):
for index in range(3):
turtle.left(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.right(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.left(90)
turtle.forward(32 * math.sqrt(2))
turtle.left(135)
turtle.up()
turtle.forward(32)
turtle.down()
for index in range(4):
turtle.forward(box - (2 * (box / 5)))
turtle.right(90)
turtle.forward(32)
first = True
for index in range(4):
if (first):
turtle.right(135)
first = False
else:
turtle.left(90)
turtle.forward(32 * math.sqrt(2))
turtle.left(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.right(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.left(135)
turtle.up()
turtle.forward(32)
turtle.down()
for index in range(4):
turtle.forward(box - (4 * (box / 5)))
turtle.left(90)
def empty():
size = 480
box = size / 3
for index in range(4):
turtle.forward(box)
turtle.right(90)
turtle.forward(32)
turtle.right(90)
turtle.up()
turtle.forward(32)
turtle.down()
for index in range(4):
turtle.forward(box - (2 * (box / 5)))
turtle.left(90)
turtle.left(90)
turtle.forward(32)
first = True
for index in range(4):
if (first):
turtle.right(135)
first = False
else:
turtle.left(90)
turtle.forward(32 * math.sqrt(2))
turtle.left(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.right(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.left(135)
turtle.up()
turtle.forward(32)
turtle.down()
for index in range(4):
turtle.forward(box - (4 * (box / 5)))
turtle.left(90)
theme()
有什么建议吗?真的不知道如何开始这样的事情。这是我第一次使用Python,尤其是Turtle mechanics。
答案 0 :(得分:1)
使用turtle.begin_fill()
和turtle.end_fill()
,也可以turtle.fillcolor
以下是实现它的代码以及 demo 。我对所有代码行都有评论。
import turtle, math
def theme():
turtle.speed(10)
column_1()
reset_column()
column_2()
reset_column()
column_1()
def reset_column():
turtle.up()
turtle.forward(160)
turtle.left(90)
turtle.forward(480)
turtle.right(90)
turtle.down()
def reset_box():
turtle.up()
turtle.forward(3 * 32)
turtle.right(90)
turtle.forward(2 * 32)
turtle.right(180)
turtle.down()
def column_2():
empty()
reset_box()
filled()
reset_box()
empty()
reset_box()
def column_1():
filled()
reset_box()
empty()
reset_box()
filled()
reset_box()
def filled():
turtle.fillcolor('black') # NEW CODE
turtle.begin_fill() # NEW CODE
size = 480
box = size / 3
for index in range(4):
turtle.forward(box)
turtle.right(90)
turtle.forward(32)
turtle.right(135)
turtle.forward(32 * math.sqrt(2))
for index in range(4):
for index in range(3):
turtle.left(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.right(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.left(90)
turtle.forward(32 * math.sqrt(2))
turtle.left(135)
turtle.up()
turtle.forward(32)
turtle.down()
for index in range(4):
turtle.forward(box - (2 * (box / 5)))
turtle.right(90)
turtle.forward(32)
first = True
for index in range(4):
if (first):
turtle.right(135)
first = False
else:
turtle.left(90)
turtle.forward(32 * math.sqrt(2))
turtle.left(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.right(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.left(135)
turtle.up()
turtle.forward(32)
turtle.down()
for index in range(4):
turtle.forward(box - (4 * (box / 5)))
turtle.left(90)
turtle.end_fill() # NEW CODE
def empty():
size = 480
box = size / 3
for index in range(4):
turtle.forward(box)
turtle.right(90)
turtle.forward(32)
turtle.right(90)
turtle.up()
turtle.forward(32)
turtle.down()
turtle.fillcolor('black') # NEW CODE
turtle.begin_fill() # NEW CODE
for index in range(4):
turtle.forward(box - (2 * (box / 5)))
turtle.left(90)
turtle.left(90)
turtle.forward(32)
turtle.end_fill() # NEW CODE
turtle.fillcolor('white') # NEW CODE
turtle.begin_fill() # NEW CODE
first = True
for index in range(4):
if (first):
turtle.right(135)
first = False
else:
turtle.left(90)
turtle.forward(32 * math.sqrt(2))
turtle.left(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.right(90)
turtle.forward((32 * math.sqrt(2)) / 2)
turtle.end_fill() # NEW CODE
turtle.left(135)
turtle.up()
turtle.forward(32)
turtle.down()
turtle.fillcolor('black') # NEW CODE
turtle.begin_fill() # NEW CODE
for index in range(4):
turtle.forward(box - (4 * (box / 5)))
turtle.left(90)
turtle.end_fill() # NEW CODE
theme()
答案 1 :(得分:1)
我认为这是标记比绘图效果更好的另一种情况。我把它组成了一套五个邮票,其中一个邮票有一个可变的填充颜色:
from turtle import Turtle, Screen
SIZE = 480
BOX = SIZE // 3
STAMP_SIZE = 20
STEP = 32 * 2 ** 0.5
screen = Screen()
stamps = []
stamp = Turtle('square', visible=False)
stamp.shapesize(BOX / STAMP_SIZE)
stamp.color('black', 'white')
stamps.append(stamp)
stamp2 = Turtle()
stamp2.goto(STEP / 2, -BOX / 2 - STEP / 4)
stamp2.begin_poly()
for _ in range(4):
for _ in range(3):
stamp2.left(90)
stamp2.forward(STEP / 2)
stamp2.right(90)
stamp2.forward(STEP / 2)
stamp2.left(90)
stamp2.forward(STEP)
stamp2.end_poly()
polygon = stamp2.get_poly()
screen.addshape('stamp2', polygon)
stamp2.reset()
stamp2.shape('stamp2')
stamp2.color('white')
stamp2.tilt(45)
stamps.append(stamp2)
stamp = Turtle('square', visible=False)
stamp.shapesize(3 * BOX / 5 / STAMP_SIZE)
stamp.color('black')
stamps.append(stamp)
stamp4 = Turtle()
stamp4.setx(BOX / 5)
stamp4.right(45)
stamp4.begin_poly()
for index in range(4):
stamp4.forward(STEP / 2)
stamp4.right(90)
stamp4.forward(STEP)
stamp4.right(90)
stamp4.forward(STEP / 2)
stamp4.left(90)
stamp4.end_poly()
polygon = stamp4.get_poly()
screen.addshape('stamp4', polygon)
stamp4.reset()
stamp4.shape('stamp4')
stamp4.color('white')
stamps.append(stamp4)
stamp = Turtle('square', visible=False)
stamp.shapesize(BOX / 5 / STAMP_SIZE)
stamp.color('black')
stamps.append(stamp)
for stamp in stamps:
stamp.speed('fastest')
stamp.penup()
parity = True
for x in range(-BOX, BOX + 1, BOX):
for y in range(-BOX, BOX + 1, BOX):
stamps[0].fillcolor(['white', 'black'][parity])
for stamp in stamps:
stamp.goto(x, y)
stamp.stamp()
parity = not parity
for stamp in stamps:
stamp.hideturtle()
screen.exitonclick()
您会发现这会使代码更简单,更快捷。