Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fidget Spinner Using Python
A fidget spinner is a simple toy that spins around a central bearing. We can create an animated fidget spinner simulation using Python's turtle graphics library. This project demonstrates graphics programming, animation loops, and user interaction concepts.
Required Libraries
The turtle library comes pre-installed with Python, so no additional installation is needed for most Python distributions ?
# Turtle is included with standard Python installation import turtle
The turtle library provides a simple interface for creating graphics and animations. It uses a virtual "turtle" that can move around the screen, drawing lines and shapes as it moves.
Project Structure
Our fidget spinner consists of three main components ?
State Management Track the spinner's rotation speed
Drawing Function Create the visual spinner with three colored dots
Animation Loop Continuously update and redraw the spinner
User Interaction Allow users to spin the fidget spinner
Implementation
# Import turtle for animation
from turtle import *
# Define the state as a dictionary to track rotation
state = {'turn': 0}
# Function to draw the fidget spinner
def spin():
clear() # Clear previous drawing
angle = state['turn'] / 10 # Calculate rotation angle
right(angle)
# First dot (maroon)
forward(100)
dot(120, "maroon")
back(100)
# Second dot (hotpink) - 120 degrees apart
right(120)
forward(100)
dot(120, "hotpink")
back(100)
# Third dot (pink) - 120 degrees apart
right(120)
forward(100)
dot(120, "pink")
back(100)
right(120)
update() # Update the display
# Function to animate the spinning motion
def animate_spin():
if state['turn'] > 0:
state['turn'] -= 1 # Reduce speed gradually (friction effect)
spin() # Redraw the spinner
ontimer(animate_spin, 20) # Schedule next frame in 20ms
# Function to accelerate when spacebar is pressed
def accelerate():
state['turn'] += 40 # Add momentum to the spinner
# Set up the graphics window
setup(600, 400, 370, 0)
bgcolor("black")
# Configure drawing settings
tracer(False) # Turn off automatic screen updates for smoother animation
width(60) # Set pen width for the spinner arms
color("white") # Set default drawing color
# Set up user interaction
onkey(accelerate, 'space') # Bind spacebar to acceleration function
# Start the animation
listen() # Enable keyboard input
animate_spin() # Begin the animation loop
done() # Keep window open
How It Works
The simulation uses several key concepts ?
| Component | Purpose | Method |
|---|---|---|
| State Dictionary | Track rotation speed | Stores current 'turn' value |
| Spin Function | Draw the spinner | Three dots positioned 120° apart |
| Animation Loop | Create smooth motion | Recursive calls every 20ms |
| Friction Effect | Realistic slowing | Decrement turn value each frame |
Key Features
Realistic Physics: The spinner gradually slows down due to the friction effect implemented by decrementing the turn value.
User Interaction: Press the spacebar to give the spinner a "flick" and watch it spin faster.
Visual Appeal: Three colorful dots (maroon, hotpink, pink) create an attractive spinning pattern against a black background.
Educational Value
This project teaches several programming concepts ?
Graphics Programming Using coordinate systems and drawing commands
Animation Techniques Creating smooth motion through recursive function calls
Event Handling Responding to user keyboard input
State Management Tracking and updating program state over time
Mathematical Concepts Working with angles, rotation, and coordinate geometry
Possible Enhancements
You can extend this project by adding ?
Different spinner designs with varying numbers of arms
Sound effects when spinning or clicking
Multiple spinners on the same screen
Variable friction rates for different "materials"
Score tracking based on spin duration
Conclusion
Creating a fidget spinner in Python demonstrates fundamental concepts in graphics programming and animation. The project combines mathematical concepts like rotation with programming skills like event handling and recursive function calls, making it an excellent learning exercise for beginners.
---