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'