🧮 Π Vs the Numbers – a Cinematic Python Turtle Animation

by Ranvi25 in Circuits > Computers

78 Views, 1 Favorites, 0 Comments

🧮 Π Vs the Numbers – a Cinematic Python Turtle Animation

Screenshot 2026-04-04 204328.png
Screenshot 2026-04-04 204345.png

What if numbers could fight?

In this project, I created a short animated “battle scene” using Python’s turtle graphics where the mathematical constant π faces off against a swarm of numbers. The animation includes orbiting motion, a dramatic invasion, screen shake, and a final explosive victory.

This is a great beginner-to-intermediate project for learning animation concepts in Python.

Supplies

A computer (Windows/Mac/Linux)

Python 3 installed

A Python editor (Thonny recommended for beginners)

Basic understanding of Python (loops, functions)

Set Up Your Environment

Open your Python editor and create a new file.

Import the required modules:


import turtle
import time
import random
import math

These allow us to:

  1. draw graphics (turtle)
  2. control timing (time)
  3. randomize movement (random)
  4. calculate circular motion (math)


Create the Screen

Set up the animation window:


screen = turtle.Screen()
screen.bgcolor("black")
screen.title("π vs The Numbers")
screen.tracer(0)

We also enable RGB color mode to avoid color errors:


turtle.colormode(255)


Create the Drawing Pen


pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)

This turtle will draw everything on screen.

Draw Π and Numbers

Create reusable functions:


def draw_pi(x, y, size=40, color=(255, 0, 0)):
pen.penup()
pen.goto(x, y)
pen.color(color)
pen.write("π", align="center", font=("Arial", size, "bold"))


def draw_number(n, x, y, size=18):
pen.penup()
pen.goto(x, y)
pen.color(255, 255, 255)
pen.write(str(n), align="center", font=("Arial", size, "bold"))


Create Orbiting Numbers

Screenshot 2026-04-04 204311.png

We place numbers around π in a circle:


numbers = []
for i in range(12):
angle = i * (360 / 12)
radius = 200
numbers.append([random.randint(1, 9), angle, radius])

Each number stores:

  1. its value
  2. its angle
  3. its distance from the center


🌀 Bring the Numbers to Life

Now we bring it to life:


for _ in range(200):
pen.clear()
draw_pi(0, 0)

for item in numbers:
n, angle, radius = item

angle += 2

x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))

draw_number(n, x, y)

item[1] = angle

screen.update()
time.sleep(0.03)


⚠️ the Numbers Attack

Make the numbers move inward:


for _ in range(150):
pen.clear()
draw_pi(0, 0)

for item in numbers:
n, angle, radius = item

radius *= 0.97

x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))

draw_number(n, x, y)

item[2] = radius

screen.update()
time.sleep(0.03)


💥 Add Cinematic Effects

Screen Shake


shake_x = random.randint(-10, 10)
shake_y = random.randint(-10, 10)

Explosion Movement


radius *= random.uniform(1.05, 1.1)
angle += random.uniform(-5, 5)

These make the animation feel more dynamic and cinematic.

🎬 the Final Moment

Screenshot 2026-04-04 204300.png

Display a final message:


pen.goto(0, -70)
pen.color((255, 255, 255))
pen.write("π is infinite.", align="center", font=("Arial", 20, "bold"))


Run the Program

Finish with:


turtle.done()

Run your script and enjoy your animation!

Conclusion

This project shows how powerful simple tools like Python turtle can be when combined with creativity.

With just a few lines of code, you created motion, tension, and a full cinematic sequence.

Whole Code