Turtle Python 3,创建一个 "mosaic tile",其中包含用户提供的行数和列数,每行和列的中间标记一个海龟

Turtle Python 3, Create a "mosaic tile" with a user supplied number of rows and columns and each with a turtle stamped in the middle of it

我需要使用特定函数创建代码,以在用户提供的行和列数量中创建矩形。

我想要做的是让第一个矩形从左上角开始,然后填充指定行和列的其余矩形。然后它在每个矩形的中心标记乌龟。

到目前为止,我的代码绘制了第一个矩形,然后它只为其余矩形绘制了很长的线,然后海龟出现了。

我目前有这个:

import turtle
import random


# -----------------------------------------+
# draw_tile                                |
# -----------------------------------------+
# rectangle: the name of the turtle        |
# width: width of tile                     |
# height: height of tile                   |
# color: color of the tile                 |
# -----------------------------------------+
# Draw a rectangle with specfied fill      |
# color and black border. Call another     |
# function to stamp the tile.              |    
# -----------------------------------------+

rows = int(input("Enter number of rows: "))
columns = int(input("Enter number of columns: "))

def pick_color():
    colors = ["blue", "green", "yellow", "orange", "brown", "red", "pink", 
"turquoise"]
    random.shuffle(colors)
    return colors[0]



def draw_tile(rectangle, width, height):
    rectangle = turtle.Turtle()
    rectangle.fillcolor(pick_color())
    rectangle.begin_fill()
    rectangle.penup()
    rectangle.goto(-345,175)
    rectangle.pendown()
    for i in range(4):
        rectangle.forward(width)
        rectangle.left(90)
        rectangle.forward(height)
    for i in range(rows):
        rectangle.forward(width + width*i)

    rectangle.end_fill()

# -----------------------------------------+
# stamp_tile                               |
# -----------------------------------------+
# stamper: the name of the turtle          |
# w: width of the tile                     |
# h: height of the tile                    |
# -----------------------------------------+
# Stamp a black arrow in the center of     |
# the tile, pointing up.                   |
# -----------------------------------------+

def stamp_tile(stamper, w,h):
    stamper = turtle.Turtle()
    stamper.shape('turtle')
    stamper.color('green')
    stamper.stamp()
    stamper.penup()
    stamper.goto(-330,200)
    stamper.pendown()
    stamper.left(90)





# -----------------------------------------+
# main                                     |
# -----------------------------------------+
# This function has no parameters.         |
# -----------------------------------------+
# This function should  prompt the user    |
# for number of rows and columns, then     |
# draw a mosaic of randomly colored tiles. |
# Call the other two functions as needed.  |
# -----------------------------------------+
def main():

    t = turtle.Turtle()
    window = turtle.Screen()
    t.speed(0)
    draw_tile(t,200,50)
    stamp_tile(t,200,50)


    t.hideturtle()

# Call the main function to begin executing
main()

我需要它看起来像这样:

What I need my code to create for the "mosaic tiles"

感谢任何回答此问题并能帮助我的人!

I just need help figuring out how to have multiple rectangles

太好了,让我们先清理一下绘制单个图块的代码:

def draw_tile(rectangle, width, height):
    rectangle = turtle.Turtle()

我们不需要创建一个新的海龟,因为一个正在以名称 rectangle.

传入
    rectangle.fillcolor(pick_color())
    rectangle.begin_fill()

根据您自己的文档,color 作为最后一个参数传入,因此我们将添加它并将调用移至其他地方的 pick_color()

    rectangle.penup()
    rectangle.goto(-345,175)
    rectangle.pendown()

既然我们要用这个例程绘制多个瓦片,那么像(-345, 175)那样把它绑定到一个特定的位置是没有意义的,所以我们就把它扔掉。

    for i in range(4):
        rectangle.forward(width)
        rectangle.left(90)
        rectangle.forward(height)

此代码已损坏,它应该重复两次,而不是四次,因为它每次迭代都会绘制两条边。它应该包括最后一个额外的 90 度左转。我们会解决这个问题。

    for i in range(rows):
        rectangle.forward(width + width*i)

这段代码除了绘制方块之外什么都不做,所以我们就扔掉它。

    rectangle.end_fill()

这是修改后的 draw_tile():

def draw_tile(rectangle, width, height, color):
    rectangle.pendown()
    rectangle.fillcolor(color)
    rectangle.begin_fill()

    for i in range(2):
        rectangle.forward(width)
        rectangle.left(90)
        rectangle.forward(height)
        rectangle.left(90)

    rectangle.end_fill()
    rectangle.penup()

用户提供了rowscolumns,我们需要相应地堆叠瓷砖。我们可以使用的是嵌套循环:

for row in range(rows):
    for column in range(columns):

我们不能简单地 t.goto(row, column) 因为那样会忽略图块的尺寸,所以我们需要做类似的事情:

        t.goto(column * 200, row * 100)

哪个应该有效。但是,它不会在屏幕上居中。这是一个更复杂的计算:

         t.goto(column * 200 - (columns * 100), row * 100 - (rows * 50))

我们可以轻松地分解此代码语句并更快地完成其中的某些部分。但我把它留给程序员作为练习。让我们把它们放在一起:

from turtle import Screen, Turtle
from random import choice

COLORS = ["blue", "green", "yellow", "orange", "brown", "red", "pink", "turquoise"]

# -----------------------------------------+
# draw_tile                                |
# -----------------------------------------+
# rectangle: the name of the turtle        |
# width: width of tile                     |
# height: height of tile                   |
# color: color of the tile                 |
# -----------------------------------------+
# Draw a rectangle with specfied fill      |
# color and black border. Call another     |
# function to stamp the tile.              |
# -----------------------------------------+

def draw_tile(rectangle, width, height, color):
    rectangle.pendown()
    rectangle.fillcolor(color)
    rectangle.begin_fill()

    for i in range(2):
        rectangle.forward(width)
        rectangle.left(90)
        rectangle.forward(height)
        rectangle.left(90)

    rectangle.end_fill()
    rectangle.penup()

def pick_color():
    return choice(COLORS)

# -----------------------------------------+
# main                                     |
# -----------------------------------------+
# This function has no parameters.         |
# -----------------------------------------+
# This function should  prompt the user    |
# for number of rows and columns, then     |
# draw a mosaic of randomly colored tiles. |
# Call the other two functions as needed.  |
# -----------------------------------------+

def main():

    rows = int(input("Enter number of rows: "))
    columns = int(input("Enter number of columns: "))

    window = Screen()

    t = Turtle()
    t.speed('fastest')
    t.penup()

    for row in range(rows):
        for column in range(columns):
            t.goto(column * 200 - (columns * 100), row * 100 - (rows * 50))
            draw_tile(t, 200, 100, pick_color())

    t.hideturtle()
    window.exitonclick()

# Call the main function to begin executing
main()

现在您需要弄清楚如何在每个瓷砖上盖章。