32. Python Classes and Turtles
12/9/2024
As I continue my Python lessons, I also continue to be amazed by the depth and wideness of everything I'm learning.
I mean, there are people out there who not only know how to write code like they know (probably) to brush their teeth, but they also make ingenious and interesting learning tools, for those like me! I started learning about Classes in Python. So using the turtle module is not only a suitable choice but also a very funny one.
Lucky, the documentation about the Turtle is extensive and examples are provided. So the challenges did not seem that hard, but still… I feel like I’m floating. I feel like I understand, but when it comes to moving the pieces around and making something new, I have difficulties.
I know, it is still early, and it might take years of perseverant active learning before I could feel a bit more secure, but I wish that the road to understanding wasn’t so hard!
Learnings
Random walk : to make the turtle move randomly, I used the random module and the.setheading() method which takes a number as argument, a number that represents the angle > the direction the turtle is going to move towards.
Dashed line: used a for loop and the .penup() and .pendown() method, alternated between .forward()
Generate random colors : I liked this one a lot. I created a function that generated random colors in rgb format . First, I needed to change the color mode for the application. Then with the help of a function, I again used the random.randint() function to create numbers for each r, g, b. The function is returning the final color, something called tuple.
Extract colors from an image : First thing, how cool is that? Second : I needed to use a package called Colorgram.py. It was quite easy to understand how to use, given the documentation. The result was a list of rgb tuples.
Make a Hirst dot painting : The extracted colors were used to generate a Hirst-like dot painting. With this occasion, I learned about Damien Hirst, an artist who is selling his art for astronomical prices, even if the art is rather difficult to be seen/understood as art by the majority of us, me among those.
The main point in creating this dot painting is figuring out how to make the turtle move between the boundaries of a perimeter, that would represent the painting. I could not do it myself, or maybe I did not try enough, but the solution is using a for loop that loops though a range of max number of dots, uses the color list to get a random color and assign it to each dot.
Then there is also a condition that checks if the dots drawn are inside the perimeter - 10 dots per line. So if the dot % 10 == 0 then the turtle should change it’s heading to 90 degrees, moves forward, changes the heading again to 180 degrees and moves forward and then finally goes back to a heading of 0.
Turtle race : Creating this one was very funny, the cute little turtles racing towards the finish line! The fact that we can generate more turtles shows the power of the classes. We can create as many different instances of the same class as we want.
For the turtle race, we needed 6 turtles. In order to align them on the left side of the screen, we needed to understand the coordinates system. At this point, it is probably better to insert the function :
for turtle_index in range(0, 6): #characters new_turtle = Turtle(shape="turtle") new_turtle.penup() new_turtle.color(colors[turtle_index]) new_turtle.goto(x=-250, y=y_positions[turtle_index]) all_turtles.append(new_turtle)
while race_on: for turtle in all_turtles: if turtle.xcor() > 260: race_on = False winner = turtle.pencolor() if winner == user_bet: print(f"You won! {winner}") else: print("You lost :()") move_distance = random.randint(0, 10) urtle.forward(move_distance)
The parts I struggled with in this case were the turtle alignments:
new_turtle.goto(x=-250, y=y_positions[turtle_index])
As well as controlling how far they can go, and this is the condition controlling it :
if turtle.xcor() > 260
Until next time, be curious and learn something new every day 💫
Victoria