如何在 Turtle 中填充这些方块 - Python [英] How Can I Fill These Squares in Turtle - Python
本文介绍了如何在 Turtle 中填充这些方块 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试填充这些方块中的颜色:
I am trying to fill the color in these squares:
现在海龟只填满了这些方块的角,而不是整个方块.
Right now the turtle only fills the corners of theses squares, not the entire square.
这是我的代码:
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,谢谢.
I've tried moving around turtle.begin_fill()
and end_fill()
in numerous locations with no luck… Using Python 3.2.3, thanks.
推荐答案
我还没有真正使用过乌龟,但看起来这可能是你想要做的.如果我为这些调用假设了错误的功能,请纠正我:
I haven't really used turtle, but it looks like this may be what you want to do. Correct me if I've assumed the wrong functionality for these calls:
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.
这篇关于如何在 Turtle 中填充这些方块 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文