Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Implementing Cherry Blossom Animations with Python Turtle Graphics

Notes 1

Creating Cherry Blossom Trees with Turtle Graphics

Basic Tree Structure with Falling Petals

import turtle
import random

# Initialize turtle settings
drawing_turtle = turtle.Turtle()
window = turtle.Screen()
window.bgcolor('wheat')
drawing_turtle.hideturtle()
window.tracer(5, 0)

def draw_branch(length, turtle_obj):
    if length > 3:
        # Set branch color and thickness
        if 8 <= length <= 12:
            turtle_obj.color('snow' if random.randint(0, 2) == 0 else 'lightcoral')
            turtle_obj.pensize(length / 3)
        elif length < 8:
            turtle_obj.color('snow' if random.randint(0, 1) == 0 else 'lightcoral')
            turtle_obj.pensize(length / 2)
        else:
            turtle_obj.color('sienna')
            turtle_obj.pensize(length / 10)
        
        turtle_obj.forward(length)
        angle_variation = 1.5 * random.random()
        turtle_obj.right(20 * angle_variation)
        
        # Recursive branch drawing
        length_reduction = 1.5 * random.random()
        draw_branch(length - 10 * length_reduction, turtle_obj)
        turtle_obj.left(40 * angle_variation)
        draw_branch(length - 10 * length_reduction, turtle_obj)
        turtle_obj.right(20 * angle_variation)
        turtle_obj.penup()
        turtle_obj.backward(length)
        turtle_obj.pendown()

def create_petals(count, turtle_obj):
    for _ in range(count):
        x_offset = 200 - 400 * random.random()
        y_offset = 10 - 20 * random.random()
        turtle_obj.penup()
        turtle_obj.forward(y_offset)
        turtle_obj.left(90)
        turtle_obj.forward(x_offset)
        turtle_obj.pendown()
        turtle_obj.color('lightcoral')
        turtle_obj.circle(1)
        turtle_obj.penup()
        turtle_obj.backward(x_offset)
        turtle_obj.right(90)
        turtle_obj.backward(y_offset)

# Position turtle and draw tree
drawing_turtle.left(90)
drawing_turtle.penup()
drawing_turtle.backward(150)
drawing_turtle.pendown()
drawing_turtle.color('sienna')

draw_branch(60, drawing_turtle)
create_petals(200, drawing_turtle)
window.exitonclick()

Alternative Implementation with Mathematical Functions

from turtle import *
from random import *
from math import *

def generate_tree(levels, size):
    pendown()
    # Create shadow effect
    shadow_intensity = cos(radians(heading() + 45)) / 8 + 0.25
    pencolor(shadow_intensity, shadow_intensity, shadow_intensity)
    pensize(levels / 3)
    forward(size)
    
    if levels > 0:
        right_angle = random() * 15 + 10
        left_angle = random() * 15 + 10
        next_size = size * (random() * 0.25 + 0.7)
        
        # Draw right branch
        right(right_angle)
        generate_tree(levels - 1, next_size)
        
        # Draw left branch
        left(right_angle + left_angle)
        generate_tree(levels - 1, next_size)
        right(left_angle)
    else:
        # Draw leaves
        right(90)
        leaf_color = cos(radians(heading() - 45)) / 4 + 0.5
        pencolor(leaf_color, leaf_color * 0.8, leaf_color * 0.8)
        circle(3)
        left(90)
        
        # Add falling leaves
        if random() > 0.7:
            penup()
            current_heading = heading()
            new_angle = -40 + random() * 40
            setheading(new_angle)
            distance = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)
            forward(distance)
            setheading(current_heading)
            pendown()
            right(90)
            leaf_color = cos(radians(heading() - 45)) / 4 + 0.5
            pencolor(leaf_color * 0.5 + 0.5, 0.4 + leaf_color * 0.4, 0.4 + leaf_color * 0.4)
            circle(2)
            left(90)
            penup()
            current_heading = heading()
            setheading(new_angle)
            backward(distance)
            setheading(current_heading)
    penup()
    backward(size)

# Setup and execution
bgcolor(0.5, 0.5, 0.5)
hideturtle()
speed(0)
tracer(0, 0)
penup()
backward(100)
left(90)
penup()
backward(300)
generate_tree(12, 100)
done()

Colorful Tree with Falling Flowers

from turtle import *
import random

def draw_colored_tree(branch_length):
    if branch_length > 1:
        # Adjust colors based on branch length
        if branch_length < 30 and branch_length > 14:
            pensize(4)
        elif branch_length < 15 and branch_length > 5:
            color('#04B486')  # Green leaves
            pensize(3)
        elif branch_length < 5 and branch_length > 1:
            color('#FE2E9A')  # Pink flowers
            pensize(2)
        else:
            color('#5E5E5E')  # Gray branches
            pensize(5)
        
        angle_variation = 2 * random.random()
        length_variation = 2 * random.random()
        
        forward(branch_length)
        right(20 * angle_variation)
        draw_colored_tree(branch_length - 10 * length_variation)
        left(40 * angle_variation)
        draw_colored_tree(branch_length - 10 * length_variation)
        right(20 * angle_variation)
        penup()
        backward(branch_length)
        pendown()

def falling_blossoms(flower_count):
    start_x, start_y = -1000, -750
    for column in range(30):
        penup()
        goto(start_x, start_y)
        start_x += 100
        pendown()
        vertical_offset = 50
        
        for flower in range(flower_count):
            horizontal_distance = 100 * random.random()
            vertical_shift = 2 * random.random()
            
            if horizontal_distance > 59:
                color('#FE2E9A')  # Pink
            else:
                color('#04B486')  # Green
            
            circle(5)
            penup()
            goto(start_x, start_y + (vertical_offset * vertical_shift))
            forward(horizontal_distance)
            vertical_offset += 50
            pendown()

# Setup canvas
setworldcoordinates(-1000, -750, 1000, 750)
tracer(False)
falling_blossoms(10)
bgcolor("#F5F6CE")
color('#5E5E5E')
pensize(5)

penup()
goto(0, -700)
pendown()
left(80)
forward(140)
draw_colored_tree(120)
input()

Execution Methods

To run these scripts, save them as .py files and execute with Python. For interactive environments like IPython, copy the code directly in to the console. To create standalone executables:

pyinstaller -F -w filename.py

For Python 2.x, add raw_input() at the end to prevent immediate window closure. For Python 3.x, use input() instead.

Addditional Drawing Examples

The turtlle graphics library supports various creative projects including:

  • Heart shapes with mathematical curves
  • Christmas trees with decorative ornaments
  • Character drawings like pandas or cartoon figures
  • Birthday cakes with multiple layers and candles
  • Complex floral patterns like roses with layered petals

Each implementation demonstrates different aspects of turtle graphics programming, including recursive algorithms, color manipulation, and coordinate system management.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.