Python Forum
Sprite on mouse-button hold
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sprite on mouse-button hold
#3
(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
[Image: xLggV6d]

However, when I move to another box or release the mouse button, the highlights do not go away.
[Image: hXDTkcJ]

I believe the problem is in the game loop and/or the class code.
Reply


Messages In This Thread
Sprite on mouse-button hold - by Clunk_Head - Oct-07-2019, 02:12 AM
RE: Sprite on mouse-button hold - by Windspar - Oct-07-2019, 11:34 AM
RE: Sprite on mouse-button hold - by Clunk_Head - Oct-08-2019, 04:31 AM
RE: Sprite on mouse-button hold - by Windspar - Oct-09-2019, 12:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,636 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,189 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 3,307 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,635 May-15-2019, 12:52 PM
Last Post: pfaber11
  [PyGame] Need Help With Sprite ghost0fkarma 2 3,318 Jan-09-2018, 02:14 PM
Last Post: ghost0fkarma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020