1

I have tried to get the variable 'clicks' by 1 every time the red sprite is clicked. Then I need the variable to update so that I can print out the updated number of clicks.

What i have tried:

import pygame

pygame.init()

WIDTH = 600
HEIGHT = 600

window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Printing")
window.fill((0, 0, 0))
rectangle = pygame.draw.rect(window, [255, 0, 0], [50, 50, 90, 90], 0)
pygame.display.flip()

allsprites = pygame.sprite.Group()
allsprites.add(rectangle)

clicks = 0

for event in pygame.event.get():
    if event.type == pygame.BUTTON_LEFT:
        pos = pygame.mouse.get_pos()
        clicked_sprites = [rectangle for rectangle in allsprites if rectangle.rect.collidepoint(pos)]
        clicks += 1
print(clicks)

# loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.quit()

Problems:

allsprites.add(rectangle)

TypeError: unhashable type: 'pygame.Rect'

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

See How can I add objects to a "pygame.sprite.Group()"?. You cannot add rectangle to a pygame.sprite.Group(). You have to crate a pygame.sprite.Sprite object.

class RectSprite(pygame.sprite.Sprite):
    def __init__(self, rect):
        super().__init__() 
        self.rect = pygame.Rect(rect)
        self.image = pygame.Surface(self.rect.size)
        self.image.fill((255, 0, 0))

See Pygame mouse clicking detection. You have check if the event key is pygame.MOUSEBUTTONDOWN and the button is BUTTON_LEFT. Use the pos attribute to get the position of the mouse curser. Increment clicks only if you find a collision:

if event.type == pygame.MOUSEBUTTONDOWN:
    if event.button == pygame.BUTTON_LEFT:
        clicked_sprites = [rectangle for rectangle in allsprites if rectangle.rect.collidepoint(event.pos)]
        if clicked_sprites:
            clicks += 1

You have to do all that in the application loop. Complete example:

import pygame

pygame.init()

WIDTH = 600
HEIGHT = 600

window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Printing")

class RectSprite(pygame.sprite.Sprite):
    def __init__(self, rect):
        super().__init__() 
        self.rect = pygame.Rect(rect)
        self.image = pygame.Surface(self.rect.size)
        self.image.fill((255, 0, 0))

allsprites = pygame.sprite.Group()
rectangle = RectSprite((50, 50, 90, 90))
allsprites.add(rectangle)

clicks = 0

# loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == pygame.BUTTON_LEFT:
                clicked_sprites = [rectangle for rectangle in allsprites if rectangle.rect.collidepoint(event.pos)]
                if clicked_sprites:
                    clicks += 1
                    print(clicks)

    window.fill((0, 0, 0))
    allsprites.draw(window)
    pygame.display.flip()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174