Valentine Project Using Python

Valentine day project using python turtle

Introduction

Valentine’s Day, a day of love and affection, is just around the corner. Why not celebrate it in a unique way this year? Let’s create a beautiful animation of hearts using Python’s Turtle module. This project is not only fun but also a great way to learn and understand Python programming.

Required Python Modules

This project requires two Python modules: turtle and math. The turtle module is a part of Python’s standard library and is used for creating graphics. The math module provides mathematical functions and is used in this project to calculate the shape of the heart.

Code Explanation

Let’s break down the code into smaller parts for better understanding:

  1. Importing Modules: We start by importing the necessary modules. turtle for graphics and math for mathematical functions.

  2. Setting up the Environment: We create a turtle object t and a screen wn. We set the turtle’s shape to “turtle”, the background color to “lavender blush”, and the turtle’s speed to 4.

  3. Color Function: This function sets the pen color based on the iteration number. We have a list of colors, and we choose the color by taking the remainder of the iteration number divided by the length of the color list.

  4. At Function: This function moves the turtle to a specific location on the screen without drawing anything.

  5. Heart Function: This function draws a heart. It takes two parameters: size and shape. The size determines the size of the heart, and the shape determines the shape of the heart. We use the sin function from the math module and some geometry to calculate the radius of the circles that form the heart.

  6. Hearts Function: This function draws a series of hearts. For each iteration, it changes the color, moves the turtle down, and draws a heart with increasing size.

  7. Displaying the Message: We move the turtle to specific locations and use the write method to display a message.

Source Code:

				
					from math import sin, pi
import turtle
t = turtle.Turtle()
wn = turtle.Screen()
t.shape("turtle")
wn.bgcolor("lavender blush")
t.speed(4)

colors = ["red", "deep pink", "light pink", "midnight blue", "blue", "dodger blue", "deep sky blue"]

def color(iteration):
    t.pencolor(colors[iteration % len(colors)])
    
def at(x, y):
    t.penup()
    t.home()
    t.forward(x)
    t.left(90)
    t.forward(y)
    t.pendown()

def heart(size, shape):
    t.pensize(5)
    radius = size * sin(shape * pi / 180) / (1 + sin((90 - shape) * pi / 180))
    t.right(shape)
    t.forward(size)
    t.circle(radius, 180 + shape)
    t.right(180)
    t.circle(radius, 180 + shape)
    t.forward(size)
    t.left(180 - shape)

def hearts():
    turtle.delay(0)
    for iteration in range(1, 14):
        color(iteration)
        at(0, iteration * -5)
        heart(iteration * 10, 45)
        
t.penup()
t.goto(0,150)
t.color("red")
t.write("Hey! this is for you....", None, "center", "14pt bold")
t.goto(1,-125)
        
hearts()

t.penup()
t.goto(0,-100)
t.color("red")
t.write("Roses are red.", None, "center", "14pt bold")
t.goto(1,-125)
t.write("Violets are blue.", None, "center", "14pt bold")
t.goto(2,-150)
t.write("I like coding.", None, "center", "14pt bold")
t.goto(3,-175)
t.write("Do you do to?", None, "center", "14pt bold")
t.color("white")
t.goto(0,-300)

				
			
Valentine day project using python turtle output
More Python Projects
Get Huge Discounts

Output:

Find More Projects

library management system using python with source code using Python GUI Tkinter (Graphical User Interface) How to Run the code: Introduction:  Imagine …

Space Shooter Game Using Python with source code Overview: A space shooter game typically involves controlling a spaceship to navigate through space …

Hotel Management System Using Python with source code Introduction: Welcome to our blog post introducing a helpful tool for hotels: the Tkinter-based …

Student Management System Using Python Introduction: The Student Management System is a comprehensive software solution designed to streamline the process of managing …

Billing Management System using Python introduction: The Billing software using python is a simple yet effective Python application designed to facilitate the …

Medical Management System using Python with Complete Source code [By using Flask Framework] Introduction Hospital Management System The Hospital Management System is …

All Coding Handwritten Notes

Browse Handwritten Notes