如何在Python中显示多个图纸?

时间:2019-10-09 14:20:21

标签: python turtle-graphics

我有这段代码可以从文件中读取图形。文件的第一行对应于尺寸,其他行代表图形。到目前为止,此代码仅打印一个数字。我想做的是在同一窗口中用不同的颜色打印4个相等的数字。像这样:

https://i.stack.imgur.com/Suldn.png

import turtle

# Initial params for drawing
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Maze")
wn.setup(700, 700)

class Pen(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("square")
        self.penup()
        self.speed(0)

# Function used to read the figure from a file
def read():
    figure= list()
    with open("figure1.txt") as f:
        header = f.readline()
        for line in f:
            figure.append(list(line.rstrip()))
    # dimensions[0]: height / dimensions[1]: width
    dimensions = header.split(' ')
    return dimensions, figure


# Function used to draw the figure
def setup(figure, pen):
    for y in range(len(figure)):
        for x in range(len(figure[0])):
            character = figure[y][x]
            screen_x = -288 + (x * 24)
            screen_y = 288 - (y * 24)

            if character == "-":
                pen.color("gray")
                pen.goto(screen_x, screen_y)
                pen.stamp()
            elif character == "*":
                pen.color("white")
                pen.goto(screen_x, screen_y)
                pen.stamp()



def main():
    dimensions, figure = read()

    pen = Pen()
    setup(figure, pen)

    wn.tracer(0)
    turtle.mainloop()


if __name__ == '__main__':
    main()

是否可以使用乌龟库打印多个屏幕?

0 个答案:

没有答案