用户:
甘霖润万物查看:0 回复:0 评论:0 创建时间:2021-05-07T22:58:12
【作品展示】

【作品介绍】
(已更新)【去太空,趣编程】活动作品
用turtle、random、math库绘制而成,将近200行代码,求赞QAQ
【作品源代码】
import turtle as t
import random as r
mypen = t.Pen()
# 设置画笔
t.bgcolor('navy')
mypen.speed(5)
def setpen(x, y, color):
mypen.penup()
mypen.setheading(0)
mypen.goto(x, y)
mypen.pencolor(color)
mypen.fillcolor(color)
mypen.pendown()
# 设置画笔为 0 度
mypen.setheading(0)
# 画火箭
def draw_huojian():
# 上部
setpen(-25, 50, 'red')
mypen.begin_fill()
for i in range(3):
mypen.forward(75)
mypen.left(120)
mypen.end_fill()
# 中部
setpen(-25, 50, 'gray')
mypen.begin_fill()
for i in range(2):
mypen.forward(75)
mypen.right(90)
mypen.forward(170)
mypen.right(90)
mypen.end_fill()
# 舷窗
setpen(13, 0, 'white')
mypen.begin_fill()
mypen.circle(20)
mypen.end_fill()
mypen.begin_fill()
setpen(13, 10, 'blue')
mypen.circle(10)
mypen.end_fill()
# 下部
setpen(-25, -120, 'red')
mypen.begin_fill()
mypen.forward(-25)
mypen.left(45)
mypen.forward(40)
mypen.left(45)
mypen.forward(-25)
mypen.end_fill()
setpen(50, -120, 'red')
mypen.begin_fill()
mypen.setheading(90)
mypen.forward(30)
mypen.right(135)
mypen.forward(40)
mypen.right(135)
mypen.forward(25)
mypen.end_fill()
def draw_weixing():
# 方形
setpen(-300, 250, 'gray')
mypen.setheading(30)
mypen.begin_fill()
for i in range(4):
mypen.forward(50)
mypen.right(90)
mypen.end_fill()
# 左太阳能板
mypen.goto(-300, 250)
mypen.setheading(30)
mypen.forward(25)
mypen.pencolor('white')
mypen.left(90)
mypen.forward(25)
mypen.setheading(30)
mypen.forward(25)
mypen.fillcolor('blue')
mypen.begin_fill()
for i in range(2):
mypen.left(90)
mypen.forward(75)
mypen.left(90)
mypen.forward(50)
mypen.end_fill()
#右太阳能板
setpen(-300,250,'white')
mypen.penup()
mypen.right(60)
mypen.forward(50)
mypen.left(90)
mypen.forward(25)
mypen.right(90)
mypen.pendown()
mypen.forward(25)
mypen.setheading(30)
mypen.forward(25)
mypen.fillcolor('blue')
mypen.begin_fill()
for i in range(2):
mypen.right(90)
mypen.forward(75)
mypen.right(90)
mypen.forward(50)
mypen.end_fill()
def draw_dimian():
mypen.begin_fill()
setpen(-420, -150, 'brown')
for i in range(2):
mypen.forward(840)
mypen.right(90)
mypen.forward(250)
mypen.right(90)
mypen.end_fill()
def draw_yuanquan():
setpen(0, -130, 'orange')
mypen.setheading(-90)
for i in range(3):
mypen.begin_fill()
mypen.circle(r.randint(15, 20))
mypen.penup()
mypen.forward(r.randint(8, 10))
mypen.pendown()
mypen.end_fill()
setpen(5, -155, 'yellow')
mypen.setheading(-90)
for i in range(3):
mypen.begin_fill()
mypen.circle(r.randint(12, 15))
mypen.penup()
mypen.forward(r.randint(8, 10))
mypen.pendown()
mypen.end_fill()
def draw_star():
for i in range(r.randint(30, 50)):
x = r.randint((-400), (400))
y = r.randint((-100), 300)
mypen.penup()
mypen.goto(x, y)
mypen.pendown()
mypen.circle(0.75)
def rectangle(x, y, width, height, color):
# 设置画笔
setpen(x, y, color)
# 开始绘制
mypen.begin_fill()
for i in range(2):
mypen.forward(width)
mypen.right(90)
mypen.forward(height)
mypen.right(90)
mypen.end_fill()
def starmac(x, y, angle, length, color):
# 设置画笔
setpen(x, y, color)
# 设置画笔朝向
mypen.setheading(angle)
# 开始绘制
mypen.begin_fill()
for i in range(5):
mypen.forward(length)
mypen.left(72)
mypen.forward(length)
mypen.right(144)
mypen.end_fill()
def main():
draw_huojian()
draw_dimian()
draw_yuanquan()
draw_weixing()
setpen(-200, 300, 'white')
mypen.write('去太空,趣编程', font=("Arial", 48))
mypen.speed(15)
# 国旗
per = 2
mx, my = -15, -15
# 旗面
width, height = 30 * per, 20 * per
rectangle(mx, my, width, height, "red")
# 大五角星
import math
R = 3 * per
ox, oy = mx + 5 * per, my - 5 * per
fa = R * math.sin(math.radians(72))
fo = R * math.cos(math.radians(72))
ax, ay = ox - fa, oy + fo
length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
starmac(ax, ay, 0, length, "yellow")
# 第一颗小五角星
R = 1 * per
ox, oy = mx + 10 * per, my - 2 * per
angle = math.degrees((math.atan(3/5)))
oh = R * math.sin(math.radians(angle))
ah = R * math.cos(math.radians(angle))
ax, ay = ox - ah, oy - oh
length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
starmac(ax, ay, angle+18, length, "yellow")
# 第二颗小五角星
R = 1 * per
ox, oy = mx + 12 * per, my - 4 * per
angle = math.degrees((math.atan(1/7)))
oh = R * math.sin(math.radians(angle))
ah = R * math.cos(math.radians(angle))
ax, ay = ox - ah, oy - oh
length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
starmac(ax, ay, angle+18, length, "yellow")
# 第三颗小五角星
R = 1 * per
ox, oy = mx + 10 * per, my - 9 * per
angle = math.degrees((math.atan(2 / 7)))
oh = R * math.sin(math.radians(angle))
ah = R * math.cos(math.radians(angle))
ax, ay = ox - ah, oy + oh
length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
starmac(ax, ay, -(angle-18), length, "yellow")
# 第四颗小五角星
R = 1 * per
ox, oy = mx + 12 * per, my - 7 * per
angle = math.degrees((math.atan(4 / 5)))
oh = R * math.sin(math.radians(angle))
ah = R * math.cos(math.radians(angle))
ax, ay = ox - ah, oy + oh
length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
starmac(ax, ay, -(angle-18), length, "yellow")
draw_star()
setpen(50, 0, 'white')
mypen.write('发射!朝着\n梦想前进!', font=("Arial", 10))
mypen.hideturtle()
t.done()
main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn