title: how to change the color a section of text in pygame
body: <p>I am creating a game with pygame in which the color of a letter changes when you type that letter. Like nitrotype.com. However the problem is that I don't know how to change the colour of individual letters.
  I can't clear the screen and then do it because then that would change the color of the entire line.
  So either I need a way to change the colour of individual letters or a way to just put single letters on the screen one at a time. However I don't know how to uniformly put the letters(such that the end sentence is centered). Please could someone help me out here. Either by telling me how to change the color of individual letters or how to put individual letters in a perfect manner and then change their color.</p>
  <pre><code>import pygame as pg
  import pygame
  
  pg.init()
  screenHeight, screenWidth = 600, 800
  gameDisplay = pg.display.set_mode((screenWidth, screenHeight))
  pg.display.set_caption(&quot;Nitrotype&quot;)
  
  black = (255, 255, 255)
  white = (0, 0, 0)
  gameDisplay.fill(white)
  pg.display.update()
  
  gameOn = True
  with open(&quot;text.txt&quot;, &quot;r&quot;) as f:
      contents = f.read()
  
  def msgToScreen(msg, color, size):
      cur = []
      strings = []
      words = msg.split(&quot; &quot;)
      for i in words:
          cur.append(i)
          if len(&quot; &quot;.join(cur)) &gt;= 35:
              strings.append(&quot; &quot;.join(cur))
              cur = []
      if cur != []:strings.append(&quot; &quot;.join(cur))
      
      curY = 20
      
      for string in strings:
          font = pg.font.SysFont(None, size)
          text = font.render(string, True, color)
          text_rect = text.get_rect(center=(screenWidth/2, curY))
          gameDisplay.blit(text, text_rect)
          curY += 40
      
      return text
  
  textOnScreen = msgToScreen(contents, black, 50)
  
  pg.display.update()
  
  curIdx = 0
  keyCombination = {&quot;a&quot;:pg.K_a, &quot;b&quot;:pg.K_b, &quot;c&quot;:pg.K_c, &quot;d&quot;:pg.K_d, &quot;e&quot;:pg.K_e, &quot;f&quot;:pg.K_f,
                  &quot;g&quot;:pg.K_g, &quot;h&quot;:pg.K_h, &quot;i&quot;:pg.K_i, &quot;j&quot;:pg.K_j, &quot;k&quot;:pg.K_k, &quot;l&quot;:pg.K_l,
                  &quot;m&quot;:pg.K_m, &quot;n&quot;:pg.K_n, &quot;o&quot;:pg.K_o, &quot;p&quot;:pg.K_p, &quot;q&quot;:pg.K_q, &quot;r&quot;:pg.K_r,
                  &quot;s&quot;:pg.K_s, &quot;t&quot;:pg.K_t, &quot;u&quot;:pg.K_u, &quot;v&quot;:pg.K_v, &quot;w&quot;:pg.K_w, &quot;x&quot;:pg.K_x,
                  &quot;y&quot;:pg.K_y, &quot;z&quot;:pg.K_z}
  while gameOn:
      for event in pygame.event.get():
          if event.type == pg.QUIT:
              gameOn = False
          if event.type == pg.KEYDOWN:
              if event.key == keyCombination[contents[curIdx].lower()]:
                  #Here is where the color of the current letter should change
                  curIdx += 1
  
  pg.quit()
  
  </code></pre>