Skip to content

Instantly share code, notes, and snippets.

@Kaizen86
Last active November 28, 2023 12:14
Show Gist options
  • Select an option

  • Save Kaizen86/c84462d5bb37b8370a16183527296854 to your computer and use it in GitHub Desktop.

Select an option

Save Kaizen86/c84462d5bb37b8370a16183527296854 to your computer and use it in GitHub Desktop.
Simple turtle program to draw 2d polygons of any size
__author__ = "@kaizen86"
import turtle
def draw_polygon(sides, size):
if sides < 3:
# Stop here with an error
raise Exception("A polygon must have at least 3 sides")
step = size/sides # Reduce length of each side with higher side counts
for i in range(sides):
turtle.forward(step) # Draw side
turtle.right(360/sides) # Compute angle between sides
turtle.forward(step) # Final step doesn't need to be followed by a turn
# Allow the file to be imported as well as directly run
if __name__ == '__main__':
turtle.clear()
for i in range(3, 10):
# Prepare for shape draw
turtle.penup()
turtle.setheading(0) # Ensure same starting direction
turtle.goto(i*80-500, 0) # Move across the screen left to right
turtle.pendown()
# Do the thing
draw_polygon(i, 200)
input("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment