Fidget Spinner using Python - GeeksforGeeks

30 Jun, 2021
Prerequisite: Turtle Programming in Python
In this article, we are going to create Fidget Spinner using the Python turtle module. It is a Python feature like a drawing board, which lets us command a turtle to draw all over it! We can use functions like turtle.forward() and turtle.right() which can move the turtle around. 
Step 1: Drawing the fidget spinner
Here in this piece of code, we will initialize the state of the fidget spinner, angles for rotating it in both clockwise and anticlockwise direction and make the colored graphics required to make the fidget spinner.
Python3
# initial state of spinner is null (stable)state= {'turn':0 }  # Draw fidget spinnerdef spin():    clear()      # Angle of fidget spinner    angle = state['turn']/10    right(angle)      # move the turtle forward by specified    # distance    forward(100)      # draw a dot with diameter 120 using colour     # red    dot(120, 'red')      # move the turtle backward by specified     # distance    back(100)      "second dot"    right(120)    forward(100)    dot(120, 'blue')    back(100)      "third dot"    right(120)    forward(100)    dot(120, 'green')    back(100)    right(120)      update()
Output:
Step 2: Animating the fidget spinner
In this step, we will call a function animate() which will animate the fidget spinner by seeing if the state is greater than 0 then decrement one from it decreases and call the spin function again. After that, a timer is installed which will call the animate function again after 20 milliseconds
Python3
# Animate fidget spinnerdef animate():    if state['turn'] > 0:        state['turn'] -= 1      spin()    ontimer(animate, 20)
Step 3: Moving the fidget spinner, setting up the window, and tracing the spinner back to its original position
Here we will define the flick function which will move the fidget spinner by increasing its state to 40, also we will set up a window and its background color, we will use a tracer that brings back the fidget spinner into its initial state after completing its rotation and after that, we will define the width and color of our fidget spinner and at last, we will define the key for moving the fidget spinner.
Python3
# Flick fidget spinnerdef flick():          # acceleration of spinner    state['turn'] += 40    # setup window screensetup(600, 400, 370, 0)bgcolor("black")  tracer(False)'''tracer brings back the fidget spinner into its initial stateafter completing the rotation'''  # wing of fidget spinnerwidth(60)color("orange")  # keyboard key for the rotation of spinneronkey(flick, 'space')  listen()animate()done()
Below is the complete implementation:
Python3
# import object from module turtlefrom turtle import *  # initial state of spinner is null (stable)state= {'turn':0 }  # Draw fidget spinnerdef spin():    clear()      # Angle of fidget spinner    angle = state['turn']/10      # To rotate in clock wise we use right    # for Anticlockwise rotation we use left    right(angle)      # move the turtle forward by specified distance    forward(100)      # draw a dot with diameter 120 using colour red    dot(120, 'red')      # move the turtle backward by specified distance    back(100)      "second dot"    right(120)    forward(100)    dot(120, 'blue')    back(100)      "third dot"    right(120)    forward(100)    dot(120, 'green')    back(100)    right(120)      update()  # Animate fidget spinnerdef animate():    if state['turn']>0:        state['turn']-=1          spin()    ontimer(animate, 20)      # Flick fidget spinnerdef flick():    state['turn']+=40 #acceleration of spinner  # setup window screensetup(600, 400, 370, 0)bgcolor("black")  tracer(False)  # wing of fidget spinnerwidth(60)color("orange")  # keyboard key for the rotation of spinneronkey(flick,'space')  listen()animate()done()
Output:
Python-turtle
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | os.path.join() method
Python | Get unique values from a list
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()