Learning how to make basic graphics thingies
So now I'm going to metaphorically throw you in the deep end, and we're going to learn how to use turtle. Turtle is a tool in Python built for learners, where you can create drawings by manipulating a virtual cursor (called a turtle).
I'm going to start by throwing some code at you. Without running/executing the code, can you try to figure out what it does?
Maybe grab a sheet of paper and a pencil, and try to predict what is going to be drawn. Keep in mind that at the moment, you know nothing about this code.
# these next two lines of code are always required
import turtle
t = turtle.Turtle()
t.forward(200) # move forward by 50 pixels
t.right(90) # rotate clockwise by 90 degrees
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
# also necessary
turtle.done()
Now, copy-paste it into a new Python file, and execute it. Was your guess right? Wrong? Can you figure out what the code's doing?
Have a play around, see if you can figure out what all of this code does! Note that you'll always need the import turtle
, t = turtle.Turtle()
and turtle.done()
lines. Don't worry if you don't understand them at all yet, we'll cover them later!
import turtle
t = turtle.Turtle()
t.forward(100)
t.right(60)
t.penup()
t.forward(20)
t.pendown()
t.pencolor("blue")
t.right(60)
t.backward(40)
t.setposition(0, 0)
t.dot()
t.setposition(50, 100)
t.dot()
t.penup()
t.forward(300)
t.pendown()
t.pencolor("red")
t.left(60)
t.forward(50)
turtle.done()