Preview

3 - Multiple Turtles - understanding objects

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

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

  It is a programmed variable

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

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

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

  Be a certain colour and a certain shape

  come to life from the code

  Go forward, backward, left and right

  Exist in space-time reality

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

  name our program, in this case 'turtle'

  remind ourselves that we are dealing with a turtle

  see turtles on the screen

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

 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 produces a single turtle as one will over write the other

  It does nothing - you cannot have two turtles

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

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

 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)

  It isn't

  Line 1

  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)

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

  It is not possible to create more than four turtles from the turtle 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()

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

  Add this to line 12: screen.bgpic(

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

  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

  screen.tracer

  black.turtle

  ColorPicker()

  brush_turtle

 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 rectangle - and there are no objects in use

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

   A circle and there is just one object in use

  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

  things it can do

  what it should be

  what it looks like

  where it comes from