Open In App

Mouse Clicks on Sprites in PyGame

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The interactiveness of your game can be significantly increased by using Pygame to respond to mouse clicks on sprites. You may develop unique sprite classes that manage mouse events and react to mouse clicks with the aid of Pygame’s sprite module. This article will teach you how to use Pygame to make clickable sprites by monitoring mouse events and looking for mouse-sprite collisions using Python. You may add mouse click capabilities to your Pygame by following the examples given below.

Concepts related to the topic

  • Pygame sprites: Pygame provides a sprite module to help you create and manage game objects. You can create a sprite by extending the pygame.sprite.Sprite class and overriding its update() method to handle mouse events.
  • Pygame mouse events: Pygame provides several mouse events that you can listen to, including MOUSEBUTTONUP, MOUSEBUTTONDOWN, and MOUSEMOTION.
  • Pygame collision detection: Pygame provides a collidepoint() method to check if a point is inside a rect. You can use this method to check if the mouse position is inside a sprite’s rect when a mouse event occurs.

Responding to mouse clicks on a sprite that changes its color when clicked

Create a ClickableSprite class that extends pygame.sprite.Sprite. Override the update() method of the ClickableSprite class to handle mouse events. In the update() method, listen for MOUSEBUTTONUP events and use the collidepoint() method to check if the mouse position is inside the sprite’s rect. If the mouse position is inside the sprite’s rect, call the sprite’s callback function. Create a sprite object and add it to a Pygame sprite group. In the game loop, call the update() method of the sprite group and draw the sprites to the screen

Python3




import pygame
 
 
class ClickableSprite(pygame.sprite.Sprite):
    def __init__(self, image, x, y, callback):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.callback = callback
 
    def update(self, events):
        for event in events:
            if event.type == pygame.MOUSEBUTTONUP:
                if self.rect.collidepoint(event.pos):
                    self.callback()
 
 
def on_click():
    color = (255, 0, 0) if sprite.image.get_at(
        (0, 0)) != (255, 0, 0) else (0, 255, 0)
    sprite.image.fill(color)
 
 
pygame.init()
screen = pygame.display.set_mode((400, 300))
 
sprite = ClickableSprite(pygame.Surface((100, 100)), 50, 50, on_click)
group = pygame.sprite.GroupSingle(sprite)
 
running = True
while running:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False
 
    group.update(events)
    screen.fill((255, 255, 255))
    group.draw(screen)
    pygame.display.update()
 
pygame.quit()


Output:

 

Toggling the visibility of a sprite when it is clicked

We have specified self.visible = True, which is the only change from the code before. We set sprite.visible = not sprite.visible as the default in the ClickableSprite class and the on_click function, which means that when we click the mouse button once, it is False, and when we click it again, it is True.

Python3




import pygame
 
 
class ClickableSprite(pygame.sprite.Sprite):
    def __init__(self, image, x, y, callback):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.callback = callback
        self.visible = True
 
    def update(self, events):
        for event in events:
            if event.type == pygame.MOUSEBUTTONUP:
                if self.rect.collidepoint(event.pos):
                    self.callback()
 
 
def on_click():
    sprite.visible = not sprite.visible
 
 
pygame.init()
screen = pygame.display.set_mode((400, 300))
 
sprite = ClickableSprite(pygame.Surface((100, 100)), 50, 50, on_click)
group = pygame.sprite.GroupSingle(sprite)
 
running = True
while running:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False
 
    group.update(events)
    screen.fill((255, 255, 255))
    if sprite.visible:
        group.draw(screen)
    pygame.display.update()
 
pygame.quit()


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads