title: python ontimer vs thread
body: <p>This is an extension of my recent post. Now that I know about onTimer and Thread, starting a new post.</p>
  
  <p>I did away w/ the while loop in the main. Replaced with onTimer() and Thread. But same results, cant move both objects simultaneously. Code for both listed below: Any advice is appreciated...this is my first go at graphics w/ Python.</p>
  
  <h1>code using onTimer</h1>
  
  <pre><code>import turtle
  import math
  
  screen = turtle.Screen()
  screen.screensize(1000, 1000)
  screen.bgcolor("light green")
  
  player = turtle.Turtle()
  player.hideturtle()
  player.penup()
  player.shape("circle")
  player.color("red")
  player.fillcolor("yellow")
  player.setposition(-50, -40)
  player.showturtle()
  player.speed(0)
  
  s = turtle.Turtle()
  s.hideturtle()
  s.penup()
  s.shape("square")
  s.color("black")
  s.fillcolor("white")
  s.setposition(-50, -60)
  s.showturtle()
  
  # first tier
  s1 = turtle.Turtle()
  s1.hideturtle()
  s1.penup()
  s1.shape("square")
  s1.color("black")
  s1.fillcolor("white")
  s1.showturtle()
  
  s2 = turtle.Turtle()
  s2.hideturtle()
  s2.penup()
  s2.shape("square")
  s2.color("black")
  s2.fillcolor("white")
  s2.setposition(s1.xcor() + 20, 0)
  s2.showturtle()
  
  s3 = turtle.Turtle()
  s3.hideturtle()
  s3.penup()
  s3.shape("square")
  s3.color("black")
  s3.fillcolor("white")
  s3.setposition(s2.xcor() + 20, 0)
  s3.showturtle()
  
  s4 = turtle.Turtle()
  s4.hideturtle()
  s4.penup()
  s4.shape("square")
  s4.color("black")
  s4.fillcolor("white")
  s4.setposition(s3.xcor() + 20, 0)
  s4.showturtle()
  
  # second tier
  s5 = turtle.Turtle()
  s5.hideturtle()
  s5.penup()
  s5.shape("square")
  s5.color("black")
  s5.fillcolor("white")
  s5.setposition(s4.xcor() + 30, 60)
  s5.showturtle()
  
  s6 = turtle.Turtle()
  s6.hideturtle()
  s6.penup()
  s6.shape("square")
  s6.color("black")
  s6.fillcolor("white")
  s6.setposition(s5.xcor() + 20, 60)
  s6.showturtle()
  
  s7 = turtle.Turtle()
  s7.hideturtle()
  s7.penup()
  s7.shape("square")
  s7.color("black")
  s7.fillcolor("white")
  s7.setposition(s6.xcor() + 20, 60)
  s7.showturtle()
  
  s8 = turtle.Turtle()
  s8.hideturtle()
  s8.penup()
  s8.shape("square")
  s8.color("black")
  s8.fillcolor("white")
  s8.setposition(s7.xcor() + 20, 60)
  s8.showturtle()
  
  # third tier
  s9 = turtle.Turtle()
  s9.hideturtle()
  s9.penup()
  s9.shape("square")
  s9.color("black")
  s9.fillcolor("white")
  s9.setposition(s8.xcor() + 30, 120)
  s9.showturtle()
  
  s10 = turtle.Turtle()
  s10.hideturtle()
  s10.penup()
  s10.shape("square")
  s10.color("black")
  s10.fillcolor("white")
  s10.setposition(s9.xcor() + 20, 120)
  s10.showturtle()
  
  s11 = turtle.Turtle()
  s11.hideturtle()
  s11.penup()
  s11.shape("square")
  s11.color("black")
  s11.fillcolor("white")
  s11.setposition(s10.xcor() + 20, 120)
  s11.showturtle()
  
  s12 = turtle.Turtle()
  s12.hideturtle()
  s12.penup()
  s12.shape("square")
  s12.color("black")
  s12.fillcolor("white")
  s12.setposition(s11.xcor() + 20, 120)
  s12.showturtle()
  
  #moving enemy
  s13 = turtle.Turtle()
  s13.hideturtle()
  s13.penup()
  s13.shape("square")
  s13.color("black")
  s13.fillcolor("red")
  s13.setposition(s11.xcor() + 5, 140)
  s13.showturtle()
  
  #static turtle
  s14 = turtle.Turtle()
  s14.hideturtle()
  s14.penup()
  s14.shape("turtle")
  s14.color("black")
  s14.fillcolor("red")
  s14.setposition(s6.xcor() + 20, 80)
  s14.showturtle()
  
  squareList = [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12]
  
  falling = False
  
  def jump():
      speed = 2
  
      while speed &gt; 0:
          y = player.ycor()
          x = player.xcor()
          y += 6
          x += 1.3
          speed -= 0.09
          player.setposition(x, y)
          collision()
      if speed &lt; 0:
          falling = True
          while falling:
              y = player.ycor()
              x = player.xcor()
              y -= 2
              x += 1
              speed += .10
              player.setposition(x, y)
              collision()
              for squares in squareList:
                  if (player.xcor() &gt;= 0 and player.xcor() &lt;= 80) and player.ycor() == 20:
                      falling = False
                  if (player.xcor() &gt;= 80 and player.xcor() &lt;= 160) and player.ycor() == 80:
                      falling = False
                  if (player.xcor() &gt;= 160 and player.xcor() &lt;= 240) and player.ycor() == 140:
                      falling = False
  
  
  def right():
      x = player.xcor()
      x += 2
      player.setx(x)
      collision()
  
  def moveEnemy():
      s13.backward(3)
      if s13.xcor() &gt; 250:
          s13.setheading(0)
      if s13.xcor() &lt; 180:
          s13.setheading(180)
      screen.ontimer(moveEnemy(), 5)
      collision()
  
  def collision():
      distance1 = math.sqrt(math.pow(player.xcor()-s13.xcor(),2) + math.pow(player.ycor()-s13.ycor(), 2))
      if distance1 &lt; 20:
          player.hideturtle()
  
      distance2 = math.sqrt(math.pow(player.xcor() - s14.xcor(), 2) + math.pow(player.ycor() - s14.ycor(), 2))
      if distance2 &lt; 20:
          s14.hideturtle()
  
  turtle.listen()
  turtle.onkey(jump, "Up")
  turtle.onkey(right, "Right")
  
  screen.ontimer(moveEnemy(), 5)
  
  turtle.mainloop()
  </code></pre>
  
  <h1>code using Thread</h1>
  
  <pre><code>import turtle
  import math
  import threading
  from threading import Thread
  
  screen = turtle.Screen()
  screen.screensize(1000, 1000)
  screen.bgcolor("light green")
  
  player = turtle.Turtle()
  player.hideturtle()
  player.penup()
  player.shape("circle")
  player.color("red")
  player.fillcolor("yellow")
  player.setposition(-50, -40)
  player.showturtle()
  player.speed(0)
  
  s = turtle.Turtle()
  s.hideturtle()
  s.penup()
  s.shape("square")
  s.color("black")
  s.fillcolor("white")
  s.setposition(-50, -60)
  s.showturtle()
  
  # first tier
  s1 = turtle.Turtle()
  s1.hideturtle()
  s1.penup()
  s1.shape("square")
  s1.color("black")
  s1.fillcolor("white")
  s1.showturtle()
  
  s2 = turtle.Turtle()
  s2.hideturtle()
  s2.penup()
  s2.shape("square")
  s2.color("black")
  s2.fillcolor("white")
  s2.setposition(s1.xcor() + 20, 0)
  s2.showturtle()
  
  s3 = turtle.Turtle()
  s3.hideturtle()
  s3.penup()
  s3.shape("square")
  s3.color("black")
  s3.fillcolor("white")
  s3.setposition(s2.xcor() + 20, 0)
  s3.showturtle()
  
  s4 = turtle.Turtle()
  s4.hideturtle()
  s4.penup()
  s4.shape("square")
  s4.color("black")
  s4.fillcolor("white")
  s4.setposition(s3.xcor() + 20, 0)
  s4.showturtle()
  
  # second tier
  s5 = turtle.Turtle()
  s5.hideturtle()
  s5.penup()
  s5.shape("square")
  s5.color("black")
  s5.fillcolor("white")
  s5.setposition(s4.xcor() + 30, 60)
  s5.showturtle()
  
  s6 = turtle.Turtle()
  s6.hideturtle()
  s6.penup()
  s6.shape("square")
  s6.color("black")
  s6.fillcolor("white")
  s6.setposition(s5.xcor() + 20, 60)
  s6.showturtle()
  
  s7 = turtle.Turtle()
  s7.hideturtle()
  s7.penup()
  s7.shape("square")
  s7.color("black")
  s7.fillcolor("white")
  s7.setposition(s6.xcor() + 20, 60)
  s7.showturtle()
  
  s8 = turtle.Turtle()
  s8.hideturtle()
  s8.penup()
  s8.shape("square")
  s8.color("black")
  s8.fillcolor("white")
  s8.setposition(s7.xcor() + 20, 60)
  s8.showturtle()
  
  # third tier
  s9 = turtle.Turtle()
  s9.hideturtle()
  s9.penup()
  s9.shape("square")
  s9.color("black")
  s9.fillcolor("white")
  s9.setposition(s8.xcor() + 30, 120)
  s9.showturtle()
  
  s10 = turtle.Turtle()
  s10.hideturtle()
  s10.penup()
  s10.shape("square")
  s10.color("black")
  s10.fillcolor("white")
  s10.setposition(s9.xcor() + 20, 120)
  s10.showturtle()
  
  s11 = turtle.Turtle()
  s11.hideturtle()
  s11.penup()
  s11.shape("square")
  s11.color("black")
  s11.fillcolor("white")
  s11.setposition(s10.xcor() + 20, 120)
  s11.showturtle()
  
  s12 = turtle.Turtle()
  s12.hideturtle()
  s12.penup()
  s12.shape("square")
  s12.color("black")
  s12.fillcolor("white")
  s12.setposition(s11.xcor() + 20, 120)
  s12.showturtle()
  
  #moving enemy
  s13 = turtle.Turtle()
  s13.hideturtle()
  s13.penup()
  s13.shape("square")
  s13.color("black")
  s13.fillcolor("red")
  s13.setposition(s11.xcor() + 5, 140)
  s13.showturtle()
  
  #static turtle
  s14 = turtle.Turtle()
  s14.hideturtle()
  s14.penup()
  s14.shape("turtle")
  s14.color("black")
  s14.fillcolor("red")
  s14.setposition(s6.xcor() + 20, 80)
  s14.showturtle()
  
  squareList = [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12]
  
  falling = False
  
  def jump():
      speed = 2
  
      while speed &gt; 0:
          y = player.ycor()
          x = player.xcor()
          y += 6
          x += 1.3
          speed -= 0.09
          player.setposition(x, y)
          collision()
      if speed &lt; 0:
          falling = True
          while falling:
              y = player.ycor()
              x = player.xcor()
              y -= 2
              x += 1
              speed += .10
              player.setposition(x, y)
              collision()
              for squares in squareList:
                  if (player.xcor() &gt;= 0 and player.xcor() &lt;= 80) and player.ycor() == 20:
                      falling = False
                  if (player.xcor() &gt;= 80 and player.xcor() &lt;= 160) and player.ycor() == 80:
                      falling = False
                  if (player.xcor() &gt;= 160 and player.xcor() &lt;= 240) and player.ycor() == 140:
                      falling = False
  
  
  def right():
      x = player.xcor()
      x += 2
      player.setx(x)
      collision()
  
  def moveEnemy():
      s13.backward(3)
      if s13.xcor() &gt; 250:
          s13.setheading(0)
      if s13.xcor() &lt; 180:
          s13.setheading(180)
      screen.ontimer(moveEnemy(), 5)
      collision()
  
  def collision():
      distance1 = math.sqrt(math.pow(player.xcor()-s13.xcor(),2) + math.pow(player.ycor()-s13.ycor(), 2))
      if distance1 &lt; 20:
          player.hideturtle()
  
      distance2 = math.sqrt(math.pow(player.xcor() - s14.xcor(), 2) + math.pow(player.ycor() - s14.ycor(), 2))
      if distance2 &lt; 20:
          s14.hideturtle()
  
  turtle.listen()
  turtle.onkey(jump, "Up")
  turtle.onkey(right, "Right")
  
  Thread(target = moveEnemy).start()
  
  turtle.mainloop()
  </code></pre>