• 技术文章 >Python技术 >Python基础教程

    如何用python海龟库画彩虹

    流芳流芳2020-06-30 11:01:05转载4604

    python生成的彩虹效果,大家可以参考:

    001.jpg

    使用的python版本:3.7.0 ,以下为源代码:

     # rainbow.py
     
    from turtle import *
    from random 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.5, 0.8, 1.0))
    	tracer(False)
    	rainbow()
    	#输出文字
    	goto(100,-100)
    	pendown()
    	color("red")
    	write("Rainbow",align="center",font=("Script MT Bold", 80, "bold"))
    	tracer(True)
    	mainloop()
     
     
    if __name__ == '__main__':
    	main()

    推荐课程:Python基础视频教程(python语言基础)

    专题推荐:海龟库
    上一篇:python代码怎么用cmd打开 下一篇:python中怎么取绝对值

    相关文章推荐

    • Python如何运用turtle绘制阴阳太极图• Python怎么引入turtle• python turtle模块有哪几种命令

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网