title: Pygame Error displaying and moving an image on a 5x5 grid of images
body: <p>works for a second then says x is referenced before assignment. Not suprised considing of how badly optizmed my code is. Problematic part is the grid() part, basically im just testing if it can display a red sqaure over the sqaures i loaded in. I tried doing it with a extra function but it stopped working? Moving the display.blit forward one made it work fine for like 2 seconds. heres my code</p>
  <pre><code>import pygame
  import random
  import time
  import sys
  
  pygame.init()
  background_colour = (255,255,255)
  (width, height) = (400, 500)
  
  display = pygame.display.set_mode((width, height))
  FPS = pygame.time.Clock()
  pygame.display.set_caption(&quot;Game&quot;)
  pygame.display.flip()
  
  # Land
  gras = pygame.image.load(&quot;assets/land/grass.png&quot;)
  attack = pygame.image.load(&quot;assets/land/attac.png&quot;)
  # UI
  Tool = pygame.image.load(&quot;assets/UIs/Toolbar.png&quot;)
  # rifle
  riflesigil = pygame.image.load(&quot;assets/Rifle/helm1.png&quot;)
  rifleforward = pygame.image.load(&quot;assets/Rifle/Rifle.png&quot;)
  rifleleft = pygame.image.load(&quot;assets/Rifle/Rifle2.png&quot;)
  rifledown = pygame.image.load(&quot;assets/Rifle/Rifle3.png&quot;)
  rifleright = pygame.image.load(&quot;assets/Rifle/Rifle4.png&quot;)
  
  class toolbar(pygame.sprite.Sprite):
    def __init__(self, image):
      self.image = image
      
  class MAP(pygame.sprite.Sprite):
    def __init__(self, grass, red, display):
      self.count = 5
      self.red = pygame.transform.scale(red, (100, 100))
      self.gras = grass
      self.grass = pygame.transform.scale(self.gras, (100, 100))
    def gen(self, display):
      row = 0
      row2 = 0
      increase = 76
      for x in range(1, 6):
        for x in range(1, 6):
          display.blit(self.grass, (increase * row, row2 * increase))
          row += 1
          if row == 5:
            row = 0
        row2 += 1
    def grid(self, display, pos1, pos2):
      self.red = pygame.transform.scale(attack, (100, 100))
      if pos1 in range(0, 75):
        x = 0
      elif pos1 in range(76, 151):
        x = 76
      elif pos1 in range(152, 227):
        x = 152
      elif pos1 in range(228, 303):
        x = 228
      elif pos1 in range(304, 379):
        x = 304
      if pos2 in range(0, 75):
        y = 0
      elif pos2 in range(76, 151):
        y = 76
      elif pos2 in range(152, 227):
        y = 152
      elif pos2 in range(228, 303):
        y = 228
      elif pos2 in range(304, 379):
        y = 304
      display.blit(self.red, (x, y))
  map = MAP(gras, attack, display)
  
  while True:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              exit()
      display.fill((255, 255, 255))
      (mouseX, mouseY) = pygame.mouse.get_pos()
      map.gen(display)
      map.grid(display, mouseX, mouseY)
      pygame.display.update()
  # 152, 228, 304, 380
  </code></pre>