Turtle – script 1

from turtle import *
from random import randint


print(pos())

def polygon(size, side=4):
	angle = 360/side
	for n in range(side):
		forward(size)
		left(angle)


for n in range(-30,30):
	polygon(10*(n+1), 4)

The code for this script written in Python. It uses the turtle module to draw a series of polygons.

The first line of code imports the entire turtle module. This means that all of the functions and classes from the turtle module will be available to the code.

The second line of code imports the randint() function from the random module. This function will be used to generate random side lengths for the polygons.

The third line of code prints the current position of the turtle. This is useful for debugging purposes.

The polygon() function takes two arguments: size and side. The size argument is the length of each side of the polygon. The side argument is the number of sides in the polygon.

The polygon() function works by first calculating the angle that the turtle needs to turn between each side of the polygon. This is done by dividing 360 degrees by the number of sides in the polygon.

The polygon() function then iterates over the number of sides in the polygon. For each side, the turtle moves forward by the specified size and then turns left by the calculated angle.

The last section of code draws a series of polygons using the polygon() function. The loop iterates from -30 to 29. For each iteration, the polygon() function is called with a size of 10*(n+1) and a side of 4. This will draw a series of squares with increasing side lengths.

Here is a more detailed explanation of each line of code:

from turtle import *

This line imports the entire turtle module. This means that all of the functions and classes from the turtle module will be available to the code.

from random import randint

This line imports the randint() function from the random module. This function will be used to generate random side lengths for the polygons.

print(pos())

This line prints the current position of the turtle. This is useful for debugging purposes.

def polygon(size, side=4):
    angle = 360/side
    for n in range(side):
        forward(size)
        left(angle)

This function defines a function called polygon(). The function takes two arguments: size and side. The size argument is the length of each side of the polygon. The side argument is the number of sides in the polygon.

The polygon() function works by first calculating the angle that the turtle needs to turn between each side of the polygon. This is done by dividing 360 degrees by the number of sides in the polygon.

The polygon() function then iterates over the number of sides in the polygon. For each side, the turtle moves forward by the specified size and then turns left by the calculated angle.

for n in range(-30,30):
    polygon(10*(n+1), 4)

This loop iterates from -30 to 29. For each iteration, the polygon() function is called with a size of 10*(n+1) and a side of 4. This will draw a series of squares with increasing side lengths.

To run this code, you can save it as a Python file (e.g. polygons.py) and then run it in a terminal or command prompt:

python polygons.py

This will open a new window and draw a series of squares with increasing side lengths.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.