Thursday, 11 April 2024

Colorful Galaxy using Python

 


Code : 

import turtle

t = turtle.Turtle()

#clcoding.com 

s = turtle.Screen()

colors=['orange', 'red', 'magenta', 'blue', 'magenta',

        'yellow', 'green', 'cyan', 'purple']

s.bgcolor('black')

t.pensize('2')

t.speed(0)

for x in range (360):

    t.pencolor(colors[x%6])

    t.width(x//100+1)

    t.forward(x) 

    t.right(59)

    turtle.hideturtle()    

#clcoding.com    

Explanation: 

let's break down the code step by step:

import turtle: This line imports the Turtle module, which provides a graphics environment for drawing shapes and patterns.

t = turtle.Turtle(): This creates a new Turtle object named t. The Turtle object represents a pen that can draw on the screen.

s = turtle.Screen(): This creates a new Screen object named s. The Screen object represents the window or canvas where the turtle will draw.

colors = ['orange', 'red', 'magenta', 'blue', 'magenta', 'yellow', 'green', 'cyan', 'purple']: This is a list of colors that will be used for drawing. Each color is represented by a string.

s.bgcolor('black'): This sets the background color of the screen to black.

t.pensize(2): This sets the width of the pen to 2 pixels.

t.speed(0): This sets the drawing speed of the turtle to the fastest speed (0 means fastest).

for x in range(360):: This starts a loop that will repeat 360 times. The loop will draw a spiral pattern.

t.pencolor(colors[x % 6]): This sets the color of the pen to one of the colors from the colors list. The % operator is used to cycle through the colors repeatedly as x increases.

t.width(x // 100 + 1): This sets the width of the pen based on the current value of x. As x increases, the pen width will gradually increase.

t.forward(x): This moves the turtle forward by a distance equal to the current value of x.

t.right(59): This rotates the turtle to the right by 59 degrees.

turtle.hideturtle(): This hides the turtle cursor from the screen.

#clcoding.com: This appears to be a comment indicating the source or reference of the code.

Overall, this code creates a colorful spiral pattern using the Turtle module in Python.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (363) Python Quiz (24) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses