title: Python CodeSkulptor Pause Drawing from Inside For Loop
body: <p>I want to build some visualizations for searching algorithms (BFS, A* etc.) within a grid.</p>
  
  <p>My solution should show each step of the algorithm using CodeSkulptor <code>simplegui</code> (or the offline version using <code>SimpleGUICS2Pygame</code>.)</p>
  
  <p>I've made a version which highlights all the cells visited by changing their color, but I've run into trouble trying to make the path display step-by-step with a time delay between each step.</p>
  
  <p>I've extracted the essence of the problem and created a minimal example representing it in the code below, also run-able online here: <a href="http://www.codeskulptor.org/#user47_jB2CYfNrH2_2.py" rel="nofollow noreferrer">http://www.codeskulptor.org/#user47_jB2CYfNrH2_2.py</a></p>
  
  <p>What I want is during the <code>change_colors()</code> function, for there to be a delay between each iteration.
  CodeSkulptor doesn't have <code>time.sleep()</code> available, and I don't think it would help anyway.</p>
  
  <p>CodeSkulptor does have timers available, which might be one solution, although I can't see how to use one in this instance.</p>
  
  <p>Code below:</p>
  
  <pre><code>import time
  
  try:
      import simplegui
  except ImportError:
      import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
  
      simplegui.Frame._hide_status = True
  
  TITLE = "TEST"
  FRAME_WIDTH = 400
  FRAME_HEIGHT = 400
  DELAY = 10
  
  
  class Square:
      """This class represents a simple Square object."""
  
      def __init__(self, size, pos, pen_size=2, pen_color="red", fill_color="blue"):
          """Constructor - create an instance of Square."""
          self._size = size
          self._pos = pos
          self._pen_size = pen_size
          self._pen_color = pen_color
          self._fill_color = fill_color
  
      def set_color(self, color):
          self._fill_color = color
  
      def get_color(self):
          return self._fill_color
  
      def is_in(self, pos):
          """
          Determine whether coordinates are within the area of this Square.
          """
          return self._pos[0] &lt; pos[0] &lt; self._pos[0] + self._size and self._pos[1] &lt; pos[1] &lt; self._pos[1] + self._size
  
      def draw(self, canvas):
          """
          calls canvas.draw_image() to display self on canvas.
          """
          points = [(self._pos[0], self._pos[1]), (self._pos[0] + self._size, self._pos[1]),
                    (self._pos[0] + self._size, self._pos[1] + self._size), (self._pos[0], self._pos[1] + self._size)]
          canvas.draw_polygon(points, self._pen_size, self._pen_color, self._fill_color)
  
      def __str__(self):
          return "Square: {}".format(self._pos)
  
  
  def draw(canvas):
      for square in squares:
          square.draw(canvas)
  
  
  def change_colors():
      for square in squares:
          # time.sleep(1) # Not implemented in CodeSkulptor and would'nt work anyway
          square.set_color("green")
  
  
  frame = simplegui.create_frame(TITLE, FRAME_WIDTH, FRAME_HEIGHT)
  frame.set_draw_handler(draw)
  
  width = 20
  squares = []
  for i in range(10):
      squares.append(Square(width, (i * width, 0)))
  
  change_colors()
  
  frame.start()
  </code></pre>
  
  <p>Any help appreciated.</p>