My turtle

Сhristmas tree with snowflakes

 

Task: Build Сhristmas tree with snowflakes


Code:

from turtle import *
import random
from math import sqrt
from random import randint

bgcolor("dark blue")
speed(0)

def move(x,y):
    penup()
    goto(x,y)
    pendown()

def field():    
    speed(0)    
    color('blue')#snow
    begin_fill()
    move(-400,0)
    goto(400,0)
    goto(400,-250)
    goto(-400,-250)
    goto(-400,0)
    end_fill()

def randommove (min_x, max_x, min_y, max_y):
    x = random.uniform(min_x, max_x)
    y = random.uniform(min_y, max_y)
    penup()
    goto(x,y)
    pendown()

### snowflak random

colors = ["#7D8A2E", "#263248", "#FF8C00", "#F0C600"]

def snowflake(size, pensize):
    #penup()
    #goto(x, y)
    forward(5*size)
    left(45)
    pendown()
    color(random.choice(colors))

    for i in range(8):
        branch(size)
        left(45)

# create one branch of the snowflake
def branch(size):
    for i in range(3):
        for i in range(3):
            forward(10.0*size/3)
            backward(10.0*size/3)
            right(45)
        left(90)
        backward(10.0*size/3)
        left(45)
    right(90)
    forward(10.0*size)

def tree (c,s): #c — leg of the triangle, S — number of cycles
    def triangle (c):
        color("dark green")
        begin_fill()
        #color("white")
        #dot(20)
        #color("dark green")
        setheading(315)
        forward(c)
        color("white")
        dot(20)
        color("dark green")
        setheading(180)
        forward(sqrt(2*c*c))#hypotenuse
        color("white")
        dot(20)
        color("dark green")
        setheading(45)
        forward(c)
        end_fill()

    for i in range (s):
        c=c*3/2 #enlarge the next triangle to 3/2c
        triangle(c)
        setheading(270)
        forward(c/3) #to descend to с/3

def ray (ang):
    color("yellow")
    for i in range(50):
        dis = randint(5, 40)
        ang = ang + 15
        pensize(2)
        setheading(ang)
        forward(dis)
        backward(dis)

def star(size,colir):
    color(colir)
    begin_fill()
    for i in range(5):
        forward(size)
        left(144)
    end_fill()

field()

for i in range(10):
    h = random.uniform(1, 3)
    randommove (-400, 400, 0, 400)
    snowflake(h/2, 10)

move(-200,200)

tree(30,5)

move(-200,200)

ray(0)

move(-200,200)
setheading(220)
forward(-20)
star(40, 'red')

hideturtle()
mainloop()

2026-07-09 10:06:06

Comments

No comments yet. Be the first to leave one!

Leave a Comment