关于python画国旗的补充

分类: python |
今天有人在博客上留言问我 ,他在运行我写的python代码中国旗的五角星中间是空的,我把代码运行一遍,结果如下:http://s8/mw690/004igrvszy76HBqieazf7&690
真的除了问题,但是我后来在windows下运行又是对的,后来看了一下版本,我的windows是python2.7.9,而我的Linux mint18默认的是python2.7.12,可能是不同版本下的fill函数填充规则的问题吧,特此将代码修改如下:
#coding=utf-8
import time
import turtle
import os
def draw_rectangle(start_x,start_y,rec_x,rec_y):
turtle.goto(start_x,start_y)
turtle.color('red')
turtle.fillcolor('red')
turtle.begin_fill()
for i in
range(2):
turtle.forward(rec_x)
turtle.left(90)
turtle.forward(rec_y)
turtle.left(90)
turtle.end_fill()
def draw_star(center_x,center_y,radius):
turtle.setpos(center_x,center_y)
#find the
peak of the five-pointed star
pt1=turtle.pos()
turtle.circle(-radius,72)
pt2=turtle.pos()
turtle.circle(-radius,72)
pt3=turtle.pos()
turtle.circle(-radius,72)
pt4=turtle.pos()
turtle.circle(-radius,72)
pt5=turtle.pos()
turtle.circle(-radius,72)
turtle.right(90)
turtle.forward(radius)
pt6=turtle.pos()
#draw the
five-pointed star
turtle.color('yellow','yellow')
turtle.fill(True)
turtle.goto(pt6)
turtle.goto(pt3)
turtle.goto(pt5)
turtle.fill(False)
turtle.fill(True)
turtle.goto(pt6)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.fill(False)
turtle.penup()
turtle.goto(pt4)
turtle.pendown()
turtle.fill(True)
turtle.goto(pt6)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.fill(False)
turtle.fill(True)
turtle.goto(pt6)
turtle.goto(pt2)
turtle.goto(pt4)
turtle.fill(False)
turtle.penup()
turtle.goto(pt1)
turtle.pendown()
turtle.fill(True)
turtle.goto(pt6)
turtle.goto(pt3)
turtle.goto(pt1)
turtle.fill(False)
turtle.penup()
#start the project
turtle.speed(5)
turtle.penup()
#draw the rectangle
star_x=-320
star_y=-260
len_x=660
len_y=440
draw_rectangle(star_x,star_y,len_x,len_y)
#draw the big star
pice=660/30
big_center_x=star_x+5*pice
big_center_y=star_y+len_y-pice*5
turtle.goto(big_center_x,big_center_y)
turtle.left(90)
turtle.forward(pice*3)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice*3)
#draw the small star
turtle.goto(star_x+10*pice,star_y+len_y-pice*2)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the second star
turtle.goto(star_x+pice*12,star_y+len_y-pice*4)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the third
turtle.goto(star_x+pice*12,star_y+len_y-7*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the final
turtle.goto(star_x+pice*10,star_y+len_y-9*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
turtle.ht()
time.sleep(3)
os._exit(1)
运行结果如下:http://s5/mw690/004igrvszy76HBEStzS14&690
另外这里也有一份别人的代码 最后画出来的效果是一样的,大家也可以看看:
# -*- coding: utf-8 –*-
import turtle
import math
def draw_polygon(aTurtle, size=50, n=3):
'''
绘制正多边形
args:
aTurtle: turtle对象实例
size: int类型,正多边形的边长
n:
int类型,是几边形
'''
for i in
xrange(n):
aTurtle.forward(size)
真的除了问题,但是我后来在windows下运行又是对的,后来看了一下版本,我的windows是python2.7.9,而我的Linux mint18默认的是python2.7.12,可能是不同版本下的fill函数填充规则的问题吧,特此将代码修改如下:
#coding=utf-8
import time
import turtle
import os
def draw_rectangle(start_x,start_y,rec_x,rec_y):
def draw_star(center_x,center_y,radius):
#start the project
turtle.speed(5)
turtle.penup()
#draw the rectangle
star_x=-320
star_y=-260
len_x=660
len_y=440
draw_rectangle(star_x,star_y,len_x,len_y)
#draw the big star
pice=660/30
big_center_x=star_x+5*pice
big_center_y=star_y+len_y-pice*5
turtle.goto(big_center_x,big_center_y)
turtle.left(90)
turtle.forward(pice*3)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice*3)
#draw the small star
turtle.goto(star_x+10*pice,star_y+len_y-pice*2)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the second star
turtle.goto(star_x+pice*12,star_y+len_y-pice*4)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the third
turtle.goto(star_x+pice*12,star_y+len_y-7*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the final
turtle.goto(star_x+pice*10,star_y+len_y-9*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
turtle.ht()
time.sleep(3)
os._exit(1)
另外这里也有一份别人的代码 最后画出来的效果是一样的,大家也可以看看:
# -*- coding: utf-8 –*-
import turtle
import math
def draw_polygon(aTurtle, size=50, n=3):