# 100 Days Of Code Challenge – Level Intermediate #2.

Posted in January 26, 2022 by

Categories: Challenges Programming Python

Tags: , , ,

This is the continuation of a series of articles from the 100 Days of Code Challenge Udemy.com course made by Angela Yu. In this article, I write about the next days in the intermediate level of this course.

If you would like to check the previous article from this series here is the link 👇:

## 100 Days of Code Challenge – Day 18.

On this day I was learning about the more advanced Turtle Library application. I was presented with tuples and how to read a documentation properly. Angela told us that a library, framework, or language documentation is the best friend of a programmer 😊.

On this day there were a few coding challenges from easy ones to more difficult ones. The first coding challenge was to draw a square 👇:

``````def draw_a_square(my_turtle):
"""This function draws a square in Turtle window."""
for _ in range(4):
my_turtle.forward(100)
my_turtle.left(90)``````

I must write that I was making from every coding challenge concept a function because it seems to be a better idea.

The second coding challenge was to draw a dashed line 👇:

``````def draw_a_dashed_line(length, my_turtle):
"""This function draws a dashed line."""
dash_length = 10

for _ in range(int(length / 10)):
my_turtle.forward(dash_length)
my_turtle.penup()
my_turtle.forward(dash_length)
my_turtle.pendown()``````

The third coding challenge was to draw an n-sides figure one inside each other 👇:

``````def draw_a_shape(number_of_sides, side_length, my_turtle):
angle = int(360 / number_of_sides)
for j in range(number_of_sides):
my_turtle.forward(side_length)
my_turtle.right(angle)

def draw_a_shapes(side_length, my_turtle):
"""This function draws a shapes inside each other in random color."""
colors = ["cyan", "brown", "blue", "azure", "coral", "green", "red", "tan", "wheat"]
for i in range(3, 8):
my_turtle.color(random.choice(colors))
draw_a_shape(i, side_length, my_turtle)``````

The fourth coding challenge was to make a function that creates a random walk. Random walk is better described under this link. Here is the code 👇:

``````def gen_random_color():
"""This function generates a random tuple with rgb colors values."""
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
random_color_tuple = (r, g, b)
return random_color_tuple

def random_walk(my_turtle):
"""This function do a random walk."""
# colors = ["cyan", "brown", "blue", "azure", "coral", "green", "red", "tan", "wheat"]
line_length = 20
my_turtle.pensize(10)
my_turtle.speed("fast")
turtle.colormode(255)
list_of_movements = [my_turtle.forward, my_turtle.backward]
list_of_turns = [my_turtle.right, my_turtle.left]
while True:
movement = random.choice(list_of_movements)
turn = random.choice(list_of_turns)
# color = random.choice(colors)
my_turtle.color(gen_random_color())
movement(line_length)
turn(random.choice([90, -90]))``````

The fifth coding challenge was to create a function that would generate a random color. I already pasted this code at the previous code listing.

The sixth coding challenge was to create a function that draws a spirograph 👇:

``````def draw_a_spirograph(my_turtle, size_of_gap):
"""This function draws a spirograph in a turtle window."""
my_turtle.color("black")
my_turtle.speed("fastest")
my_turtle.pensize(3)
turtle.colormode(255)
for _ in range(int(360 / size_of_gap)):
my_turtle.color(gen_random_color())

The seventh coding challenge was to extract colors, by using the colorgram.py library, from Damien Hirst color dots JPG image painting. About the artist, you can read under this link.

Here is the code 👇:

``````import colorgram

def extract_colors_from_image(image_src, num_of_colors):
"""Extracting colors from JPG image."""
colors = colorgram.extract(image_src, num_of_colors)
colors_palete = []
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
color_tuple = (r, g, b)
colors_palete.append(color_tuple)

return colors_palete``````

The final coding challenge was to make a dotted painting that is similar to those ones that Damien Hirst created.

Here is the code 👇:

``````import turtle
import colorgram
from turtle import Screen
import random

def extract_colors_from_image(image_src, num_of_colors):
"""Extracting colors from JPG image."""
colors = colorgram.extract(image_src, num_of_colors)
colors_palete = []
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
color_tuple = (r, g, b)
colors_palete.append(color_tuple)

return colors_palete

def set_initial_turtle_position(my_turtle, my_screen, y_offset):
my_turtle.penup()
my_turtle.goto(-my_screen.window_width() / 2 + 50, -my_screen.window_height() / 2 + 20 + y_offset)
my_turtle.pendown()
my_turtle.showturtle()

def draw_dotted_line(my_turtle, length, color_palete):
dash_length = 50

for _ in range(int(length / 10)):
r, g, b = random.choice(color_palete)
my_turtle.pencolor(r, g, b)
my_turtle.fillcolor(r, g, b)
my_turtle.begin_fill()
my_turtle.end_fill()
my_turtle.penup()
my_turtle.forward(dash_length)
my_turtle.pendown()

