从整个画布保存大的龟图形图像

问题描述

我已经环顾了几个小时,但找不到针对此特定问题的任何解决方案。我确实看到一个人问了同样的问题,但是他们没有提供代码,因此也没有得到答案。我的问题是这样:

我有一个程序,可以将乌龟随机左右移动一定的圈数。车削部分可以工作,但是当我尝试保存画布时,它仅保存可见部分,这通常会切断内容。我尝试增加设置画布的大小以及屏幕截图,但是保存的文件始终以左上角结尾,因此被截断了。我正在附上我的整个程序(简短地说,不用担心),因为我不确定确切的方面需要更改。

任何帮助将不胜感激!

import turtle
import random

'''
Takes a variable,number,of how many random turns the turtle should make and a variable filenumber,which is used to save multiple different iterations of the program per program execution
'''

def left_or_right(number,filenumber):

    # Counters to make sure the turtle doesn't turn too many times
    count = 0
    subright = 0
    subleft = 0

    #sets up screen
    win_width,win_height,bg_color = 10000,10000,'white'
    turtle.setup()
    turtle.screensize(win_width,bg_color)

    # Clears the screen,sets the turtle in the middle of the screen,drops the pen and hides the
    # Turtle. If you want to see the turtles do the drawing,turn turtle.tracer to True,and
    # uncomment out turtle.speed('fastest')

    turtle.clear()
    turtle.home()
    turtle.hideturtle()
    turtle.pendown()
    turtle.speed('fastest')
    turtle.tracer(False)

    # A for loop for the turtle's turns
    for i in range(number):
        # Calls decider to you can get a random direction every time
        decider = random.randint(0,1)
        # in order to minimize the turtle just going around in circles,# if the turtle has made more that 3 of the same turn,turn it the other way
        # the next to if statements deal with the turtle turning right too many times
        if decider == 1 and subright <= 3:
            turtle.right(90)
            turtle.forward(20)
            count += 1
            subright += 1

        elif decider == 1 and subright > 3:
            turtle.left(90)
            turtle.forward(20)
            count += 1
            subright = 0

        # The next two if statements make sure the turtle doesn't turn right too many times
        elif decider == 0 and subleft <= 3:
            turtle.left(90)
            turtle.forward(20)
            count += 1
            subleft += 1

        elif decider == 0 and subleft > 3:
            turtle.right(90)
            turtle.forward(20)
            count += 1
            subleft = 0

    # if the number of turns is equal to what the user inputed,stop making the turns
    # and update the screen
    if count == number:
        turtle.penup()
        turtle.update()

    # Take a screen capture of the screen and saves it to where the python file is
    ts = turtle.getscreen()
    ts.getcanvas().postscript(file=filenumber,height=10000,width=10000)

    # turtle.done() is here as a debugging tool. If you set the number of files you're
    # generating to one,you can keep the turtle window open to check that the file correctly
    # copied the turtle screen
    #turtle.done()


''' Takes a variable,filenumber,which is the number of generated files you want'''

def filegen(filenumber):

    for i in range(filenumber):
        # Calls the left or right function as many times as you told it to
        # Note: this is where the left or right function get's its turn number parameters
        left_or_right(10000,i)

def main():

    # calls filegen as many times as you want it to
    filegen(4)

if __name__ == '__main__':
    main()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)