title: Highlighting tile on Pygame tilemap
body: <p>I'm making a simple tile-based game with Pygame.</p>
  
  <p>Currently, it displays a 10x10 grid of randomly selected tiles. That part is working perfectly, but I have a problem with highlighting.</p>
  
  <p>When you mouse over a tile, it should load a greyish tile at about half opacity. It loads, but the opacity isn't working correctly. If you put the mouse on a tile, it loads correctly with the opacity and all. But if you move the mouse around the tile, it turns normal, without opacity.</p>
  
  <p>I think it's happening because it's loading the highlighting tile every time an event occurs, but I'm not sure how to fix it.</p>
  
  <p>I'm using a class called Tilemap that I made to generate and draw the tilemap. I think it's something in the <code>draw()</code> function that's causing all of this.</p>
  
  <pre><code>import pygame
  import sys
  import random
  from pygame.locals import *
  
  running = True
  
  class Tilemap:
      tilemap = []
      ht = None # ht = highlight texture
  
      def __init__(self, height, width, tilesize, textures):
          self.height = height # How many tiles high it is
          self.width = width # How many tiles wide it is
          self.tilesize = tilesize # How many pixels each tile is
          self.textures = textures # The textures
          self.size = (self.width*self.tilesize,self.height*self.tilesize)
  
      def generate_random(self):
          # Generate a random tilemap
          self.tilemap = [[random.randint(0, len(
              self.textures)-1) for e in range(self.width)] for e in range(
                  self.height)]
  
      def draw(self, display, mouse=None):
          mouse = mouse
          # Draw the map
          for row in range(self.width):
              for column in range(self.height):
  
  
  
                  texture = self.textures[self.tilemap[row][column]]
                  # Highlight a tile (this is where the problem is)
                  if self.ht != None:
                      if mouse[0] &gt;= (column*self.tilesize) and mouse[0] &lt;= (
                          column*self.tilesize)+self.tilesize:
                          if mouse[1] &gt;= (row*self.tilesize) and mouse[1] &lt;= (
                              row*self.tilesize)+self.tilesize:
                              texture = self.ht
  
                  display.blit(texture,
                               (column*self.tilesize, row*self.tilesize))
  
  
  
  
  tilemap = Tilemap(10,10,40,
                    # Load the textures
                    {0: pygame.image.load("tile1.png"),
                     1: pygame.image.load("tile2.png")
                     }
                    )
  
  tilemap.generate_random() # Generate a random tilemap
  
  pygame.init()
  DISPLAYSURF = pygame.display.set_mode((tilemap.size))
  # Load the highlighter
  tilemap.ht = pygame.image.load("highlight.png").convert_alpha()
  
  while running:
      for event in pygame.event.get():
          if event.type == QUIT:
              pygame.quit()
              sys.exit()
  
          # Draw the tilemap
          tilemap.draw(DISPLAYSURF, pygame.mouse.get_pos())
  
      pygame.display.update()
  </code></pre>
  
  <p>If you need more explanation feel free to ask!</p>