if __name__ == "__main__":
color_list = [(232, 251, 242), (198, 12, 32), (250, 237, 17), (39, 76, 189), (38, 217, 68), (238, 227, 5),
(229, 159, 46), (27, 40, 157), (215, 74, 12), (15, 154, 16), (199, 14, 10), (242, 246, 252),
(243, 33, 165), (229, 17, 121), (73, 9, 31), (60, 14, 8), (224, 141, 211), (10, 97, 61),
(221, 160, 9), (17, 18, 43), (46, 214, 232), (11, 227, 239), (81, 73, 214), (238, 156, 220),
(74, 213, 167), (77, 234, 202), (52, 234, 243), (3, 67, 40)]
matt = turtle.Turtle()
matt.speed("fastest")
matt.hideturtle()
screen = Screen()
screen.colormode(255)
screen.setup(550, 550)

for i in range(45, 451, 45):
set_initial_turtle_position(matt, screen, i)
draw_dotted_line(matt, 100, color_list)

screen.exitonclick()``````

And that is all for day 18 🙂.

## 100 Days of Code Challenge – Day 19.

This day was all about the Turtle module.

I was able to learn more about event listening evoked by the keypress. I was learning also about passing a function as an argument to another function.

The first coding challenge was to make an Etch-A-Sketch mini-game.

Code is below 👇:

``````from turtle import Turtle, Screen

def move_forward():
matt.forward(20)

def move_backwards():
matt.backward(20)

def move_left():

def move_right():

def clear_screen():
matt.clear()
matt.penup()
matt.home()
matt.pendown()

matt = Turtle()
screen = Screen()

# Window is starting to listen for events like key stroke.
screen.listen()
screen.onkey(key="w", fun=move_forward)
screen.onkey(key="s", fun=move_backwards)
screen.onkey(key="a", fun=move_left)
screen.onkey(key="d", fun=move_right)
screen.onkey(key="c", fun=clear_screen)

screen.exitonclick()
``````

The final coding challenge was to build a Turtle Race Game. I wanted to expand a little bit with that project so I decided I will additionally print game results on the turtle screen and count the first three turtles which won 😋.

The whole code is below 👇:

``````from turtle import Screen, Turtle
import random

def setup_writers(x, y):
"""This function setup all writers that will write on turtle screen."""
writer_main = Turtle()
writer_main.penup()
writer_main.hideturtle()
writer_main.setpos(x=x, y=y)

return writer_main

def write_champions_info(champ):
writer = setup_writers(-90, 170)
writer.write(f"Gold: {champ.pencolor()} won!", False, align="center",
font=('Arial', 14, 'normal'))

writer = setup_writers(-90, 140)
writer.write(f"Silver: {champ.pencolor()}!", False, align="center",
font=('Arial', 14, 'normal'))

writer = setup_writers(-90, 110)
writer.write(f"Bronze: {champ.pencolor()}!", False, align="center",
font=('Arial', 14, 'normal'))

is_race_on = False

screen = Screen()
screen.setup(width=500, height=400)

colors = ["red", "orange", "yellow", "green", "blue", "purple"]
race_participants = []
for color in colors:
race_participants.append(Turtle(shape="turtle"))

y = -100
for indeks, turt in enumerate(race_participants):
turt.penup()
turt.color(colors[indeks])
turt.goto(x=-238, y=y)
y += 35

is_race_on = True
count = 0
champions = []
turts_to_remove = []

while is_race_on:
# Movement
for turt in race_participants:
move_forward = random.randint(1, 10)
turt.forward(move_forward)

# Checking if turtle won.
for index, turt in enumerate(race_participants):
if turt.xcor() > 200:
count += 1
champions.append(turt)
turts_to_remove.append(index)
if count > 3:
write_champions_info(champions)
is_race_on = False

# We remove from checking and moving list those turtles that already finish race.
counter = 1
for i, champ in enumerate(turts_to_remove):
if i == 0:
race_participants.pop(champ)
else:
champ -= counter
race_participants.pop(champ)

counter += 1

turts_to_remove.clear()

screen.exitonclick()
``````

And that is all for day 19 🙃.

## Summary

By doing days, 18 and 19 I learned more about the Turtle module and how to manipulate graphics in Python.

Building Turtle Race Game was super fun for me and upgrading it a bit was both challenging and also fun.

If you would like to join 100DaysOfCodeChallenge on Udemy and learn with me and Angela below is the link to the course 👇:

If you find this article valuable and informative you can leave some comments, share it on social media, and give a thumb up.

Thank you for your time and have a great day.

Bye 👋.

## One thought on “100 Days Of Code Challenge – Level Intermediate #2.”

error: Content is protected !!