最简单的方法是制作4到4的印章?

时间:2016-12-11 22:20:05

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

(对不起,对Python来说还是新手) 这是我的代码:

from turtle import Turtle, Screen


coordinate_calc = 20  ## Will be used later to calculate equal coordinates
                      ## for the turtles.

def pattern_printer1(turtle, start, stop, step):
    '''Calculates coordinates and sends turtles to them, and stamps in
a tessellating pattern.'''
    for x in range(start, stop + 1, step):
        for y in range(start, stop + 1, step):
            turtle.goto(x * coordinate_calc, y * coordinate_calc)
            turtle.stamp()

def square_perameters1(big_color, small_color, size):
    '''Constructs two separate turtles to create the large star shape,
and the small. Takes two color and one size parameters.'''
    alex = Turtle(shape="square") ## Creates the first turtle in the shape of a square.
    alex.shapesize(size)
    alex.color(big_color)
    alex.penup()


    pattern_printer1(alex, -12, 12, 12) ## Calls pattern printing function.

    tess = Turtle(shape="square") ## Creates the second turtle in the shape of a square.
    tess.shapesize(size/2) ## To fit the pattern, the second star shape will be
                           ## one half of the size of the first.
    tess.color(small_color)
    tess.penup()

    pattern_printer1(tess, -6, 6, 12) ## Calls pattern printing function.

def pattern_printer2(turtle, start, stop, step):
    '''Calculates coordinates and sends turtles to them, and stamps in
a tessellating pattern.'''
    turtle.lt(45)
    for x in range(start, stop + 1, step):
        for y in range(start, stop + 1, step):
            turtle.goto(x * coordinate_calc, y * coordinate_calc)
            turtle.stamp()

def square_perameters2(big_color, small_color, size):
    '''Constructs two separate turtles to create the large star shape,
and the small. Takes two color and one size parameters.'''

    alex = Turtle(shape="square") ## Creates the first turtle in the shape of a square.
    alex.shapesize(size + .5)
    alex.color(big_color)
    alex.penup()


    pattern_printer2(alex, -12, 12, 12) ## Calls pattern printing function.

    tess = Turtle(shape="square") ## Creates the second turtle in the shape of a square.
    tess.shapesize((size + .5)/2) ## To fit the pattern, the second star shape will be
                                  ## one half of the size of the first.
    tess.color(small_color)
    tess.penup()

    pattern_printer2(tess, -6, 6, 12) ## Calls pattern printing function.

square_perameters1("dark red", "orange", 8)
square_perameters2("dark red", "orange", 8)

screen = Screen()

screen.exitonclick() ## If the window is clicked after the turtles have finished
                     ## drawing, the window will close.

如果可能的话,我希望这个模式是4乘4,而不是3乘3。我试图在范围中添加另一个参数,但当然,它只接受三个。那么,我是否需要重新编写此代码的大部分内容?或者我可以简单地添加另一行?

提前致谢!

2 个答案:

答案 0 :(得分:1)

你非常接近!您遇到的问题是拨打pattern_printer1pattern_printer2pattern_printer函数的参数是turtle, start, stop, stepturtle位显而易见,但让我们谈谈其他三位。在Python中,范围排除最后一个元素。所以,list(range(10, 15)) == [10, 11, 12, 13, 14]。即使您正在使用某个步骤,这也是正确的。

您致电pattern_printer1(alex, -12, 12, 12)pattern_printer1创建range(start, stop + 1, step)。如果我们插入函数调用中的值,我们可以看到正在创建的范围是range(-12, 12 + 1, 12)

现在,根据我们对Python范围的了解,我们可以计算出范围内的值:[-12, 0, 12]。如果您写的是range(start, stop, step)而不是range(start, stop + 1, step),那么12就不会被包含在内。

无论如何,您想要在图纸中添加第四行和第二列。为此,我们需要确保我们的产品系列中有四个项目!我们可以增加范围的界限,也可以减少步长。

在这种情况下,增加范围的大小 - 例如,调用pattern_printer1(alex, -24, 12, 12) - 会创建正确大小的范围,但会导致部分在屏幕外绘制。

因此,我们最好的选择是将大小从12减小到8,因为8也会平均分为24。果然,如果您将函数调用更改为pattern_printer1(alex, -12, 12, 8),您将看到一个非常漂亮的4x4网格。

答案 1 :(得分:1)

你似乎已经采用solution I provided earlier并使其尽可能复杂。让我们回到原始的,更简单的解决方案,并使其可以根据需要进行调整:

from turtle import Turtle, Screen

PATTERN_WIDTH = 4
BIG_COLOR = "dark red"
SMALL_COLOR = "orange"

def print_pattern(turtle, start, stop, size, offset=0):
    for x in range(start, stop + 1):
        for y in range(start, stop + 1):
            turtle.goto(x * STAMP_WIDTH * size + offset, y * STAMP_WIDTH * size + offset)
            turtle.stamp()

def configure_turtle(size, color):
    turtle = Turtle(shape="star")
    turtle.shapesize(size)
    turtle.color(color)
    turtle.speed("fastest")
    turtle.penup()

    return turtle

STAMP_WIDTH = 6

star = ( \
    ( 3,  0), ( 2, -1), ( 2, -2), ( 1, -2), \
    ( 0, -3), (-1, -2), (-2, -2), (-2, -1), \
    (-3,  0), (-2,  1), (-2,  2), (-1,  2), \
    ( 0,  3), ( 1,  2), ( 2,  2), ( 2,  1), \
 )

screen = Screen()

screen.register_shape("star", star)

screen_width = (screen.window_width() // STAMP_WIDTH)

star_size = int(screen_width / PATTERN_WIDTH)

alex = configure_turtle(star_size, BIG_COLOR)

print_pattern(alex, -PATTERN_WIDTH//2 -1, PATTERN_WIDTH//2 + 1, star_size)

tess = configure_turtle(star_size // 2, SMALL_COLOR)

print_pattern(tess, -PATTERN_WIDTH//2 - 1, PATTERN_WIDTH//2 + 1, star_size, star_size * STAMP_WIDTH//2)

screen.exitonclick()

现在您可以将PATTERN_WIDTH设置为您需要的内容。 (尝试1到10!)这次的其他更改是,代码不是标记正方形并旋转它(上次稍微模式化),代码定义了我们需要标记为多边形的形状。数学略有改变,以适应轻松改变图案宽度。

<强>输出

enter image description here