Master Python Animation in Minutes: Build a Stunning Spiral Galaxy- Animation-2

20 Jul 2023 Balmiki Mandal 0 Python

✨ Python Animation Made Easy: Learn to Create Hypnotic Spirals (Fast!)

Full Source Code:

from turtle import * #Importing  all functions and classes from the turtle module,
import colorsys  # Importing the colorsys module for color conversion.

# Set up turtle canvas and initial parameters
speed(0)  #Setting the animation speed to the fastest (0).
bgcolor("black") #Setting the background color to black.
spiral_hue = 0.1  #Initializing the spiral_hue variable, used to generate different colors.
pensize(2)  #Sets the pen width to 2 pixels.
goto(300, -100)  # Moves the turtle to the starting position (300, -100).

# Draw spirals with changing colors
for i in range(200): #Loops 200 times to draw multiple spirals.
    col = colorsys.hsv_to_rgb(spiral_hue, 1, 1)  #Converting hue value to an RGB color 
    pencolor(col)  # Setting the pen color to the generated color.
    spiral_hue += 0.006  #Increments the hue for a smooth color transition.
    left(150)  # Turns the turtle left by 150 degrees, creating a spiral pattern.
    circle(400 - i, 80) #Drawing circle with a decreasing radius and a distance of 80 pixels from the center.

done()  # Keeps the graphics window open until manually closed.

 

Breakdown of the code:

Importing Necessary Modules:

  • from turtle import *: This imports all functions and classes from the turtle module, which is a Python library used for creating graphics and animations.
  • import colorsys: This imports the colorsys module, which provides functions for converting colors between different color systems.

2. Setting Up the Turtle Canvas:

  • speed(0): Sets the animation speed to the fastest, so you won't see the turtle drawing each line individually.
  • bgcolor("black"): Sets the background color of the canvas to black.
  • spiral_hue = 0.1: Initializes a variable called spiral_hue to a value of 0.1, which will be used to generate different colors for the spirals.
  • pensize(2): Sets the thickness of the lines that the turtle will draw to 2 pixels.
  • goto(300, -100): Moves the turtle to the starting position on the canvas, which is 300 pixels to the right and 100 pixels down from the center.

3. Drawing the Colorful Spirals:

  • for i in range(200): This starts a loop that will repeat 200 times, allowing the code to draw multiple spirals.
    • col = colorsys.hsv_to_rgb(spiral_hue, 1, 1): Converts the spiral_hue value to an RGB color using the colorsys.hsv_to_rgb() function. This creates a vibrant range of colors for the spirals.
    • pencolor(col): Sets the color of the turtle's pen to the generated color.
    • spiral_hue += 0.006: Increments the spiral_hue value slightly for the next iteration of the loop, creating a smooth color transition between the spirals.
    • left(150): Turns the turtle left by 150 degrees, which creates the spiral pattern as the turtle moves forward.
    • circle(400 - i, 80): Draws a circle with a radius that decreases as the loop progresses (400 - i). The circle is always drawn 80 pixels away from the center of the spiral.

4. Finishing Up:

  • done(): This function keeps the graphics window open until you manually close it, allowing you to admire the colorful spirals you've created!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.