Lab Assignment #2

TURTLE BLAST!!


DESCRIPTION
In this lab assignment, I will explain how Turtle Blast works. Turtle Blast is a graphical effect done by the turtle program, in python, that tells the turtle to draw multiple lines at different lengths at different angles while returning to its original location on the screen. The end result should look like the turtle is going into warp speed or the turtle shattered a window.


CODE
import turtle
import random
wall = turtle.Screen()
wall.bgcolor(“red”)
spade = turtle.Turtle()
spade.pencolor(“white”)
spade.speed(0)
for x in range(100):
rotate = random.randint(0,45)
spade.right(rotate)
stretch = random.randint(0,150)
spade.forward(stretch)
spade.back(stretch)


CODE DESCRIPTION
The code above is what I use to create the Turtle Blast effect. Here’s what you must do to create the same effect. Open the python shell. Select File. Then, select New File. It will open the IDLE screen. Then, add the following codes.

Line 1, import turtle. Import turtle is a turtle module that allows you to use the turtle.Screen and the turtle.Turtle program. It creates a screen and a turtle for graphic art and design purposes.

Line 2, import random is another module that imports a random number generator when using specific codes.

Line 3, name = turtle.Screen is part of the turtle module. The variable name, to the left of the assignment symbol (=), allows you to change the name of the code. I chose the name, wall.

Line 4, name.bgcolor() allows you to change the background color of the screen. I chose the color, red. Remember to change the name to the name in line 3.

Line 5, name = turtle.Turtle creates a turtle on the screen. Choose a name for the turtle. After naming the turtle, any task you want the turtle to do, use the name you named the turtle or the program will cause an error. I chose the name, spade.

Line 6, name.pencolor() allows you to change the turtle’s pen color. It’s a pen the turtle use to draw on the screen. I chose the color, white.

Line 7, name.speed() is a speed code for the turtle. Without the code, the turtle will draw at a normal speed. Adding a number in the () will make the turtle draw quickly.

Line 8, for x in range(): is a code that loops (repeats) any task below line 8. If you add 100 in the (), the task will loop 100 times.

Line 9, name = random.randint() is part of the random module. It’s a random number generator that generate numbers at will. For example, if the code has (0,100), the random generator chooses a number from 0 to 100. If the first number is larger than the second number (100,0), it will cause an error. If the first number is equal to the second number (10,10), only the number 10 will be generated. (10,20) means any number from 10 to 20 will be generated. Also, adding numbers in the () is the speed level of the turtle’s rotation. 0 means no rotation. 100 means a fast rotation. Remember, the numbers are random in this code.

Line 10, name.right() turns the turtle’s angle by adding a number in the (). Adding a number in the () ruins the Turtle Blast effect. I added the variable name in line 9.

Line 11, name = random.randint() is another part of the random module. Adding a number in the () is the length of the line determined by the random number generator. Whatever number that’s generated, the turtle will draw the length of that line on the screen.

Line 12 and line 13, name.forward() and name.back() tells the turtle to move forward and backward. Adding a number in the () ruins the Turtle Blast effect causing the turtle to draw crazy lines or even draw lines off the screen. I stuck with the variable name in line 11.

After adding the codes, select Run at the top and select Run Module. A prompt will appear. Click Ok. Name and save the .py file on the desktop. Then, watch the turtle create the Turtle Blast effect.


Result
Here’s a pic of the Turtle Blast effect. All effects should look different due to the random number generator.

turtle blast