Share your repls and programming experiences

← Back to all posts
Geometric Shapes
16014 (26)

Basically you need to have a shape like a square or a triangle that keeps tracing over itself. To do this we can can make a variable named: Shape
Step 1. Set variable shape to random.randint(3, 6)
The reasoning behind this is that 3 must be the lowest because
it is a triangle and a triangle has the lowest amount of sides
possible in a shape. (You cant have a shape with only two sides.)
And 6 must be the maximum because as the number of sides
increases it becomes harder to see the exact edges.

Step 2. Create different if statements for the different shapes.
If shape == 3:
If shape == 4:
e.t.c.
Step 3. Add onto the angle each time it traces over the shape.
Here is the code for you to do that:

If the shape is 3:
angle = (180 / shape) + 1
length = random.randint(75,150)
for i in range(182):
if minusangle == True:
angle -= shape x 50
minusangle = False
if i // shape + 1:
angle += shape x 50
minusangle = True
t.forward(length)
t.right(angle)

If the shape is 4:
angle = (360 / shape) + 1
length = random.randint(75,150)
for i in range(91):
if minusangle == True:
angle -= shape x 90
minusangle = False
if i // shape + 1:
angle += shape x 90
minusangle = True
t.forward(length)
t.right(angle)

If the shape is 5:
angle = (180 / shape) + 1
length = random.randint(75,150)
for i in range(74):
if minusangle == True:
angle -= shape x 50
minusangle = False
if i // shape + 1:
angle += shape x 50
minusangle = True
t.forward(length)
t.right(angle)

If the shape is 6:
angle = (1 / shape) + 1
length = random.randint(75,150)
for i in range(62):
if minusangle == True:
angle -= shape x 50
minusangle = False
if i // shape + 1:
angle += shape x 50
minusangle = True
t.forward(length)
t.right(angle)
This will put each trace out of sync with the next making it just a
little off from the last line each time it repeats. I've given all instances because the integers change for each instance of shape.

Make sure you specify all the turtle requirements beforehand.
import turtle
import random
minusangle = False
t = turtle.Turtle()
t.color(random.choice(['red', 'blue', 'cyan', 'magenta', 'pink', 'lime', 'green', 'black', 'grey', 'orange', 'brown', 'yellow']))
t.speed(100)
t.penup()
t.left(100)
t.forward(200)
t.right(100)
t.pendown()
Shape = random.randint(3,6)

Here is the link: https://repl.it/@16014/Geometric-Shapes

Feel free to adapt it to other shapes!
Have fun : )