python - How do I change the color of a single rectangle in a grid? -


i've programmed grid, want change color of single rectangle in grid.

x = 5 y = 5  height = 30 width = 50 size = 20 color = (255,255,255) new_color = (255,255,0)  screen.fill((0,0,0))  def draw_grid():     y in range(height):         x in range(width):             rect = pygame.rect(x * (size + 1),y * (size + 1),size,size)             pygame.draw.rect(screen,color,rect)             x += 20             rects.append((rect,color))         y += 20  rects = [] colored_rects = []  while 1:      event in pygame.event.get():         if event.type == quit:             sys.exit()      draw_grid()      if pygame.mouse.get_pressed()[0]:         mouse_pos = pygame.mouse.get_pos()         i,(rect,color) in enumerate(rects):             if rect.collidepoint(mouse_pos):                 rects[i] = (rect,new_color)                 colored_rects.append((rect,new_color))       rect,color in rects:         pygame.draw.rect(screen,color,rect)     rect,new_color in colored_rects:         pygame.draw.rect(screen,new_color,rect)      pygame.display.flip()     clock.tick() 

now want change 1 rectangle when click on it, later must change automatically (for example when there 3 rectangles touching in same color, must become white). i've updated little bit, there still problems. example: have click on rectangle till changes color, , takes time te change color.

one solution store rects color in tuples. if mouse button pressed, iterate on rectangles list, if rectangle collides mouse, create tuple rect , new color , replace tuple @ current index.

import sys import pygame pg   def main():     screen = pg.display.set_mode((640, 480))     clock = pg.time.clock()      height = 30     width = 50     size = 20     color = (255, 255, 255)     new_color = (255, 255, 0)      rectangles = []     y in range(height):         x in range(width):             rect = pg.rect(x * (size+1), y * (size+1), size, size)             # grid list of (rect, color) tuples.             rectangles.append((rect, color))      done = false      while not done:         event in pg.event.get():             if event.type == pg.quit:                 done = true          if pg.mouse.get_pressed()[0]:             mouse_pos = pg.mouse.get_pos()             # enumerate creates tuples of number (the index)             # , rect-color tuple, looks like:             # (0, (<rect(0, 0, 20, 20)>, (255, 255, 255)))             # can unpack them directly in head of loop.             index, (rect, color) in enumerate(rectangles):                 if rect.collidepoint(mouse_pos):                     # create tuple new color , assign it.                     rectangles[index] = (rect, new_color)          screen.fill((30, 30, 30))          # draw rects. can unpack tuples         # again directly in head of loop.         rect, color in rectangles:             pg.draw.rect(screen, color, rect)          pg.display.flip()         clock.tick(30)   if __name__ == '__main__':     pg.init()     main()     pg.quit()     sys.exit() 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -