Do you want to create a stunning 3D animation using Python Turtle graphics? In this tutorial, we’ll build a spinning 3D cube using mathematical projections and animation techniques.
This is a perfect project for learning 3D geometry, projections, and animation while having fun with Python’s Turtle graphics. By the end, you’ll have a rotating cube animation that looks dynamic and impressive! 🚀✨
What You’ll Learn
✅ Setting up a Turtle graphics window for 3D animations 🖥️
✅ Projecting 3D points into 2D using mathematical transformations 🔍
✅ Drawing and connecting cube edges for a complete 3D effect 🎨
✅ Animating the cube rotation to create a smooth spinning effect 🌟
✅ Customizing the rotation speed, size, and perspective for unique designs 🎯
Step 1: Import Required Modules
We need three built-in Python modules:
- turtle: For drawing and animation
- math: For 3D transformations
- time: To control the animation speed
import turtle
import math
import time
Step 2: Setup the Turtle Screen
We create a black background for contrast and set a title for the animation window.
# Create the screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Spinning 3D Cube")
Step 3: Initialize the Turtle
We create a turtle object (pen
) to draw the cube, set its speed to the fastest (0
), and hide the default turtle shape.
# Create the turtle
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
pen.color("cyan")
Step 4: Define Functions for 3D Projection and Drawing
Function to Draw Lines Between Two Points
def draw_line(p1, p2):
pen.penup()
pen.goto(p1[0], p1[1])
pen.pendown()
pen.goto(p2[0], p2[1])
This function connects two projected 3D points on the 2D screen using Turtle’s goto()
function.
Function to Project 3D Points into 2D
def project_3d(x, y, z, angle):
# Simulate 3D rotation around the Y-axis
new_x = x * math.cos(angle) - z * math.sin(angle)
new_z = x * math.sin(angle) + z * math.cos(angle)
# Perspective projection: Shrink based on distance
factor = 200 / (new_z + 300) # Adjust perspective
proj_x = new_x * factor
proj_y = y * factor
return (proj_x, proj_y)
✔ Applies a simple 3D rotation around the Y-axis
✔ Uses a perspective formula to make farther objects appear smaller
Step 5: Define Cube Vertices and Edges
3D Cube Coordinates (Relative to Center)
# Cube vertices
cube_vertices = [
(-50, -50, -50), (50, -50, -50), (50, 50, -50), (-50, 50, -50), # Back face
(-50, -50, 50), (50, -50, 50), (50, 50, 50), (-50, 50, 50) # Front face
]
✔ Defines 8 vertices of the cube in 3D space
✔ Each vertex has (x, y, z) coordinates
Edges Connecting the Cube’s Vertices
# Cube edges
cube_edges = [
(0, 1), (1, 2), (2, 3), (3, 0), # Back face edges
(4, 5), (5, 6), (6, 7), (7, 4), # Front face edges
(0, 4), (1, 5), (2, 6), (3, 7) # Connecting edges between front and back
]
✔ Defines 12 edges that connect pairs of vertices
✔ Used to draw the cube’s wireframe structure
Step 6: Animate the Spinning Cube
We set the initial rotation angle and start an infinite loop to animate the cube.
# Rotation angle
angle = 0
# Main animation loop
while True:
pen.clear() # Clear the screen for each frame
# Project all 3D vertices into 2D
projected_points = [project_3d(x, y, z, angle) for (x, y, z) in cube_vertices]
# Draw all cube edges
for edge in cube_edges:
p1 = projected_points[edge[0]]
p2 = projected_points[edge[1]]
draw_line(p1, p2)
# Increment rotation angle for animation
angle += 0.02
time.sleep(0.02) # Control animation speed
✔ Clears the screen to refresh the frame
✔ Projects 3D points into 2D using our function
✔ Draws cube edges to create a wireframe look
✔ Gradually increases the rotation angle to create a spinning effect
Step 7: Keep the Window Open
To keep the Turtle graphics window open, we use:
# Keep the window open
screen.mainloop()
How the Animation Works 🎥
🎨 Defines cube vertices and edges in 3D
🎲 Projects them into 2D using mathematical transformations
🏁 Rotates the cube around the Y-axis for animation
🌟 Redraws the cube at every frame for a smooth spinning effect
Final Output: A Spinning 3D Cube!
When you run the script, you’ll see a cyan-colored wireframe cube spinning against a black background. 🎥📦
💡 Want to customize it? Try these ideas:
✔ Change the cube size by modifying the vertex values 📏
✔ Experiment with rotation speeds by adjusting angle += 0.02
🔄
✔ Add colors or fill effects for a vibrant look 🎨
✔ Implement keyboard controls to rotate the cube manually ⌨
Watch the Full Video Tutorial 🎥
See the step-by-step process and live animation demo of the spinning cube!
Download the Full Code
Get the complete source code on GitHub:
Conclusion
In this tutorial, we built a spinning 3D cube animation using Python Turtle graphics and mathematical transformations. This project is great for learning animation, 3D projections, and creative coding. 🎮🐍
💡 Next Steps:
✔ Add keyboard controls to rotate the cube interactively 🕹️
✔ Implement multiple rotating cubes for a stunning effect 🎆
✔ Experiment with different rotation axes (X, Y, Z) for a dynamic feel 🔄
Did you enjoy this tutorial?
💡 Like this post
💬 Comment your thoughts
🔔 Subscribe for more Python animation projects!
🚀 Happy Coding! 🎥📦✨