Previously in Snap! we have been used to programming with sprites. We will do something similar to help bridge your understanding of how computational thinking can be realized either in Snap! or python.
Now let's start with some basics of python using something we are already familiar with turtle graphics. Remember in Snap!, we had our sprite, and the sprite could move around the stage. We can do the same thing with python. To do this, lets start by importing the turtle package. But before we do that, we need to make sure that we are using the right version of python. For this class, the good folks at Beauty and Joy of Computing have decided that we should stick with python3.
In [1]:
import turtle
wn = turtle.Screen() # creates a graphics window
The python code brings in the turtle graphics package by using the keyword "import." The turtle package is made up of a series of functions that allows the user to move a cybernetic turtle around in space. Very similar to alonzo in our Snap! environment.
In [2]:
alonzo = turtle.Turtle() # create a turtle named alonzo
alonzo.forward(150) # tell alex to move forward by 150 units
alonzo.left(90) # turn by 90 degrees
alonzo.forward(75)
You should see something like this in your window.
In [3]:
alonzo.forward(150) # tell alonzo to move forward by 150 units
alonzo.left(90) # turn by 90 degrees
alonzo.forward(150)
alonzo.left(90)
alonzo.forward(90)
alonzo.shape("turtle") # throwing this piece of code in there, changes the shape of the sprite
In [4]:
from IPython.display import HTML
HTML('<iframe src=https://docs.python.org/3.2/library/turtle.html width=800 height=500></iframe>')
Out[4]:
Ah, once I scroll down the page, I see there is a clear() command. Now I can clear my screen, and start all over again with making the square.
In [5]:
alonzo.clear() # clear my screen
alonzo.shape("turtle") # make the shape of a turtle
alonzo.forward(150) # tell alonzo to move forward by 150 units
alonzo.left(90) # turn by 90 degrees
alonzo.forward(150)
alonzo.left(90)
alonzo.forward(150)
alonzo.left(90)
alonzo.forward(150)
In [6]:
alonzo.clear() # clear my screen
alonzo.shape("turtle") # make the shape of a turtle
for x in range (0, 4):
alonzo.forward(150) # tell alonzo to move forward by 150 units
alonzo.left(90) # turn by 90 degrees
Now that we have done that, lets make a more interesting visualization before we move on to more complex stuff. Lets make a fun starburst using turtle graphics. The code is included below. You should get an image like the one below.
In [8]:
#clear()
#penup()
from turtle import *
shape("turtle")
color('blue', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(position()) < 1:
break
end_fill()
1) A more advanced block we made in Snap! was this block. Can you write the same block in Python?
In [1]:
# Your code here
In [2]:
# Your code here
By the way, did you know that the guy who help invent the field of Artificial Intelligence created Turtle Graphics? His name is Seymour Papert and he has the most delighful book on computational thinking called Mindstorms, Children, Computers and Powerful Ideas. You can read it free on the web.