Fidget Spinner in Python with Source Code

Master programming with our job-ready courses: Enroll Now

Remember the toy that has become famous among all of us irrespective of age? Yes, I am talking about the Fidget Spinner. And we will be building fidget spinner game in this project using Python in simple steps. So, let’s get started.

What is a Fidget Spinner?

Most probably you would have played the Fidget Spinner or at least know about it. For the sake of completion, let’s get introduced to it. A Fidget spinner is a two/three lobed toy which is designed to spin along its axis with pressure. The more time it is moved, the faster it rotates!

Fidget Spinner Game in Python

We will be building a three-armed fidget spinner using the Python Turtle module. And here the speed of rotation is controlled by the number of times the space button is pressed.

Download the Fidget Spinner Game in Python

Please download the source code for the Fidget Spinner game in Python using the link: Fidget Spinner Game Project

Project Prerequisites

It is advised for the user to have prior knowledge of Python and the Turtle module to build the project. You can install this module by using the below command.

pip install PythonTurtle

Project Structure

Let’s get to the main part, that is, developing the game. We will be following the below steps to build the Fidget Spinner game:

1. Importing turtle module

2. Writing the function to draw the spinner in a particular alignment

3. Creating a function to spin the spinner

4. Creating a function to increase the number of spins on the space button press

5. Creating a function to start game

6. Writing the code create the main window

1. Importing turtle module

from turtle import *

no_of_spins=0

Code explanation:

a. First, we start by importing the turtle module and all its components

b. We also create a variable to hold the number of turns and initiate it with 0

2. Writing the function to draw the spinner in a particular alignment

def spin_the_fidget():
    global no_of_spins
    #clearing the objects on the turtle window
    clear()
    #setting the angle based on number of spins
    angle = no_of_spins / 10
    dot(120, 'black')
    #setting the angle
    right(angle)
    #Drawing the first lobe
    #Moving the mouse by 100 units in the 'angle' direction to draw the arm
    forward(150)
    dot(120, 'red') #Drawing the cap at the end of the arm of radius 120 units and red color
    back(150) #Moving the drawing pen back to the center
    #Drawing the second lobe
    right(120) #Changing the angle  to right by 120 degrees
    #Moving the mouse by 100 units in the 'angle' direction to draw the second arm
    forward(150)
    dot(120, 'green')#Drawing the cap at the end of the arm of radius 120 units and green color
    back(150)#Moving the drawing pen back to the center
    #Drawing the third lobe
    right(120)#Changing the angle  to right by 120 degrees
    #Moving the mouse by 100 units in the 'angle' direction to draw the third arm
    forward(150)
    dot(120, 'blue')#Drawing the cap at the end of the arm of radius 120 units and blue color
    back(150)#Moving the drawing pen back to the center
    right(120)#Changing the angle  to right by 120 degrees
    update() #Updating the turtle window

Code explanation:

a. In this function, we start by getting the angle of one of the first lobe based on the number of spins.

b. Then draw circle at the center connecting the arms

c. Then we start by drawing the three lobes by following the below steps:

i. Set the angle of movement

ii. Drawing the arm of the spinner in the direction of angle

iii. Drawing the spinner of radius 120

iv. Get back the drawing pointer to the center of the spinner

v. Changing the angle of movement by 120 degrees to draw next arm

3. Creating a function to spin the spinner

def draw_spinner():
    global no_of_spins
    #Decreasing the number of spins by 1 after rotating the spinner once
    if no_of_spins > 0:
        no_of_spins -= 1

    spin_the_fidget() #Drawing the spinner on window
    ontimer(draw_spinner, 20) #Updating the window regularly

Code explanation:

a. In this, we reduce the number of spins every time we spin.

b. And then draw the spinner in that alignment.

c. It updates the turtle window regularly.

4. Creating a function to increase the number of spins on the space button press

def flipSpinner():
    global no_of_spins
    #Increasing the number of spins by 10 on pressing the space button
    no_of_spins += 10

Code explanation:

a. This function runs every time the player presses the space button

b. It increases the number of spins by 10

5. Creating a function to start game

def gameWin():
    #width,height,startx,starty
    setup(450, 450, 370, 50)
    hideturtle() #hiding the turtle symbol
    tracer(False) #offing the turtle animations
    title("PythonGeeks Fidget Spinner Game")
    width(20) #setting the width
    onkey(flipSpinner, 'space') #running the flipSpinner function on pressing space button
    listen() #listening to the keyboard press
    draw_spinner() #Updating the spinner on window
    done()

Code explanation:

a. This function starts the Fidget Spinner game.

b. In this, we create a window of a specified size and title.

c. Set the turtle properties by hiding the turtle symbol and setting animations as False.

d. Then we map to the space button and to the function flipSpinner()

e. The more times the space button is pressed, the faster it rotates.

f. And then run the function draw_spinner()

g. The done() function runs the window in an infinite loop till it is closed by the user.

6. Writing the code to create the main window

setup(420, 420, 370, 50)
hideturtle() #hiding the turtle symbol
tracer(False) #offing the turtle animations
title("PythonGeeks Fidget Spinner Game")
setposition(-170, 180)
write("PythonGeeks Fidget Spinner Game", font=("Calibre",15, "bold"))
setposition(0, 0)
write("Press the Space button to \nstart the game", font=("Calibre",15, "normal"),align='center')
onkey(gameWin, 'space') #running the flipSpinner function on pressing space button
listen()
done()

Code explanation:

a. Here, create the main window of a specified size and title.

b. And then set the turtle properties by hiding the turtle symbol and setting animations as False.

c. We then set a heading for the window.

d. Then we map to the space button to start the game by running the function gameWin()

e. The done() function runs the window in an infinite loop till it is closed by the user

Python Fidget Spinner Output

Python fidget spinner game window

python fidget spinner output

Conclusion

Hurray! We have successfully built the Fidget Spinner game using Python. We learned to draw different components in turtle windows and move them. I hope you enjoyed developing the game. So, get ready to have fun and lessen your stress by playing the game.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google | Facebook


PythonGeeks Team

At PythonGeeks, our team provides comprehensive guides on Python programming, AI, Data Science, and machine learning. We are passionate about simplifying Python for coders of all levels, offering career-focused resources.

Leave a Reply

Your email address will not be published. Required fields are marked *