我是python的初学者,正在用python龟编写一个简单的程序,提示用户输入多边形的边长,程序应该绘制多边形并打印人的名字(我)在多边形下。
我已经让程序工作,但是我似乎无法弄清楚如何在多边形下打印文本,因为边长可以由用户更改,因此多边形有时可能会偏离视图,取决于边长估算的大小。
该程序应该如下所示: end result
然而,每次看起来都像这样: My result
我的代码如下:
import turtle
print('************************************************')
print('This program draws a randomly colored polygon')
print('with side lengths provided by the user.')
print('************************************************')
polygonSideLength = int(input('Enter length of polygon side: \n'))
numberOfSides = int(5 + (28 / 4))
turnAngle = 360 / numberOfSides
import random
randomColor = random.randint(0,5)
if randomColor == 0:
fillcolor="red"
elif randomColor == 1:
fillcolor="green"
elif randomColor == 2:
fillcolor="blue"
elif randomColor == 3:
fillcolor="cyan"
elif randomColor == 4:
fillcolor="magenta"
elif randomColor == 5:
fillcolor="yellow"
print('Length of polygon side =', polygonSideLength)
print('Number of polygon sides =', numberOfSides)
print('Turn angle at each vertex =', turnAngle)
print('Random fill color is', fillcolor)
turtle.begin_fill()
turtle.pen(pensize = 5, pencolor="black", fillcolor = fillcolor)
count = 0
while (count < numberOfSides):
turtle.forward(polygonSideLength)
turtle.right(turnAngle)
count = count + 1
turtle.end_fill()
turtle.setheading(270)
turtle.penup()
turtle.forward(65)
turtle.left(90)
turtle.forward(130)
turtle.pendown()
turtle.write("polygon drawn by: Name", align = "right", font=("Arial", 12, "normal"))
turtle.hideturtle()
turtle.done()
有人可以帮我解决如何更改此代码以使其有效吗?
答案 0 :(得分:0)
让我们尝试最简单的路线到解决方案。首先,让我们将多边形置于窗口中心。我们可以通过添加:
来做到这一点turtle.backward(polygonSideLength / 2)
在填充多边形绘图开始之前。接下来,让它进入窗口的上半部分而不是下半部分。我们可以通过改变:
来做到这一点turtle.right(turnAngle)
为:
turtle.left(turnAngle)
即。只是向另一个方向画画。既然我们将多边形水平地中心化并且垂直于中心线上方,我们只需要在中心线下方写入居中文本,该中心线位于字体高度的位置(或两个以留出一些空间):
fontSize = 18
...
turtle.goto(0, -fontSize * 2)
turtle.write("polygon drawn by: Name", align="center", font=("Arial", fontSize, "normal"))
将所有内容放在一起,并进行一些简单的样式更改,我们得到:
import turtle
import random
colors = ["red", "green", "blue", "cyan", "magenta", "yellow"]
print('************************************************')
print('This program draws a randomly colored polygon')
print('with side lengths provided by the user.')
print('************************************************')
polygonSideLength = int(input('Enter length of polygon side: '))
numberOfSides = 5 + (28 // 4) # I assume this should be something more interesting...
turnAngle = 360 / numberOfSides
fontSize = 18
randomColor = random.randint(0, 5)
fillcolor = colors[randomColor]
print('Length of polygon side =', polygonSideLength)
print('Number of polygon sides =', numberOfSides)
print('Turn angle at each vertex =', turnAngle)
print('Random fill color is', fillcolor)
turtle.pen(pensize=5, pencolor="black", fillcolor=fillcolor)
turtle.backward(polygonSideLength / 2)
turtle.begin_fill()
for count in range(numberOfSides):
turtle.forward(polygonSideLength)
turtle.left(turnAngle)
turtle.end_fill()
turtle.penup()
turtle.setheading(270)
turtle.goto(0, -fontSize * 2)
turtle.write("polygon drawn by: Name", align="center", font=("Arial", fontSize, "normal"))
turtle.hideturtle()
turtle.done()