【问题标题】:Where to position fill color in a turtle graphic function in python 3python 3中乌龟图形函数中填充颜色的位置
【发布时间】:2018-02-03 03:14:57
【问题描述】:

我正在用 Think like a Computer Scientist 练习函数,我想 在一个以画一个八边形为目的的练习中做一些变化。我希望用特定颜色填充八边形,但我不确定将颜色填充代码放在哪里。仅使用笔颜色就可以正常工作。这就是我所做的,但结果是八边形的外部和内部都是黑色:

import turtle

wn =turtle.Screen()
wn.bgcolor("yellow")


def drawOctagon(t, sz,ang):
"""Make turtle t draw an octagon of size sz with angle ang"""

    for i in range(8):
        t.forward(sz)
        t.left(ang)


t = turtle.Turtle()        
sz = 70
ang = 360/8
t.color("white,white")
t.begin_fill()
drawOctagon(t,sz, ang)
t.end_fill()
wn.exitonclick()

有什么建议吗?

谢谢!

【问题讨论】:

    标签: python-3.x function turtle-graphics


    【解决方案1】:

    你基本上拥有它。只需更改此行:

    t.color("white,white")
    

    改为阅读:

    t.color("white", "white")
    

    虽然黄底白字很难看!以下是我对您的代码进行了上述修复和一些样式更改的返工:

    from turtle import Turtle, Screen
    
    my_size = 70
    my_angle = 360 / 8
    
    def drawPolygon(turtle, size, angle):
        """ Make 'turtle' draw a polygon of 'size' with 'angle' """
    
        for _ in range(int(360 / angle)):
            turtle.forward(size)
            turtle.left(angle)
    
    wn = Screen()
    wn.bgcolor("yellow")
    
    yertle = Turtle()
    yertle.color("white", "white")
    
    yertle.begin_fill()
    drawPolygon(yertle, my_size, my_angle)
    yertle.end_fill()
    
    wn.exitonclick()
    

    将角度传递给名为drawOctagon() 的例程对我来说没有意义,因为它应该已经知道角度,所以我将其概括为drawPolygon()。可以将my_angle计算中的分母改成六边形等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多