Preview

3 - Multiple Turtles - understanding objects

 1. Tina is a 'turtle' - but what really is a turtle?

  It is a programmed variable

  It's what's called an 'object' in python

  It's what's called an 'animal' in python

  It is an animal that is directly related to a python snake

 2. Note that Tina can do things (these are called 'methods') such as:

  Exist in space-time reality

  Go forward, backward, left and right

  Be a certain colour and a certain shape

  come to life from the code

 3. At the beginning of all of our examples, we "import turtle". This lets us …..

  name our program, in this case 'turtle'

  use code that's already been written and gives Tina and any other turtles we make their abilities

  see turtles on the screen

  remind ourselves that we are dealing with a turtle

 4. Copy and paste this code into the trinket and analyse it. What happens when you run it?
import turtle

tina = turtle.Turtle()
tina.shape('turtle')
tina.forward(100)
tina.left(90)

jonathan = turtle.Turtle()
jonathan.shape('turtle')
jonathan.forward(120)
tina.left(200)

  It does nothing - you cannot have two turtles

  It creates two turtles - Tina and Jonathan, both are based on the turtle object

  It creates one turtle but replaces the text on Tina to suggest it is two turtles

  It produces a single turtle as one will over write the other

 5. On what line of the code is the new object 'jonathan' that is based on the turtle pre-existing object created?
import turtle

tina = turtle.Turtle()
tina.shape('turtle')
tina.forward(100)
tina.left(90)

jonathan = turtle.Turtle()
jonathan.shape('turtle')
jonathan.forward(120)
tina.left(200)

  Line 1

  It isn't

  Line 8

  Line 3

 6. Analyse the code below and decide which of the following statements is true
import turtle

tina = turtle.Turtle()
jonathan=turtle.Turtle()
ruth=turtle.Turtle()
tommy=turtle.Turtle()

tina.shape('turtle')
tina.color('black')
ruth.color('green')

ruth.left(199)
ruth.write("I'm Ruth")

tina.left(90)
tina.forward(100)
tina.write("I'm Tina!")
tina.forward(20)
tina.right(90)

tommy = turtle.Turtle()
tommy.shape('turtle')
tommy.color('black')

tommy.right(90)
tommy.forward(100)
tommy.write("I'm Tommy!")
tommy.forward(20)
tommy.left(90)

  It is not possible to create more than four turtles from the turtle object

  All turtles created from the turtle obejct have a shape and color/colour - this can be specified for the specific object.

  All turtles created from the turtle obejct are exactly the same size and shape - so have exactly identical attributes to each other

  All turtles created from the turtle object have to have the same color/colour (e.g. black)

 7. The following code creates a space rocket that you can move with mouse keys. How would you add a space background (image)
# Click in the righthand window to make it active then use your arrow
# keys to control the spaceship!
import turtle

screen = turtle.Screen()

# this assures that the size of the screen will always be 400x400 ...
screen.setup(400, 400)

# ... which is the same size as our image
# now set the background to our space image


# Or, set the shape of a turtle
screen.addshape("rocketship.png")
turtle.shape("rocketship.png")

move_speed = 10
turn_speed = 10

# these defs control the movement of our "turtle"
def forward():
  turtle.forward(move_speed)

def backward():
  turtle.backward(move_speed)

def left():
  turtle.left(turn_speed)

def right():
  turtle.right(turn_speed)

turtle.penup()
turtle.speed(0)
turtle.home()

# now associate the defs from above with certain keyboard events
screen.onkey(forward, "Up")
screen.onkey(backward, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.listen()

  It has been done - see line 10

  import space.jpg right at the start before 'import turtle'

  Python, please add a space background(space.jpg) on line 1

  space=turtle.Turtle(space.jpg)

 8. This is another implementation of turtle to create an online painter. What is the name of the object created based on Turtle here?
*refer to the first quarter or half of the program

  black.turtle

  screen.tracer

  brush_turtle

  ColorPicker()

 9. What will the following code produce? How many objects are in use?
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

   A circle and there is just one object in use

  A rectangle - and there are no objects in use

  A square - and there is just one object in use (turtle)

  A square, and there are four objects (including left and right)

 10. To recap, an object can have various methods — ________________ — and it can also have attributes — (sometimes called properties). For example, each turtle has a color attribute

  where it comes from

  what it looks like

  things it can do

  what it should be