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

yipeiwu_com6年前Python基础

本文实例为大家分享了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设计】。

相关文章

python中使用 xlwt 操作excel的常见方法与问题

前言 Python可以操作Excel的模块不止一种,我习惯使用的写入模块是xlwt(一般都是读写模块分开的) python中使用xlwt操作excel非常方,和Java使用调框架apac...

深入解析python中的实例方法、类方法和静态方法

深入解析python中的实例方法、类方法和静态方法

1、实例方法/对象方法 实例方法或者叫对象方法,指的是我们在类中定义的普通方法。 只有实例化对象之后才可以使用的方法,该方法的第一个形参接收的一定是对象本身 2、静态方法 (1).格式...

详解将Python程序(.py)转换为Windows可执行文件(.exe)

详解将Python程序(.py)转换为Windows可执行文件(.exe)

python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! /post/165763.htm),并要将软件打包为可...

python 图片验证码代码分享

复制代码 代码如下: #coding: utf-8 import Image,ImageDraw,ImageFont,os,string,random,ImageFilter def i...

tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用

tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用

1.创建tfrecord tfrecord支持写入三种格式的数据:string,int64,float32,以列表的形式分别通过tf.train.BytesList、tf.train.I...