Python龟无法正确绘制

时间:2016-04-01 02:41:04

标签: python-2.7 drawing turtle-graphics

前段时间我制作了这个程序来绘制一个标志,我最近尝试运行它并且由于某种原因它没有工作,即使它是我最初编码的时候。条纹画得很好但是当涉及蓝色方形和星星时,乌龟指针不会移动到任何地方。我使用的是python 2.7.11,但这是一年前写的,我想那可能就是它不能工作的原因吗?我不记得我正在使用的是什么版本的python。

import turtle

def draw_stripes(length, height, color):
    turtle.speed('fast')
    turtle.color(color)
    turtle.begin_fill()
    turtle.pendown()
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(height)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(height)

    for i in range(0,6):
        turtle.penup()
        turtle.left(180)
        turtle.forward(height*2)
        turtle.pendown()
        turtle.right(90)
        turtle.forward(length)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
        turtle.forward(length)
        turtle.left(90)
        turtle.forward(height)

    turtle.end_fill()

def draw_canton(length, height, color):
    turtle.speed('fast')
    turtle.left(180)
    turtle.forward(height/13)
    turtle.color(color)
    turtle.begin_fill()
    turtle.right(90)
    turtle.pendown()
    turtle.forward(length*(2/5))
    turtle.right(90)
    turtle.forward(height*(7/13))
    turtle.right(90)
    turtle.forward(length*(2/5))
    turtle.right(90)
    turtle.forward(height*(7/13))
    turtle.end_fill()

def draw_star_row_six(length, height, size):
    turtle.speed('fast')
    turtle.right(90)
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(length)
    turtle.right(180)
    turtle.forward(size)
    turtle.right(163)

    for i in range (6):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*6)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (6):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*6)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (6):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*6)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (6):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*6)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (6):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length * 2)*6)
    turtle.right(90)
    turtle.forward(height*7)

def draw_star_row_five(length, height, size):
    turtle.right(90)
    turtle.forward(length)
    turtle.right(73)

    for i in range (5):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*5)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (5):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*5)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (5):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.left(253)
    turtle.forward((length*2)*5)
    turtle.left(90)
    turtle.forward(height*2)
    turtle.right(180)
    turtle.right(163)

    for i in range (5):
        draw_star(size, 'white')
        turtle.left(73)
        turtle.forward(length*2)
        turtle.right(73)

    turtle.home()

def draw_star(size, color):
    turtle.color(color)
    turtle.begin_fill()
    turtle.pendown()

    for i in range(5):
        turtle.forward(size)
        turtle.right(144)

    turtle.end_fill()
    turtle.penup()

def get_color():
    return r,g,b



def draw_flag(height):
    turtle.speed('fast')
    turtle.setx(-150)
    turtle.color('black')
    turtle.pendown()
    turtle.forward(height*1.9)
    turtle.left(90)
    turtle.forward(height)
    turtle.left(90)
    turtle.forward(height*1.9)
    turtle.left(90)
    turtle.forward(height)
    turtle.penup()
    turtle.left(90)
    draw_stripes((height*1.9), height/13, 'red')
    draw_canton((height*1.9), height, 'blue')
    draw_star_row_six(((height*1.9)*(2/5))/12,(height*(7/13))/10, ((height/13)*(4/5))/2)
    draw_star_row_five(((height*1.9)*(2/5))/12,(height*(7/13))/10, ((height/13)*(4/5))/2)

height = float(input('Enter The Height of Your Flag: '))
draw_flag(height)

1 个答案:

答案 0 :(得分:0)

这是你的问题:

  

我正在使用python 2.7.11

由于除法运算符在Python 2和Python 3之间的工作方式不同,因此只有在Python3中运行时,此程序才能正常工作。证明: enter image description here

在Python2中,除法截断:3 / 2 => 1
在Python3中,除法总是产生一个浮点数:3 / 2 => 1.5

只需在Python3中运行您的代码即可。