为python的'turtle import'设置特定区域bgcolor

时间:2015-11-30 11:06:39

标签: python turtle-graphics

我在网上搜索了信息,但我能找到的就是我所知道的:

    from turtle import *
    bgcolour('gold')

现在我想要的是有一些东西可以使一半的屏幕变为一种颜色,另一半则由特定的角落coodinates分开。 e.g。

    from turtle import *
    bgcolour('gold')
    (-200, -150 to -200, 150)

因此它只会在特定区域设置。

2 个答案:

答案 0 :(得分:1)

您必须自己实施,例如像这样:

from turtle import *

def left_bgcolor(c):
    showturtle()
    penup()
    w = window_width()
    h = window_height()
    penwidth = 10
    # set a big pen width so the turtle is visible while drawing
    width(penwidth)
    # adjust position so we're inside the window border
    goto(-w/2 + penwidth/2, -h/2+penwidth)
    setheading(90)  # north

    pendown()
    color(c, c)     # set both drawing and fill color to be the same color
    begin_fill()
    forward(h - penwidth)
    right(90)
    forward(w/2 - penwidth)
    right(90)
    forward(h - penwidth)   # could be optimized..
    right(90)
    forward(w/2 - penwidth)
    right(90)
    end_fill()

    penup()
    goto(0,0)  # end by setting the turtle in the middle of the screen 
    width(1)
    color('black', 'white')
    setheading(90)

screen = Screen()
left_bgcolor('orange')

答案 1 :(得分:0)

这是标记绘图更简单的另一种情况。我还使用了一个单独的乌龟实例,因此默认乌龟保持原样供用户使用:

import turtle

STAMP_UNIT = 20

def left_bgcolor(color):
    squirtle = turtle.Turtle(shape="square", visible=False)
    squirtle.penup()
    screen = turtle.Screen()
    width = screen.window_width() / 2
    height = screen.window_height()
    squirtle.setx(-width / 2)
    squirtle.shapesize(height / STAMP_UNIT, width / STAMP_UNIT)
    squirtle.color(color)
    squirtle.stamp()

left_bgcolor('gold')

turtle.circle(100)

turtle.exitonclick()