Simple Python Turtle Graphic and Code: Overlapping Squares

Pubblicato il: 28 luglio 2023
sul canale di: Basic Python Turtle Art
343
5

A group of squares made more visually appealing by the gradual decrease in size and change in hue of every shape that overlaps the preceding one. The simple Python Turtle code uses the colormode(1.0) to facilitate transition from initial color to terminal one. The code allows the user to alter the initial and terminal colors.

Feel free to copy the basic Python Turtle code that is given below. Don't hesitate to ask questions about the code if you have any. Enjoy! Please comment, like, or subscribe :)

Incidentally, for manually colorable graphics and variations, please visit my author site at https://www.amazon.com/author/basicpy...

import turtle
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
t.pensize(4)
screen.colormode(1.0) #Requirement for use of numbers from 0 to 1.0 as color r, g, and b values
size = 250 #Changeable; Distance from center to vertex of square
number_of_rotations = 15 #Changeable; Count of stepwise rotations for the square over 360-degree span
size_change = size / number_of_rotations #Change in dimension of square for every stepwise rotation
r1 = r = 1 #Changeable to a number from 0 to 1.0 for each of r1, g1, and b1;
g1 = g = 0 #Initial (r1, g1, b1) = (1, 0, 1) for "violet"; (r, g, b) -- value holders
b1 = b = 1 #of stepwise changes in red, green, and blue values
r2 = 1 #Changeable to a number from 0 to 1.0 for each of r2, g2, and b2;
g2 = 1 #Target (r2, g2, b2) = (1, 1, 0) for "yellow"
b2 = 0
r_change = (r2 - r1) / number_of_rotations #Change in red value per stepwise rotation
g_change = (g2 - g1) / number_of_rotations #Change in green value per stepwise rotation
b_change = (b2 - b1) / number_of_rotations #Change in blue value per stepwise rotation
angle_change = 360 / number_of_rotations #Angular change per stepwise rotation
t.penup() #Python_Graphic start of drawing procedure
for j in range(number_of_rotations): #Loop to draw variously-sized and colored squares, one each for every rotation
t.forward(size) #Trail-less movement of turtle to starting point of drawing one square
t.left(135)
t.pendown()
t.fillcolor(r, g, b) #Color fill procedure for current square
t.begin_fill()
for i in range(4): #Drawing of square's four sides
t.forward(size * (2 ** (1 / 2)))
t.left(90)
t.end_fill()
r += r_change #Stepwise changes in values or r, g, and b for next square
g += g_change
b += b_change
size -= size_change #Stepwise change in dimension for next square
t.penup()
t.home()
t.left((1 + j) * angle_change) #Angular rotation of turtle to start drawing of next square
t.hideturtle()
screen.exitonclick()


In questa pagina del sito puoi guardare il video online Simple Python Turtle Graphic and Code: Overlapping Squares della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Basic Python Turtle Art 28 luglio 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 343 volte e gli è piaciuto 5 spettatori. Buona visione!