Oct-08-2019, 04:31 AM
(This post was last modified: Oct-08-2019, 04:32 AM by Clunk_Head.)
(Oct-07-2019, 11:34 AM)Windspar Wrote: 2. You should not kill image and recreate.
Thank you, your example was very helpful. I was able to solve all of my problems, save one. I need the sprites to go away when the mouse moves off of them. I think this means that I do need to kill and recreate them elsewhere.
I provided a simplified example. Here is the code that I corrected using your example:
(There is a whole lot more, but this is the important part)
class hlight(pygame.sprite.Sprite): def __init__(self, pos, size): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface(size, flags=pygame.SRCALPHA) color = pygame.Color(*HLIGHT) color.a = 2 self.image.fill(color) self.rect = self.image.get_rect() self.rect.x = pos[0] self.rect.y = pos[1] highlights = pygame.sprite.Group() def clear_highlights(): for highlight in highlights: highlight.kill() def highlight_intersection(selected_cell): clear_highlights() x = (selected_cell[0] - 2) * cell + offset y1 = offset y2 = offset + (selected_cell[1] - 1) * cell height = y2 + cell - offset width = (max_factor - selected_cell[0] + 2) * cell # Vertical highlight highlights.add(hlight((x, y1), (cell, height))) # Horizontal highlight highlights.add(hlight((x, y2), (width, cell))) selected_cell = None while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif pygame.mouse.get_pressed()[0]: new_cell = get_cell(pygame.mouse.get_pos()) if new_cell and new_cell != selected_cell: selected_cell = new_cell clear_highlights() highlights.update() highlights.draw(main_surface) pygame.display.update() if selected_cell: print(selected_cell) highlight_intersection(selected_cell) elif event.type == pygame.MOUSEBUTTONUP: print("Up") clear_highlights() highlights.update() highlights.draw(main_surface) pygame.display.update()This image shows that the alpha is now working correctly
However, when I move to another box or release the mouse button, the highlights do not go away.
I believe the problem is in the game loop and/or the class code.