Python教程

Python圣诞树-夜空下的圣诞森林

本文主要是介绍Python圣诞树-夜空下的圣诞森林,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

圣诞森林

代码如下:

from turtle import*
import random
from math import*

t = cos(radians(heading()+45))/8+0.25
delay(0)
hideturtle()
screensize(1200,1200,'black')
speed(0)
goto(-700,-280)
pencolor('white')
pensize(222)
pendown()
seth(0)
forward(1400)

def curvemove(x,y):
    penup()
    goto(x,y)
    pensize(1)
    color('red','pink')        
    begin_fill()
    left(140)
    forward(2.1165)
    for i in range(200):
        right(1)
        forward(0.05)
    left(120)
    for i in range(200):
        right(1)
        forward(0.05)
    forward(2.1165)
    end_fill()
def stars1 (x,y,left_angle,edge_len):
    pensize(1)
    pencolor('white')
    fillcolor('white')
    penup()
    goto(x,y)
    begin_fill()
    pendown()
    left(left_angle)
    for _ in range(5):
        forward(edge_len)
        right(144)
    end_fill()
    left(-left_angle)


def stars (x,y,left_angle,edge_len):
    pensize(1)
    pencolor('yellow')
    fillcolor('yellow')
    penup()
    goto(x,y)
    begin_fill()
    pendown()
    left(left_angle)
    for _ in range(5):
        forward(edge_len)
        right(144)
    end_fill()
    left(-left_angle)
for _ in range(40):
    x = random.randint(-700,700)
    y = random.randint(-100,700)
    edge_len = random.randint(3,8)
    left_angle = random.randint(0,180)
    stars(x,y,left_angle,edge_len)
penup()

def tree1(d, s):
    if d <= 0: return
    if d > 6:
        pensize(d)
        pencolor(t,t,t)
    if d<=6 and d>3:
        pensize(10)
        pencolor('dark green')
    if d <=3:
        pensize(5)
        pencolor('seagreen')
    forward(s)
    tree1(d-1, s*.8)
    right(120)
    tree1(d-3, s*.5)
    right(120)
    tree1(d-3, s*.5)
    right(120)
    backward(s)

def tree2(n,k):
    if n <= 0: return
    if n > 5:
        pensize(n)
        pencolor(t,t,t)
    if n <=5:
        pensize(8)
        pencolor('seagreen')
    forward(k)
    tree2(n-1, k*.8)
    right(120)
    tree2(n-3, k*.5)
    right(120)
    tree2(n-3, k*.5)
    right(120)
    backward(k)
def tree3(n,k):
    if n <= 0: return
    if n > 5:
        pensize(n)
        pencolor(t,t,t)
    if n <=5:
        pensize(8)
        pencolor('dark green')
    forward(k)
    tree3(n-1, k*.8)
    right(120)
    tree3(n-3, k*.5)
    right(120)
    tree3(n-3, k*.5)
    right(120)
    backward(k)
def tree0(o,p,z,q):
    goto(z,q)
    seth(90)
    pendown()
    tree2(o,p)
    penup()
def tree(d,s,a,b):
    goto(a,b)
    seth(90)
    pendown()
    tree1(d,s)
    penup()
    seth(90)
    fd(d/5)
    pendown()
    seth(-90)
    pencolor(t,t,t)
    pensize(d*2)
    fd(d/3)
    penup()
    seth(90)
    for _ in range(20):
        f=random.randint(a-100,a+100)
        g=random.randint(b,s)
        x=random.randint(a-100,a+100)
        y=random.randint(b,s)
        edge_len = random.randint(3,8)
        left_angle = random.randint(0,180)
        stars(f,g,left_angle,edge_len)
        curvemove(x,y)
    for _ in range(30):
        x1 = random.randint(a-100,a+100)
        y1 = random.randint(b,s)
        edge_len1 = random.randint(3,8)
        left_angle1 = random.randint(0,180)
        stars1(x1,y1,left_angle1,edge_len1)
    penup()
def treez(o,p,z,q):
    goto(z,q)
    seth(90)
    pendown()
    tree3(o,p)
    penup()


for _ in range(8):
    x = random.randint(-700,700)
    w = random.randint(-700,700)
    e = random.randint(-700,700)
    treez(10,10,x,-170)
    treez(8,20,w,-170)
    tree0(5,15,e,-170)
for _ in range(3):
    x = random.randint(-600,800)
    w = random.randint(-600,800)
    e = random.randint(-600,800)
    tree0(8,28,x,-173)
    treez(12,37,w,-174)
    tree0(13,45,e,-186)
for _ in range(2):
    x = random.randint(-500,700)
    w = random.randint(-500,800)
    e = random.randint(-500,800)
    tree0(13,60,x,-175)
    tree0(12,55,w,-187)
    tree0(13,70,e,-180)


tree(15,82,-600,-188)
goto(-600,200)
pensize(2)
pencolor('yellow')
fillcolor('yellow')
begin_fill()
seth(-72)
pendown()
for _ in range(5):
    forward(35)
    right(144)
end_fill()

for _ in range(400):
    x = random.randint(-700,700)
    y = random.randint(-700,700)
    edge_len = random.randint(3,4)
    left_angle = random.randint(0,180)
    stars1(x,y,left_angle,edge_len)
penup()

最后星星亮起来那一下感觉还可以,喜欢的家人们可以试一试
最后的效果

这篇关于Python圣诞树-夜空下的圣诞森林的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!