python使用turtle画五星红旗!
python使⽤turtle画五星红旗!
import turtle
from math import *
# 绘制五⾓星, 默认为正五⾓星(⼀个顶点朝正北⽅)
# 五⾓星每个顶⾓的⾓度为 180/5 = 36度或 pi/5
# (x, y): 五⾓星中⼼点坐标
# size: 中⼼到顶点的长度, 即外接圆的半径
鸡年限定皮肤
# angle 旋转⾓度, 正五⾓星正北顶点 turtle.left ⽅式旋转到被绘制五⾓星的⾓度
def draw5star(x=0, y=0, size=100, angle=0, fillcolor='yellow', pencolor='yellow'):
turtle.speed(0)
turtle.shape('blank')
turtle.penup()
# 定位到中⼼点正北⽅顶点(angle为0时的正北⽅)
<(x, y)
turtle.setheading(90)
turtle.left(angle)
turtle.forward(size)
turtle.right(180 - 36 / 2)
turtle.pendown()
# ⼀条直线上的两个顶点的距离
distance = 2 * size * cos(pi / 10)
# 开始绘制,对于 angle 为 0 的, 从正北⽅顶点开始
turtle.begin_fill()
for i in range(5):
turtle.forward(distance)
turtle.right(144)
# 画矩形
# (x,y) 矩形左上⾓坐标
def drawrectangle(x=0, y=0, height=100, width=100, fillcolor='red', pencolor='black'):
turtle.speed(0)
turtle.shape('blank')
turtle.penup()
<(x, y)
turtle.setheading(0)
turtle.pensize(2)
turtle.pendown()
turtle.begin_fill()
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
# 画辅助线,⽅便判断
# (x,y) 国旗中⼼点坐标
# mag 国旗放⼤倍数
def drawsubline(x=0, y=0, mag=1):
# 国旗尺⼨
幸福的鸭子读后感# 国旗尺⼨
width = 300 * mag
height = 200 * mag
猜不透的心看不透的人# 画中⼼⼗字
turtle.speed(0)
turtle.shape('blank')
turtle.pencolor('black')
turtle.penup()
<(x + width / 2, y)
turtle.setheading(180)
turtle.pensize(2)
turtle.pendown()
turtle.forward(width)
turtle.penup()
<(x, y - height / 2)
turtle.setheading(90)
turtle.pendown()
turtle.forward(height)
# 画⼩⽅格的横线
for i in range(1, 10):
# 横线
turtle.penup()
<(x, y + height / 2 - i * 10 * mag)
turtle.setheading(180)
turtle.pendown()
turtle.forward(width / 2)
# 画⼩⽅格的竖线
for i in range(1, 15):
turtle.penup()
<(x - width / 2 + i * 10 * mag, y)
turtle.setheading(90)
turtle.pendown()
turtle.forward(height / 2)
# 计算国旗矩形左上⾓坐标
r_x = x - width / 2
r_y = y + height / 2
# 画⼤五⾓星外接圆,圆绘制起始点为圆最右侧切点
turtle.penup()
<(r_x + 80 * mag, r_y - 50 * mag)
turtle.pendown()
turtle.circle(30 * mag)
# 画4个⼩五⾓星外接圆,从上⾄下 (110, 20), (130, 40), (130, 70), (110, 90)    turtle.penup()
<(r_x + 110 * mag, r_y - 20 * mag)
turtle.pendown()
turtle.circle(10 * mag)
turtle.penup()
春晚歌曲
<(r_x + 130 * mag, r_y - 40 * mag)
turtle.pendown()看完湿得最厉害的句子
turtle.circle(10 * mag)
turtle.penup()
<(r_x + 130 * mag, r_y - 70 * mag)
turtle.pendown()
turtle.circle(10 * mag)
turtle.penup()
<(r_x + 110 * mag, r_y - 90 * mag)
turtle.pendown()
turtle.pendown()
turtle.circle(10 * mag)
# 画4个⼩星到⼤星中⼼的连线
turtle.penup()
<(r_x + 100 * mag, r_y - 20 * mag)
turtle.pendown()
最帅的名字<(r_x + 50 * mag, r_y - 50 * mag)
turtle.penup()
<(r_x + 120 * mag, r_y - 40 * mag)
turtle.pendown()
<(r_x + 50 * mag, r_y - 50 * mag)
turtle.penup()
<(r_x + 120 * mag, r_y - 70 * mag)
turtle.pendown()
<(r_x + 50 * mag, r_y - 50 * mag)
turtle.penup()
<(r_x + 100 * mag, r_y - 90 * mag)
turtle.pendown()
<(r_x + 50 * mag, r_y - 50 * mag)
# 绘制五星红旗
# (x, y) 红旗中⼼点坐标,默认为(0,0),即标准模式turtle绘图的中⼼起点。
# mag: 放⼤倍数, maganification
def drawflag(x=0, y=0, mag=1):
# 国旗尺⼨
width = 300 * mag
height = 200 * mag
# 计算国旗矩形左上⾓坐标
r_x = x - width / 2
r_y = y + height / 2
# 画国旗矩形
drawrectangle(x=r_x, y=r_y, height=height, width=width)
# 画最⼤的五⾓星
draw5star(x=r_x + 50 * mag, y=r_y - 50 * mag, size=30 * mag)
# draw5star(x=r_x+50*mag, y=r_y-50*mag, size=30*mag)
# 从上⾄下画4颗⼩五⾓星, 中⼼:(100, 20), (120, 40), (120, 70), (100, 90)
l_size = 10 * mag
draw5star(x=r_x + 100 * mag, y=r_y - 20 * mag, size=l_size, angle=180 - atan(5 / 3) / pi * 180)    draw5star(x=r_x + 120 * mag, y=r_y - 40 * mag, size=l_size, angle=180 - atan(7 / 1) / pi * 180)    draw5star(x=r_x + 120 * mag, y=r_y - 70 * mag, size=l_size, angle=90 - atan(2 / 7) / pi * 180)    draw5star(x=r_x + 100 * mag, y=r_y - 90 * mag, size=l_size, angle=90 - atan(4 / 5) / pi * 180)
if __name__ == '__main__':
m = eval(input("请输⼊红旗长度(长⽐宽为3:2):\n"))
mag = m/300
n = eval(input("是否要辅助线?(0为不显⽰辅助线,1为显⽰辅助线):\n"))
if n == 0:
drawflag(mag=mag)
elif n == 1:
drawflag(mag=mag)
drawsubline(mag=mag)
else:
print("输⼊错误")
exit()
turtle.done()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。