Day eighteen point eight of the hundred day challenge. you were playing with Turtle graphics for uni and I think day eighteen in my challenge closely parallels what you are doing.
This is the teachers turtle code
You had to create a pattern from a triangle all the way to a ten sided shape, no idea the name, tetragon? Well anyway below here is the solution to a very code that took me about ten days to finish.
import turtle as t
import random
tim = t.Turtle()
colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "wheat", "SlateGray", "SeaGreen"]
def draw_shape(num_sidces):
angle = 360 / num_sides
for _ in range(num_sides):
tim.forward(100)
tim.right(angle)
for shape_side_n in range(3, 11):
tim.color(random.choice(colours))
draw_shape(shape_side_n)
The beauty of functions allows you to have a streamlined approach to tackling what desired outcome you want for Turtle graphics. Well below is how I did it and man was I good or what!?
import random
from turtle import Turtle, Screen
tim = turtle.Turtle()
tim.shape("turtle")
colours = ["red", "CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "wheat", "SlateGray", "SeaGreen"]
time = 0
screen = Screen()
screen.colormode(255)
triangle = True
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while triangle:
tim.forward(100)
tim.right(120)
time += 1
if time == 3:
triangle = False
time = 0
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while square:
tim.forward(100)
tim.right(90)
time += 1
if time == 4:
square = False
time = 0
pentagon = True
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while pentagon:
tim.forward(100)
tim.right(72)
time += 1
if time == 5:
pentagon = False
time = 0
hexagon = True
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while hexagon:
tim.forward(100)
tim.right(60)
time += 1
if time == 6:
hexagon = False
time = 0
heptagon = True
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while heptagon:
tim.forward(100)
tim.right(51.4)
time += 1
if time == 7:
heptagon = False
time = 0
octagon = True
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while octagon:
tim.forward(100)
tim.right(45)
time += 1
if time == 8:
octagon = False
time = 0
nonagon = True
screen.colormode(255)
tim.pencolor(randint(1, 255), randint(1, 255), randint(1, 255))
while nonagon:
tim.forward(100)
tim.right(40)
time += 1
if time == 9:
nonagon = False
time = 0
screen.exitonclick()
I mean I really sucked and it was fully realised after I watched her solution code, I mean talk about repeats in commands and what ever I thought was great became clear I had forgotten about functions.
Of course after that I had other challenges like making these two patterns, which turned out to be quite easy after remembering to use functions to make something like this below. A random right angled pattern created with a few function calls. I am sure you would not want me to give you a code work for it.
Here's one.
Can you guess what it pattern it gives out. Of course it is not optimised but I just wanted the pattern. Still a work in progress.
import random
import turtle as t
tim = t.Turtle()
t.colormode(255)
time = 0
tim.speed(100)
tim.pensize(5)
screen = t.Screen()
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = (r, g, b)
return color
def random_step():
tim.color(random_color())
tim.right(5)
tim.circle(100)
# tim.setheading(random.choice(compass))
# tim.forward(10)
while time < 1000:
random_step()
time += 1
screen.exitonclick()
Like all things I do, my mind is racing to other things while doing something, even when I really like what I am doing, I have found I am still doing others things simultaneously. I think the term neurodiverse kind of applies to me somehow. Its a nice way of say I have a mild form of ADHD. Cause you know feelings...
Anyway thanks for your time, hope you got something out of this.
Thanks for your time
Geomining to a better future referral link if you decide to support me