用python海龟绘制五星红旗(怎么用python海龟画五角星)

http://www.itjxue.com  2023-03-06 21:15  来源:未知  点击次数: 

请问一下网友老铁们 美国国旗用python怎么做呀 求其代码 谢谢拉

参考下五星红旗

code#!/usr/bin/env python

# -*- coding: utf-8 –*-

''' 对于turtle类的一些封装方法,包括画正多边形,正多角形和五星红旗。'''

__author__ = 'Hu Wenchao'

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)

aTurtle.left(360.0/n)

def draw_n_angle(aTurtle, size=50, num=5, color=None):

''' 绘制正n角形,默认为黄色

args:

aTurtle: turtle对象实例

size: int类型,正多角形的边长

n: int类型,是几角形

color: str, 图形颜色,默认不填色

'''

if color:

aTurtle.begin_fill()

aTurtle.fillcolor(color)

for i in xrange(num):

aTurtle.forward(size)

aTurtle.left(360.0/num)

aTurtle.forward(size)

aTurtle.right(2*360.0/num)

if color:

aTurtle.end_fill()

def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):

''' 根据起始位置、结束位置和外接圆半径画五角星

args:

aTurtle: turtle对象实例

start_pos: int的二元tuple,要画的五角星的外接圆圆心

end_pos: int的二元tuple,圆心指向的位置坐标点

radius: 五角星外接圆半径

color: str, 图形颜色,默认不填色

'''

aTurtle = aTurtle or turtle.Turtle()

size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)

aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))

aTurtle.penup()

aTurtle.goto(start_pos)

aTurtle.fd(radius)

aTurtle.pendown()

aTurtle.right(math.degrees(math.pi*9/10))

draw_n_angle(aTurtle, size, 5, color)

def draw_5_star_flag(times=20.0):

''' 绘制五星红旗

args:

times: 五星红旗的规格为30*20, times为倍数,默认大小为10倍, 即300*200

'''

width, height = 30*times, 20*times

# 初始化屏幕和海龟

window = turtle.Screen()

aTurtle = turtle.Turtle()

aTurtle.hideturtle()

aTurtle.speed(10)

# 画红旗

aTurtle.penup()

aTurtle.goto(-width/2, height/2)

aTurtle.pendown()

aTurtle.begin_fill()

aTurtle.fillcolor('red')

aTurtle.fd(width)

aTurtle.right(90)

aTurtle.fd(height)

aTurtle.right(90)

aTurtle.fd(width)

aTurtle.right(90)

aTurtle.fd(height)

aTurtle.right(90)

aTurtle.end_fill()

# 画大星星

draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow')

# 画四个小星星

stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]

for pos in stars_start_pos:

draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow')

# 点击关闭窗口

window.exitonclick()

if __name__ == '__main__':

draw_5_star_flag()

/code

如何用海龟画图画一面五星红旗?

代码如下

import turtle as t

t.speed(10)

t.pensize(1)

t.fillcolor('red')

t.pencolor('black')

t.shape(name='classic')

t.tracer(True)

#旗布

print('1.旗布')

t.penup()

t.left(135)

t.fd(350)

t.right(135)

t.pendown()

t.begin_fill()

for i in range(2):

t.fd(500)

t.right(90)

t.fd(350)

t.right(90)

t.end_fill()

#就位

t.fd(417)

t.penup()

t.right(90)

t.fd(30)

t.left(18)

t.pendown()

#星1*大

print('2.大五角星')

t.fillcolor('yellow')

t.begin_fill()

for j in range(5):

t.fd(40)

t.left(72)

t.fd(40)

t.right(144)

t.end_fill()

#星2*小1

print('3.小五角星,一')

t.right(108)

t.penup()

t.fd(85)

t.left(90)

t.fd(10)

t.pendown()

t.left(18)

t.begin_fill()

for j in range(5):

t.fd(10)

t.left(72)

t.fd(10)

t.right(144)

t.end_fill()

t.right(18)

#星3*小2

print('4.小五角星,二')

t.penup()

t.fd(50)

t.left(90)

t.fd(10)

t.right(90)

t.pendown()

t.left(18)

t.begin_fill()

for j in range(5):

t.fd(10)

t.left(72)

t.fd(10)

t.right(144)

t.end_fill()

t.right(18)

#星4*小3

print('5.小五角星,三')

t.penup()

t.fd(50)

t.left(90)

t.fd(30) #左右位置

t.right(90)

t.pendown()

t.left(18)

t.begin_fill()

for j in range(5):

t.fd(10)

t.left(72)

t.fd(10)

t.right(144)

t.end_fill()

t.right(18)

#星5*小4

print('6.小五角星,四')

t.penup()

t.fd(15)

t.left(90)

t.fd(55) #左右位置

t.right(90)

t.pendown()

t.left(18)

t.begin_fill()

for j in range(5):

t.fd(10)

t.left(72)

t.fd(10)

t.right(144)

t.end_fill()

t.right(18)

t.shape(name='blank')

print('_行程结束_')

t.done()

如何用python的turtle画五角星

turtle.seth(angle):只改变海龟的行进方向(角度按逆时针),但不行进,angle为绝对度数

