本文實例為大家分享了python繪制雪景圖的具體代碼,供大家參考,具體內(nèi)容如下
成都服務(wù)器托管,成都創(chuàng)新互聯(lián)提供包括服務(wù)器租用、綿陽服務(wù)器托管、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、域名注冊等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:18980820575繪制雪景圖,應(yīng)用到turtle和random。
from turtle import * from random import * # 繪制隨機(jī)雪花 def DrawSnow(snow): x = randrange(-750, 750) y = randrange(-50, 400) snowcolor = (randrange(0, 255), randrange(0, 255), randrange(0, 255)) snowsize = randrange(10, 20) snowstyle = choice([30, 45]) snow.penup() snow.setpos(x, y) snow.pendown() colormode(255) snow.pencolor(snowcolor) for i in range(int(360/snowstyle)): if snowsize < 11: snowstyle = 90 snow.forward(snowsize) snow.backward(snowsize) snow.right(snowstyle) #繪制上半部分雪花 def InitSnow(n): snow = Turtle() for i in range(n): DrawSnow(snow) #繪制下半部分雪地 def InitField(): field = Turtle() colormode(255) for i in range(300): x = randrange(-850, 850) y = randrange(-400, 0) width = randrange(50, 200) length = randrange(3, 8) field.penup() field.setpos(x, y) field.pendown() tempcolor = int(-255 * y/400) print(tempcolor) fieldcolor = (tempcolor, tempcolor, tempcolor) field.color(fieldcolor, fieldcolor) field.begin_fill() field.fd(width) field.circle(length, 180) field.fd(width) field.circle(length, 180) field.end_fill() def main(): bgcolor("black") tracer(False) InitField() InitSnow(200) tracer(True) mainloop() if __name__ == '__main__': main()