脚本专栏 
首页 > 脚本专栏 > 浏览文章

python使用turtle库与random库绘制雪花

(编辑:jimmy 日期: 2024/10/26 浏览:3 次 )

本文实例为大家分享了python绘制雪花的具体代码,供大家参考,具体内容如下

代码非常容易理解,画着玩玩还是可以的。直接上代码

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 14:35:14 2018

@author: Administrator
"""

from turtle import *
from random import *

def ground():
 hideturtle()
 speed(100)
 for i in range(400):
  pensize(randint(5,10))
  x=randint(-400,350)
  y=randint(-280,-1)
  r=-y/280
  g=-y/280
  b=-y/280
  pencolor(r,g,b)
  penup()
  goto(x,y)
  pendown()
  forward(randint(40,100))

def snow():
 hideturtle()
 speed(100)
 pensize(2)
 for i in range(100):
  r=random()
  g=random()
  b=random()
  pencolor(r,g,b)
  penup()
  setx(randint(-350,350))
  sety(randint(1,270))
  pendown()
  dens=randint(8,12)
  snowsize=randint(10,14)
  for j in range(dens):
   forward(snowsize)
   backward(snowsize)
   right(360/dens)

def main():
 setup(800, 600, 0, 0)
 tracer(False)
 bgcolor("black")
 snow()
 ground()
 tracer(True)
 mainloop()
main()

运行结果

python使用turtle库与random库绘制雪花

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:django admin 后台实现三级联动的示例代码
下一篇:Python3导入CSV文件的实例(跟Python2有些许的不同)
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap