海王星上全是大海吗?为什么土星竟然有62个月亮,而地球只有一个月亮?据说喜欢仰望天空的是充满好奇心的人….
以下是部分代码预览:
"""本程序实现星空中闪烁烁的星星,然后有七颗星星会连起来,它们是北斗七星,还有启明星。""" from star import * from time import sleep "定义全局变量" stars_amounts = 49 width ,height = 800,600 #屏幕宽高 beidou_cors = [(-276,110),(-125,135),(-38,88),(76,38),(100,-64),(284,-70),(314,52)] #北斗七星坐标表 beidou_names = ["瑶光","开阳","玉衡","天权","天玑","天旋","天枢"] #名称表 images_small = ["star1.gif","star2.gif"] #星星的小造型images_small列表 images_big = ["big_star1.gif","big_star2.gif"] #星星的大造型images_big列表 s1 = "北斗七星,是由天枢、天璇、天玑、天权、玉衡、开阳、瑶光七星组成的。" s2 = "古代中国人民把这七星联系起来想象成为古代舀酒的斗形。" s3 = "天枢、天璇、天玑、天权组成为斗身,古曰魁;玉衡、开阳、摇光组成为斗柄,古曰杓。" s4 = "道教称北斗七星为七元解厄星君,居北斗七宫。以下是它们的名称:" s5 = "天枢宫贪狼星君、天璇宫巨门星君、天玑宫禄存星君、天权宫文曲星君、" s6 = "玉衡宫廉贞星君、开阳宫武曲星君、瑶光宫破军星君。" knowledge = [s1,s2,s3,s4,s5,s6] # 放在列表中,方便遍历 "生成屏幕对象" screen = Screen() screen.title("昨夜星辰_作者:李兴球") screen.setup(width,height) screen.delay(0) # 绘画延时为0 screen.bgpic("北斗定位坐标用.png") screen.addshape("star1.gif") # 注册形状 screen.addshape("star2.gif") screen.addshape("big_star1.gif") # 注册形状 screen.addshape("big_star2.gif") screen.update() sleep(1) "生成星星49颗" stars = [] # 创建stars列表 for i in range(stars_amounts): # 重复stars_amounts次 "Star参数为:图形列表,x坐标,y坐标" stars.append(Star(images_small,randint(-width/2,width/2),randint(0,height/2))) #新建Star,并添加到stars列表 #sleep(randint(1,2)/4) screen.delay(6) # 绘画延时为6 "以下等待几秒钟,不直接用sleep(5)是防止窗口无响应" for i in range(5): sleep(1) screen.update() "定位前7颗星星到北斗七星的坐标位置" for i in range(7): star = stars.pop() # 弹出一颗星星 x,y = beidou_cors[i] # 取出x,y坐标 star.goto(x,y) # 定位到此坐标 star.images = images_big "准备连线" t.pencolor("gray") # 画笔颜色为灰 t.pensize(2) # 画笔迹宽为2 t.goto(beidou_cors[0]) # 定位到北斗第一颗星的坐标 t.write(beidou_names[0],font=("黑体",14,"normal"))# 写第一颗星的名字 t.pendown() # 落笔,准备画连线 for i in range(1,7): # 把其它六颗星连线 t.goto(beidou_cors[i]) t.write(beidou_names[i],font=("黑体",14,"normal")) t.goto(beidou_cors[3]) screen.title("北 斗 七 星") "剩下的都隐藏" for i in range(stars_amounts-7): star = stars.pop() # 弹出最后的 star.ht() # 隐藏 "以下是写字,包括标题和北斗七星的小知识。" t.penup() t.pencolor("yellow") t.goto(0,200) t.write("北 斗 七 星",align='center',font=("黑体",24,"bold")) t.pencolor("cyan") t.goto(-340,-160) for s in knowledge: # 遍历“知识”列表 t.write(s,font=("宋体",12,"normal")) t.sety(t.ycor() - 24) screen.mainloop()
下面是star模块,star.py源代码:
from turtle import * from random import randint from time import sleep class Star(Turtle): def __init__(self,images,x,y): Turtle.__init__(self,visible=False) # 父类初始化 self.penup() # 抬笔 self.images = images # 图形列表 self.index = 0 # 索引号 self.goto(x,y) # 定位坐标 self.twinkle() # 调用闪烁方法 self.st() # 显示 if __name__ == "__main__": screen = Screen() screen.setup(800,600) screen.title("一闪一闪亮晶晶") screen.delay(0) screen.bgpic("bg2.png") screen.addshape("star1.gif") screen.addshape("star2.gif") images = ["star1.gif","star2.gif"] [Star(images,randint(-400,400),randint(0,400)) for i in range(50)] screen.mainloop()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)