Fidget Spinner Using Python


Fidget Spinner is one of the fun and interesting projects that can be made possible using existing Python modules. A fidget spinner is a device that remains stationary in one place and keeps spinning as long as the person flicks it constantly, or else it will stop spinning completely.

To simulate this, we can use libraries like pygame which provides us with an interface which can be customized to our liking while building games in general. There is also another such library which will be used for running the fidget spinner, similar to its real-life counterpart.

Installation and Syntax

To install the turtle library on your computer, type down the following command.

pip install PythonTurtle

The turtle library in Python is a simple gateway to creating graphics and animations via the turtle metaphor. It helps you create a virtual turtle that can be controlled forwards or backwards, turn left or right, customize colours and so more. It is used as a gateway to explain concepts to beginners in graphic design or programming in general.

Algorithm

  • Import the turtle library.

  • Create a dictionary with the key as ‘turn’ and the value as 0.

  • Create a user-defined function spin() to build the bare bones of the fidget spinner.

  • Divide the value of ‘turn’ by 10 and assign that as the angle of the fidget spinner.

  • Define 3 dots with an angle difference of 120 degrees with a forward and backward movement of 100.

  • After defining all possible movements, define a function animate_spin() to animate the spin in the fidget spinner.

  • After checking if the state of the spinner is greater than 0, the state is decremented by 1 and the spin function is called again.

  • Using the concept of recursion, the function is called again after every 20 milliseconds.

  • Define another function called acc() which increases the acceleration of spin when a hotkey is pressed.

  • Set up the window screen to your preferred resolution.

  • Set tracer(False) to bring back the spinner to its initial state after completing its revolution.

  • Build the wing of the fidget spinner and define its colour.

  • Set the hotkey for rotating the spinner.

  • Run listen(), animate_spin() and done().

Example

#import turtle for animation
from turtle import *

#define the state as a dictionary 
s={'turn': 0}

#define the three dots, colour and movement
def spin():
   clear()
   ang=s['turn']/10
   right(ang)

   forward(100)
   dot(120, "maroon")
   back(100)

   "second dot"
   right(120)
   forward(100)
   dot(120, "hotpink")
   back(100)

   "third dot"
   right(120)
   forward(100)
   dot(120, "pink")
   back(100)
   right(120)

   update()

#to animate the spin sequence
def animate_spin():
   if s['turn']>0: s['turn']-=1 #reduce by 1 if state > 0
   spin()
   ontimer(animate_spin,20) #call function again once every 20 ms

#accelerate when the spacebar is pressed.
def acc():
   s['turn']+=40

setup(600,400,370,0)
bgcolor("black")

#return to the initial state
tracer(False)
#define wingspan of fidget spinner
width(60)
color("white")
#hotkey is assigned to the spacebar
onkey(acc,'space')

#call all functions
listen()
animate_spin()
done()

Output

To start, we define the state of the spinner as 0. Then, we construct an image with 3 dots before animating them. This is done by defining the spin function. The spin is then animated by reducing the states constantly once every 20 milliseconds. We also define a function to accelerate the speed of the spinning animation whenever the spacebar is pressed.

Then, for the window, we define a black background. Then in order to return the cursor to its original state we define the tracer as ‘False’. Then, for the fidget spinner wing, we define the wing of the fidget spinner and its colour. We then define the acceleration hotkey as a spacebar. Then we run all the functions to see the output.

There are many reasons why this is used as an example, namely −

  • For visualization and graphics simulation − Displaying a fidget spinner in Python allows you to create a visual representation of an object or concept. It can be used to simulate the spinning motion of a physical fidget spinner, providing a visual and interactive experience.

  • To learn and teach − It allows beginners to explore graphics and animation, giving a deeper understanding of prop movement, rotation, angles and such.

  • For game development − Due to its easy-to-use graphics and animation library, it is a useful tool for developing interactive games with stellar animation.

  • Entertainment − It can also be used as a cool trick to surprise your friends.

  • Design and Prototyping − It can also be used to test the feasibility of designs and prototypes built before application.

Conclusion

Overall, creating a fidget spinner in Python provides a gateway to creatively apply programming skills, design concepts, colour theory, and animations. It is mostly used in the education industry to learn and teach concepts, or it is part of a larger application.

Updated on: 23-Aug-2023

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements