我想填补这些方块的颜色:
现在乌龟只填充这些方块的角落,而不是整个方块。
这是我的代码:
import turtle
import time
import random
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)
angle = 180 - 180*(squares-2)/squares
turtle.up
x = 0
y = 0
turtle.setpos(x,y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(),random.random(), random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
turtle.end_fill()
time.sleep(11)
turtle.bye()
我试过在很多地方移动turtle.begin_fill()
和end_fill()
而没有运气......使用Python 3.2.3,谢谢。
答案 0 :(得分:6)
我还没有真正使用龟,但看起来这可能是你想要做的。如果我为这些电话假设了错误的功能,请纠正我:
turtle.begin_fill() # Begin the fill process.
turtle.down() # "Pen" down?
for i in range(squares): # For each edge of the shape
turtle.forward(40) # Move forward 40 units
turtle.left(angle) # Turn ready for the next edge
turtle.up() # Pen up
turtle.end_fill() # End fill.
答案 1 :(得分:2)
您正在绘制一系列三角形,每个三角形使用begin_fill()
和end_fill()
。您可以做的是将您的来电移至内部循环外的begin_fill()
和end_fill()
,以便绘制一个完整的正方形,然后然后要求填充它。
答案 2 :(得分:1)
使用填充
t.begin_fill()
t.color("red")
for x in range(4):
t.fd(100)
t.rt(90)
t.end_fill()
答案 3 :(得分:0)
答案 4 :(得分:0)
正如一些人提到的那样,将begin_fill()
和end_fill()
移到循环外,您的代码还有其他问题。例如,这是一个无人操作:
turtle.up
即它什么也没做。 (缺少括号。)此测试:
if num_str.isdigit():
对您没有多大帮助,因为没有else
子句可以处理该错误。 (即,当它不是数字时,下一条语句只是将字符串用作数字而失败。)此计算似乎有点复杂:
angle = 180 - 180*(squares-2)/squares
最后应该有一种更干净的方法退出程序。让我们解决所有这些问题:
from turtle import Screen, Turtle
from random import random
NUMBER_SHAPES = 8
print("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = ""
while not num_str.isdigit():
num_str = input("Enter the side number of the shape you want to draw: ")
sides = int(num_str)
angle = 360 / sides
delta_distance = 0
delta_angle = 0
screen = Screen()
turtle = Turtle()
for x in range(NUMBER_SHAPES):
turtle.color(random(), random(), random())
turtle.penup()
delta_distance += 5
turtle.forward(delta_distance)
delta_angle += 5
turtle.left(delta_angle)
turtle.pendown()
turtle.begin_fill()
for _ in range(sides):
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
turtle.end_fill()
screen.exitonclick()