Home ยป Create a Fidget Spinner with Python

Create a Fidget Spinner with Python

In this article we will create a fidget spinner using python

A fidget spinner is a toy that consists of a ball bearing in the center of a multi-lobed (typically two or three) flat structure made from metal or plastic designed to spin along its axis with pressure. Fidget spinners became trending toys in 2017, although similar devices had been invented as early as 1993.

Lets create one in Python using turtle, when the space bar on your PC is pressed the spinner will spin faster, if you do not press the space bar it will slow down. A basic simulation of a fidget spinner

 

# import object from module turtle
from turtle import *
  
# initial state of spinner is null
state= {'turn':0 }
  
# Draw fidget spinner
def spin():
    clear()
  
    # Angle of fidget spinner
    angle = state['turn']/10
  
    # To rotate clock wise we use right
    right(angle)
  
    # move the turtle forward by specified distance
    forward(100)
    dot(120, 'red')
    back(100) 
    right(120)
    forward(100)
    dot(120, 'blue')
    back(100)
    right(120)
    forward(100)
    dot(120, 'green')
    back(100)
    right(120)
  
    update()
  
# Animate fidget spinner
def animate():
    if state['turn']>0:
        state['turn']-=1
      
    spin()
    ontimer(animate, 20)
      
# Flick fidget spinner
def flick():
    state['turn']+=20 #acceleration of spinner
  
# setup window screen
setup(450, 450, 370, 0)
bgcolor("black")
  
tracer(False)
  
# draw the wings
width(30)
color("yellow")
  
# space key for the rotation of spinner
onkey(flick,'space')
  
listen()
animate()
done()

I use thonny, when the code above is run you should see something like this

 

Link

fidget

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More