如何阻止蟒蛇画画 [英] How to stop the python turtle from drawing

查看:30
本文介绍了如何阻止蟒蛇画画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么这段代码总是在屏幕上出现一行以及如何阻止它?

Can anyone tell me why this code always has a line on the screen and also how to stop it?

这里的一个小问题是,每次发生这种情况时,无论我尝试做什么,我的画布上总会出现一条线.关于如何防止这种情况的任何想法?

Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try to do. Any ideas on how to prevent this?

现在这可能是太多代码,但我不知道如何显示失败的代码.我尝试了多种方法,但似乎都不起作用.

Now this is probably too much code, but I have no idea how else to show the failing code. I have tried a variety of ways but none seem to work.

def drawpixel(x, y, colour):
    turtle.goto(x, y)
    turtle.dot(1, colour)

def main():
    screen = turtle.Screen()
    screen.title('Mandelbrot set drawer')
    screen.bgcolor('#22ccaa')
    turtle.hideturtle()
    turtle.speed(0)
    turtle.penup()
    turtle.tracer(-10000, 0)
    turtle.speed(0)
    width = int(turtle.numinput('Screen size', 'What width?', 600, 100, 20000))
    height = int(turtle.numinput('Screen size', 'What height?', 600, 100, 10000))
    turtle.setworldcoordinates((-width)//2, (-height)//2, (width)//2, (height)//2)
    radius = 2
    turtle.goto((-width)//2, (-height)//2)
    x, y = ((-width)//2, (-height)//2)
    while y<((height)//2 +1):
        while x!=((width)//2 +1):
            newx = x/(width//2)*radius
            newy = y/(width//2)*radius
            mpos = newx + newy*1j
            drawpixel(x, y, getcolour(mpos))
            x += 1
        y += 1
        turtle.goto((-width)//2, y)

也许 getcolour 失败了,所以这里是它的代码:

Maybe the getcolour is failing so here is the code for it:

def iterc(c):
    iters = 0
    z = 0+0j
    while iters < 16 and math.hypot(z.real, z.imag)<2:
        z = z*z+c
        iters += 1
    return iters

def getcolour(pos):
    x = iterc(pos)
    colournum = int(x*6.25)
    colour = 'gray{}'.format(colournum)

推荐答案

我已经重新编写了您的代码,以摆脱该行并修复我看到的其他问题:

I've reworked your code to get rid of the line and fix other issues that I saw:

def main():
    screen = turtle.Screen()
    screen.title('Mandelbrot Set')
    screen.bgcolor('#22ccaa')

    width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))
    height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))
    screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)
    screen.tracer(0, 0)

    radius = 2

    turtle.penup()
    turtle.hideturtle()
    turtle.speed('fastest')

    x, y = -width // 2, -height // 2
    turtle.goto(x, y)

    while y < (height // 2):

        while x < (width // 2):

            newx = x / (width // 2) * radius
            newy = y / (width // 2) * radius
            mpos = newx + newy * 1j
            drawpixel(x, y, getcolour(mpos))
            x += 1

        x, y = -width // 2, y + 1
        screen.update()

尽管有标题,但我没有把这幅画描绘成曼德布罗,但它至少应该扫描正确.

Despite its title, I don't picture this drawing a Mandelbrot but it should at least scan correctly.

更新

既然您已经提供了 getcolour(),那么 Mandelbrot 的本质就变得很清楚了.下面是完整的返工代码和一些输出:

Now that you've provided getcolour(), the Mandelbrot nature of this becomes clear. Below is the complete reworked code and some output:

import math
import turtle

def iterc(c):
    iters = 0
    z = 0 + 0j

    while iters < 16 and math.hypot(z.real, z.imag) < 2:
        z = z * z + c
        iters += 1

    return iters

def getcolour(pos):
    x = iterc(pos)
    colournum = int(x * 6.25)
    return 'gray{}'.format(colournum)

def drawpixel(x, y, colour):
    turtle.goto(x, y)
    turtle.dot(1, colour)

def main():
    screen = turtle.Screen()
    screen.title('Mandelbrot Set')
    screen.bgcolor('#22ccaa')

    width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))
    height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))
    screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)
    screen.tracer(0, 0)

    radius = 2

    turtle.penup()
    turtle.hideturtle()
    turtle.speed('fastest')

    x, y = -width // 2, -height // 2
    turtle.goto(x, y)

    while y < (height // 2):

        while x < (width // 2):

            newx = x / (width // 2) * radius
            newy = y / (width // 2) * radius
            mpos = newx + newy * 1j
            drawpixel(x, y, getcolour(mpos))
            x += 1

        x, y = -width // 2, y + 1
        screen.update()

main()

OUTPUT(缩小了尺寸,还没跑完)

OUTPUT (reduced in size, still not finished running)

既不是最漂亮也不是最快的实现,但它是一个 Mandelbrot.

Neither the prettiest nor the fastest implementation but it is a Mandelbrot.

既然您已经看到您的代码在我的系统上运行,您需要查看您的环境(Python 版本、Windows 或 Unix 等)以查看有什么不同.以上是在 Max OSX 10.11 上使用 Python 3.6.0 完成的

Now that you've seen your code works on my system, you need to review your environment (Python version, Windows or Unix, etc.) to see what's different. The above was done with Python 3.6.0 on Max OSX 10.11

这篇关于如何阻止蟒蛇画画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