In this tutorial, we will learn how to make a Happy New Year Animated Greeting Card using a python turtle. The sample for the animated card displayed to us below with this tutorial, we can wish our friends and family members.

Python Turtle
In Python turtle is used draw different shapes,picture and design on the screen.Here turtle is work as a pen and screen is work as drawing board where different shapes,pictureand design are drawn.
Happy New Year Code
In the following code, we will import the turtle library from which we can create a Happy New Year animated card.
- width = height = 500 which we use to assign or give the width and height to the window in which our animation card for Happy New Year is generated.
- win.bgcolor(“sky blue”) is used to give the “sky blue ” color to the background of the window.
- win.title(“Pythontpoint”) is used to give the title to the window.
- turtle_name.color(colour) is used to give the color to the turtle from which we can write or draw anything on the animated card.
- turtle_name.setposition(x, y) is used to move the turtle to an absolute position.
- turtle_name.dot(size) is used to create a dot with the help of the turtle.
- sb.color(“white”) is used to give the white color to the turtle.
Block of Codes:
In this block of code, we have created a list of snowballs through which we randomly generate the snowball and store that into the list.
list_of_sbs = []
def make_sb():
sb = turtle.Turtle()
sb.color("white")
sb.penup()
sb.setposition(random.randint(-2*width, width/2), height/2)
sb.hideturtle()
sb.size = random.randint(*sb_size)
list_of_sbs.append(sb)
Block of codes:
In this block of code, we will create a snowman body with the help of a circle and these circles are drawn with the help of a turtle.
sm = turtle.Turtle()
x_position = 0
y_positions = 75, 0, -100
size = 75
for y in y_positions:
make_circle(sm, x_position, y, size, "white")
size = size *1.5
Block of codes:
In this block of code, we created the eye of a snowman. We can draw the eye with the help of a small circle and these circles are drawn with the help of a turtle.
y_offset = 10
x_seperation = 15
for x in x_position-x_seperation, x_position+x_seperation:
make_circle(sm, x, y_positions[0] + y_offset, 20, "green")
make_circle(sm, x, y_positions[0] + y_offset, 10, "black")
Block of codes:
In this block code, we will create the nose of a snowman. We can draw the nose in the shape of a triangle and this triangle shape is created with the help of a turtle.
sm.color("orange")
sm.setposition(x_position - 10, y_positions[0]-y_offset)
sm.shape("triangle")
sm.setheading(200)
sm.turtlesize(0.5, 2.5)
- ground.fillcolor(“forest green”) is used to fill the color in the ground.
- ground.forward(width) is used to move the turtle in the forward direction.
- ground.left(90) is used to move the turtle in the left direction.
- txt.color(“Purple”) is used to give purple color to the turtle.
- txt.write(“Pythontpoint”, font=(“purple”, 30, “bold”), align=”center”) is used to write the text on the screen.
from turtle import *
import turtle
import random
import time
width = height = 500
win = turtle.Screen()
win.setup(width, height)
win.bgcolor("sky blue")
win.title("Pythontpoint")
sb_rate = 1, 3
sb_size = 5, 15
wind_change = 1, 5
max_wind = 3
def make_circle(turtle_name, x, y, size, colour):
turtle_name.color(colour)
turtle_name.penup()
turtle_name.setposition(x, y)
turtle_name.dot(size)
list_of_sbs = []
def make_sb():
sb = turtle.Turtle()
sb.color("white")
sb.penup()
sb.setposition(random.randint(-2*width, width/2), height/2)
sb.hideturtle()
sb.size = random.randint(*sb_size)
list_of_sbs.append(sb)
def move_sb(turtle_name, falling_speed=1, wind=0):
turtle_name.clear()
turtle_name.sety(turtle_name.ycor() - falling_speed)
if wind:
turtle_name.setx(turtle_name.xcor() + wind)
turtle_name.dot(turtle_name.size)
sm = turtle.Turtle()
x_position = 0
y_positions = 75, 0, -100
size = 75
for y in y_positions:
make_circle(sm, x_position, y, size, "white")
size = size *1.5
button_seperation = 25
button_y_positions = [y_positions[1]-button_seperation,
y_positions[1],
y_positions[1]+button_seperation]
for y in button_y_positions:
make_circle(sm, x_position, y, 10, "black")
y_offset = 10
x_seperation = 15
for x in x_position-x_seperation, x_position+x_seperation:
make_circle(sm, x, y_positions[0] + y_offset, 20, "green")
make_circle(sm, x, y_positions[0] + y_offset, 10, "black")
sm.color("orange")
sm.setposition(x_position - 10, y_positions[0]-y_offset)
sm.shape("triangle")
sm.setheading(200)
sm.turtlesize(0.5, 2.5)
win.tracer(0)
ground = turtle.Turtle()
ground.fillcolor("forest green")
ground.penup()
ground.setposition(-width/2, -height/2)
ground.begin_fill()
for _ in range(2):
ground.forward(width)
ground.left(90)
ground.forward(70)
ground.left(90)
ground.end_fill()
ground = turtle.Turtle()
for x in range(int(-width/2), int(width/2), int(width/200)):
make_circle(ground, x, -180, random.randint(5, 20), "white")
txt = turtle.Turtle()
txt.color("Purple")
txt.penup()
txt.setposition(-100,190)
txt.write("Pythontpoint", font=("purple", 30, "bold"), align="center")
txt.setposition(130, 150)
txt.color("green")
txt.write("Wishing You ", font=("blue", 30, "bold"), align="right")
txt.color("red")
txt.setx(10)
txt.setposition(90, 110)
txt.write("Happy New Year", font=("red", 30, "normal"), align="center")
#txt.write("from", font=("Apple Chancery", 20, "bold"), align="right")
txt.hideturtle()
time_delay = 0
start_time = time.time()
wind = 0
wind_delay = 5
wind_timer = time.time()
wind_step = 0.1
while True:
if time.time() - start_time > time_delay:
make_sb()
start_time = time.time()
time_delay = random.randint(*sb_rate)/10
for sb in list_of_sbs:
move_sb(sb, wind=wind)
if sb.ycor() < -height/2:
sb.clear()
list_of_sbs.remove(sb)
if time.time() - wind_timer > wind_delay:
wind += wind_step
if wind >= max_wind:
wind_step = -wind_step
elif wind <= 0:
wind_step = abs(wind_step)
wind_timer = time.time()
wind_delay = random.randint(*wind_change)/10
win.update()
turtle.done()
Output:
After running the above code we get the following output in which we can see that a beautiful Happy New Year Animated Card is drawn on the screen with the help of turtle.

Do follow the following tutorials also:

Comments are closed.