Lab #3: Turtle Blast

In today’s lab, we were able to manipulate a “turtle” in Python. The turtle is basically an arrow on the screen that moves when you insert a command you want it to do, and while it moves it leaves a line behind drawing in the window.

In the turtle blast part of the lab, we had to used what we learned: loops and the random module. 100 lines had to made for the blast shape so a for loop iterating a range from 0 to 100 would repeat the block of code inside 100 times as needed.

But first we have to set up the window before actually drawing anything. So three lines of code was written. One sets the windows background color to red, second one sets the turtle “arrow” color to white, third on sets the color of the line it draws to white.

Inside the loop is instructions for the turtle movement. I first turn the turtle to the left with a random angle value between 0 and 45. Next I create another random value except its for the forward movement, but before even calling the function, the value must be saved in a variable.

The reason for saving it in a variable is that we need the turtle to move forward and then return to origin, so we can’t call randint twice or else it would return two different values. Afterwards the turtle is told to move forwards and then backward the value the variable holds. (I used forward again but with negative sign inside, the main intention was to use turt.backward(dist) but they both work as intended)

Again the piece of code is repeated 100 times and generates this:

Capture

 

The actual process is slow but could be sped up by adding turt.speed(10) before the for loop, or if you want to see the turtle movement better, it can be slowed down with turt.speed(2).