Python利用turtle库绘制彩虹代码示例
语言:Python
IDE:Python.IDE
需求
做出彩虹效果
颜色空间
RGB模型:光的三原色,共同决定色相
HSB/HSV模型:H色彩,S深浅,B饱和度,H决定色相
需要将HSB模型转换为RGB模型
代码示例:
#-*- coding:utf-8 C*- from turtle import * def HSB2RGB(hues): hues = hues * 3.59 #100转成359范围 rgb=[0.0,0.0,0.0] i = int(hues/60)%6 f = hues/60 -i if i == 0: rgb[0] = 1; rgb[1] = f; rgb[2] = 0 elif i == 1: rgb[0] = 1-f; rgb[1] = 1; rgb[2] = 0 elif i == 2: rgb[0] = 0; rgb[1] = 1; rgb[2] = f elif i == 3: rgb[0] = 0; rgb[1] = 1-f; rgb[2] = 1 elif i == 4: rgb[0] = f; rgb[1] = 0; rgb[2] = 1 elif i == 5: rgb[0] = 1; rgb[1] = 0; rgb[2] = 1-f return rgb def rainbow(): hues = 0.0 color(1,0,0) #绘制彩虹 hideturtle() speed(100) pensize(3) penup() goto(-400,-300) pendown() right(110) for i in range (100): circle(1000) right(0.13) hues = hues + 1 rgb = HSB2RGB(hues) color(rgb[0],rgb[1],rgb[2]) penup() def main(): setup(800, 600, 0, 0) bgcolor((0.8, 0.8, 1.0)) tracer(False) rainbow() #输出文字 tracer(False) goto(100,-100) pendown() color("red") write("安科网",align="center", font=("Script MT Bold", 80, "bold")) tracer(True) mainloop() if __name__ == "__main__": main()
效果展示:
总结
#-*- coding:utf-8 C*- <br>
更详细办法参考:python中文乱码不着急,先看懂字节和字符
以上就是本文关于Python利用turtle库绘制彩虹代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
使用Python的turtle模块画图的方法
Python中turtle作图示例
Python内置模块turtle绘图详解
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16