title: Creating a (R,G,B) Sierpinski Triangle in python, using pygame window. Each corner is, respectively Red, Green, and Blue and gradient into each other
body: <p>So, for my computer science class we are supposed to import the pygame from the website: </p>
  
  <p><a href="http://www.petercollingridge.co.uk/image-processing-pygame" rel="nofollow">http://www.petercollingridge.co.uk/image-processing-pygame</a></p>
  
  <p>Then we are supposed to create the sierpinski triangle in python in the pygame window using pixels. So each pixel in the window needs to be colored in the shape of the triangle. I can't even get my triangle to show up in just black pixels, and what we're supposed to do is get it to appear with the top corner as red, the bottom left as green, and the bottom right as blue. These colors are supposed to slowly fade into each other (gradient) throughout the triangle. The finished process is supposed to look something like this: </p>
  
  <p><a href="http://eldar.mathstat.uoguelph.ca/dashlock/ftax/Gallery/Sierpenski2D960.gif" rel="nofollow">http://eldar.mathstat.uoguelph.ca/dashlock/ftax/Gallery/Sierpenski2D960.gif</a></p>
  
  <p>First off, everytime I set the window up it says that the midPoint in my main function, where I call the earlier midPoint function is not assigned. This confuses me because I clearly assigned it in the very first function: def midPoint, so any help with that problem would be great. Other than that I'm not sure why I can't get the actual triangle to show up. I just want to start out by getting it to show up black first, and then color it. Any help on what is wrong with my, most likely, awful code would be much appreciated. I am supposed to be minoring in computer science but I am failing the class. I am desperate. Please you can even make fun of my shitty code but I need something, anything. I'm lost. Thank you.</p>
  
  <pre><code>#######################################
  
  import pygame, math, random
  
  pygame.init()
  
  #######################################
  
  def midpoint (x0, x1, y0, y1):
  
      panda = ((x0 + x1)/2)
      polarbear = ((y0 + y1)/2)
      return panda, polarbear
  
  #######################################
  
  def randomPoint (width, height):
  
      potato = (random.random() * width)
      pickle = (random.random() * height)
      return potato, pickle
  
  
  #newImage
  
  #   PRE: Takes size, which is a 2-tuple (width, height) and provides size of image
  #   POST: Creates list of length size[0]. Each item in list is length size[1]. Each item of list is a 3-tuple.
  #
  #   Points of this data structure are denoted as image[x][y] and each point has 3 components: [0] for red, [1] for green, and [2] for blue
  #
  def newImage(size):
  
  return pygame.surfarray.array3d(pygame.Surface(size))
  #######################################
  
  #showImage
  #   Takes image created by newImage and displays it in open window
  #   PRE: image is a list of lists of 3-tuples. Each 3 tuple corresponds to a (R,G,B) color.
  #   POST: image is displayed in window.
  #
  def showImage(image):
  
      width, height, depth = image.shape
      pygame.display.set_mode((width, height))
      surface = pygame.display.get_surface()
      pygame.surfarray.blit_array(surface, image)
      pygame.display.flip()
  
  #######################################
  
  # MAIN PROGRAM
  
  pygame.init()
  
  width = int(input("How large do you want your window? "))
  height = int(input("How tall do you want your window? "))
  window = newImage((width, height))
  
  for x in range(width):
      for y in range(height):
          window[x][y] = (255,255,255) #Colors window white
  showImage(window)
  
  #
  
  p = randomPoint(width, height)
  
  for i in range(1000001):
  
      corners = [(width, height),(0, height),(width//2, 0)]
      c = random.choice(corners)
      mid = midPoint(p[0], p[1], c[0], c[1])
      if i &gt; 10:
          window[(mid[0])][(mid[1])] = (0,0,0)
      p = mid
      if i % 1000 == 0:
          showImage(window)
  
  #
  
  print('Done!')
  
  input("Enter to quit")
  
  pygame.quit()
  #
  #######################################`
  </code></pre>