Day 6 of #100DaysOfCode: Turtle Maze Game
This project has also taken me quite a long time as I was trying to figure out the correct logic to get the turtle to complete the maze automatically as if they are learning about the path. On Day 6 of #100DaysOfCode, I created another mini project called Turtle Maze Game. It consists of the following concepts I have learned from Dr Angela Yu's 100DaysOfCode: The Complete Python Bootcamp Udemy course:
Create functions
While Loops
The aim of the Turtle Maze Game is to navigate through the maze and avoiding the obstacles and reach the finish line. The turtle moves along the maze's path, changing the direction when it runs into an obstacle to move towards the end goal. As soon as it reaches to the finish line, it displays "Well Done!! The Turtle Completed the Maze!!!"
Python Concepts Used:
Create Functions
In the Turtle Maze Game, I have created multiple functions to increase readability. By breaking the code down into smaller, manageable pieces, each function is responsible for the aspects of the Turtle Maze Game such as checking if the turtle object is going to collide with the obstacle or when it reaches towards the finish line. This approach improves the clarity and flow, making the code much simpler to understand and maintain.
def will_hit_obstacle(turtle_figure, obstacle):
for obstacle in obstacles:
if turtle_figure.distance(obstacle) <= 65:
return obstacle
return None
While Loop
While Loop is utilised to repeatedly evaluate whether a certain condition is met. In the current subject of the Turtle Maze Game, the while loop is used to continuously validate if the turtle runs into an obstacle. If the condition is true, the steps within the if statement is executed. The process gets repeated infinitely until the condition is false. When the condition is false, it suggests that the turtle has reached the finish line, the else statement is executed. This controls the flow of the Turtle Maze Game until it's completed.
while True:
obstacle_hit = will_hit_obstacle(turtle_figure, obstacles)
if obstacle_hit:
if obstacle_hit.shape() == "triangle":
turtle_figure.left(90)
...
elif obstacle_hit.shape() == "square":
turtle_figure.right(90)
...
else:
turtle_figure.left(90)
...
else:
finish_hit = reach_finish_line(turtle_figure, Complete)
....
Reflections on Turtle Maze Game
This mini project has definitely been the most challenging so far. I have faced most of the issues on figuring out the logic of the turtle moving along the path using the concepts I have learned on this day. What helped me mitigate these issues is using breakpoints to go through the code incrementally and understand how the while loops and functions are used to create the game's flow. Another key take away since it has been a challenging mini project is that I have annotated my code to remind my understanding of each of the function's purpose within the game.
Overall, I learned a lot from this project and look forward to even more challenging mini projects down the line. To access the full code - https://github.com/keiransystem14/Python100daysofcode/tree/main/Day%206%20Project%20-%20Turtle%20Maze%20Game