Adding a Second Turtle to the Race

Do this on pencilcode.net

t1 = turtle        
t2 = new Turtle red
t2.bk 170          
t2.rt 90           
play1 = true       
play2 = true       
speed Infinity
tick 60, ->
  if play1
    if pressed "up" then t1.fd 1
    if pressed "left" then t1.lt 1
    if not t1.touches gold
      write "#1 Game Over"
      play1 = false
  if play2
    if pressed "W" then t2.fd 1
    if pressed "A" then t2.lt 1
    if not t2.touches gold
      write "#2 Game Over"
      play2 = false

To make a new turtle, pick a name like x and write x = new Turtle. The turtle can be moved by putting x. in front of commands for that turtle. For example, x.fd 100 moves it forward.

This program creates a new red turtle (using the name t2) by writing t2 = new Turtle red. Then t2.bk 170 and t2.rt 90 are used to move it into position.

The main turtle comes with a predefined name: turtle, so fd 1 and turtle.fd 1 do exactly the same thing. This program sets up t1 = turtle, so now t1.fd 1 will also do exactly the same thing. Why do you think the author of this program set up the new name t1?

"Game over" is handled in a new way here. Since there are two players, we want to let one player play after the other player runs off the track. So this program uses two new variables play1 and play2 to keep track of which of the players is still playing.

At the start, both play1 = true and play2 = true, which means both players are playing. If just player 1 runs off the track, we set play1 = false, and then the line if play1 will stop player 1's keyboard controls from running, even as the timer continues running for player 2.