0

I'm making a simple "Find the objects" game in pygame. It increases the score by 1 whenever an element is clicked. I am using the coordinates of an element to check if it has been clicked or not. Here's the code:

import pygame
pygame.init()
pygame.mixer.init()
screen_width = 905
screen_height = 390
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
done = False
score = 0
first = pygame.image.load("1st.png").convert()
current_level_no = 1
if current_level_no ==1:
    screen.blit(first, [0, 0])
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            x = event.pos[0]
            y = event.pos[1]
            if current_level_no == 1:
                if x > 766 and x < 787:
                    if y > 311 and y < 339:
                        pygame.draw.ellipse (screen, LIME, [766, 311, 21, 28], 3)
                        score +=1
                if x > 60 and x < 135:
                    if y > 365 and y < 380:
                        pygame.draw.ellipse (screen, LIME, [60, 365, 71, 15], 3)
                        score +=1
                if x > 798 and x < 843:
                    if y > 150 and y < 171:
                        pygame.draw.ellipse (screen, LIME, [798, 150, 45, 25], 3)
                        score +=1
                if x > 61 and x < 102:
                    if y > 115 and y < 141:
                        pygame.draw.ellipse (screen, LIME, [61, 115, 44, 36], 3)
                        score +=1
                if x > 357 and x < 394:
                    if y > 151 and y < 170:
                        pygame.draw.ellipse (screen, LIME, [357, 151, 44, 22], 3)
                        score +=1
                if x > 256 and x < 288:
                    if y > 209 and y < 230:
                        pygame.draw.ellipse (screen, LIME, [256, 209, 37, 25], 3)
                        score +=1
    clock.tick(60)
    pygame.display.flip()
pygame.quit()

I want to know how to detect if the element has already been clicked by the user. Thanks.

  • When you click on an element, set an attribute on it. Then you can check the attribute the next time. – Barmar Jul 20 '21 at 17:57
  • Or you could put the clicked elements in a list, and then check if the element is already in the list. – Barmar Jul 20 '21 at 17:58
  • @Barmar I'm not using sprites to detect whether the element has been clicked or not, just the coordinates of the element, so I can't apply any of the solutions you mentioned. – PianoPianist Jul 20 '21 at 18:00
  • does this help: https://stackoverflow.com/questions/10990137/pygame-mouse-clicking-detection – TheHappyBee Jul 20 '21 at 18:01
  • No it doesn't, I am using the coordinates of the element to check if it has been clicked or not. – PianoPianist Jul 20 '21 at 18:04
  • @PianoPianist Do not use coordinates, but `pygame.Rect` objects and add the clicked rectangles to a list. – Rabbid76 Jul 20 '21 at 18:28

0 Answers0