Python使用turtle库与random库绘制雪花
记录Python使用turtle库与random库绘制雪花,代码非常容易理解,画着玩玩还是可以的。
完整代码如下:
# -*- 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 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17
- python使用turtle库与random库绘制雪花
- 使用Python中的Turtle库绘制简单的图形
- Python编程练习:使用 turtle 库完成玫瑰花的绘制
- python使用turtle绘制分形树
- Python编程练习:使用 turtle 库完成正方形的绘制
- Python实现使用turtle绘制圣诞树
- Python使用Turtle模块绘制五星红旗代码示例
- python使用Turtle库绘制动态钟表
- 使用python中turtle库绘制图形
- 使用Python的turtle库实现七段数码管绘制
- python 包的使用 (三)——turtle:使用海龟图形(turtle graphics)绘制图像
- Python3使用turtle绘制超立方体图形示例
- python使用turtle库绘制树
- Python编程练习:使用 turtle 库完成六边形的绘制
- python使用Turtle库绘制动态钟表
- python使用turtle库绘制时钟
- python使用turtle库绘制时钟
- Python 绘图—使用Turtle库绘制动态钟表
- python写红包的原理流程包含random,lambda其中的使用和见简单介绍
- 在Python中使用matplotlib模块绘制数据图的示例