2016-09-28 49 views
0

我是一個初學者在python和我正在寫一個簡單的程序與蟒蛇龜,提示用戶輸入一個多邊形的邊長,程序應該繪製多邊形並在多邊形下打印出人物(我)的名字。如何對齊蟒蛇與n邊繪製多邊形中的文本

我已經得到程序的工作,但我似乎無法弄清楚如何讓文字在多邊形下打印,因爲邊長可以由用戶改變,所以多邊形有時可以看不見,取決於邊長的估算值。

的程序應該是這樣的: My result

我的代碼如下: end 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

讓我們試着走最簡單的路線到解決方案。首先,讓我們讓你的多邊形居中在窗口上。我們可以做的是通過添加:填充多邊形繪圖開始

turtle.backward(polygonSideLength/2) 

之前。接下來,讓我們進入窗口的上半部分而不是下半部分。我們可以做到這一點通過改變:

turtle.right(turnAngle) 

到:

turtle.left(turnAngle) 

即只是在另一個方向繪製它。現在,我們有多邊形水平centertered和中心線的垂直上方,我們只需要編寫由字體的高度偏移中心線下方居中的文本(或兩個,使一些空間):

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() 

enter image description here