求一段python中用class方法绘制国旗的代码!记得不是常见的海龟做法!这个星期给我再加送财富点!

from?matplotlib?import?patches,?pyplot?as?plt

from?math?import?sin,?cos,?pi

fig?=?plt.figure(figsize=(6,?4))

ax?=?fig.add_subplot(111)

def?star(coord,?size,?rotate):

pts?=?[(size?*?sin(i?*?4?*?pi?/?5?+?rotate)?+?coord[0],?size?*?cos(i?*?4?*?pi?/?5?+?rotate)?+?coord[1])?for?i?in?range(5)]

return?patches.Polygon(pts,?fc='yellow',?ec='yellow')

ax.add_patch(patches.Rectangle([0,?-2],?3,?2,?fc='red',?ec='red'))

ax.add_patch(star((0.5,?-0.5),?0.3,?0.0))

ax.add_patch(star((1.0,?-0.2),?0.07,?0.3))

ax.add_patch(star((1.2,?-0.4),?0.07,?0.9))

ax.add_patch(star((1.2,?-0.7),?0.07,?0.0))

ax.add_patch(star((1.0,?-0.9),?0.07,?0.3))

ax.set_axis_off()

plt.axis('scaled')

plt.show()

import?turtle??

import?time??

import?os??

#??

def??draw_square(org_x,?org_y,?x,?y):??

????turtle.setpos(org_x,?org_y)??#?to?left?and?bottom?connor??

????turtle.color('red',?'red')??

????turtle.begin_fill()??

????turtle.fd(x)??

????turtle.lt(90)??

????turtle.fd(y)??

????turtle.lt(90)??

????turtle.fd(x)??

????#print(turtle.pos())??

????turtle.lt(90)??

????turtle.fd(y)??

????turtle.end_fill()??

??

def?draw_star(center_x,?center_y,?radius):??

????print(center_x,?center_y)??

????turtle.pencolor('black')??

????turtle.setpos(center_x,?center_y)??

????pt1?=?turtle.pos()??

????turtle.circle(-radius,?360?/?5)??

????pt2?=?turtle.pos()??

????turtle.circle(-radius,?360?/?5)??

????pt3?=?turtle.pos()??

????turtle.circle(-radius,?360?/?5)??

????pt4?=?turtle.pos()??

????turtle.circle(-radius,?360?/?5)??

????pt5?=?turtle.pos()??

????turtle.color('yellow',?'yellow')??

????turtle.begin_fill()??

????turtle.goto(pt3)??

????turtle.goto(pt1)??

????turtle.goto(pt4)??

????turtle.goto(pt2)??

????turtle.goto(pt5)??

????turtle.end_fill()??

print(turtle.pos())??

??

turtle.pu()??

draw_square(-320,?-260,?660,?440)??

star_part_x?=?-320??

star_part_y?=?-260?+?440??

star_part_s?=?660?/?30??

center_x,?center_y?=?star_part_x?+?star_part_s?*?5,?star_part_y?-?star_part_s?*?5??

turtle.setpos(center_x,?center_y)??#?big?star?center??

turtle.lt(90)??

draw_star(star_part_x?+?star_part_s?*?5,?star_part_y?-?star_part_s?*?2,?star_part_s?*?3)??

??

#?draw?1st?small?star??

turtle.goto(star_part_x?+?star_part_s?*?10,?star_part_y?-?star_part_s?*?2)????#?go?to?1st?small?star?center??

turtle.lt(round(turtle.towards(center_x,?center_y))?-?turtle.heading())??

turtle.fd(star_part_s)??

turtle.rt(90)??

draw_star(turtle.xcor(),?turtle.ycor(),?star_part_s)??

??

#?draw?2nd?small?star??

turtle.goto(star_part_x?+?star_part_s?*?12,?star_part_y?-?star_part_s?*?4)????#?go?to?1st?small?star?center??

turtle.lt(round(turtle.towards(center_x,?center_y))?-?turtle.heading())??

turtle.fd(star_part_s)??

turtle.rt(90)??

draw_star(turtle.xcor(),?turtle.ycor(),?star_part_s)??

??

#?draw?3rd?small?star??

turtle.goto(star_part_x?+?star_part_s?*?12,?star_part_y?-?star_part_s?*?7)????#?go?to?1st?small?star?center??

turtle.lt(round(turtle.towards(center_x,?center_y))?-?turtle.heading())??

turtle.fd(star_part_s)??

turtle.rt(90)??

draw_star(turtle.xcor(),?turtle.ycor(),?star_part_s)??

??

#?draw?4th?small?star??

turtle.goto(star_part_x?+?star_part_s?*?10,?star_part_y?-?star_part_s?*?9)????#?go?to?1st?small?star?center??

turtle.lt(round(turtle.towards(center_x,?center_y))?-?turtle.heading())??

turtle.fd(star_part_s)??

turtle.rt(90)??

draw_star(turtle.xcor(),?turtle.ycor(),?star_part_s)??

turtle.ht()??

time.sleep(5)??

os._exit(1)

两个版本,仅供参考

(责任编辑:IT教学网)

更多

推荐3DMAX教程文章