0

I'm working on creating an aim trainer using pygame. Here's the code that is supposed to check when I hit the target:

    if event.type == pygame.MOUSEBUTTONDOWN:
        mx,my = pygame.mouse.get_pos()
        print(mx, my)

        if x <= mx <= (x + targetwidth):
            if y <= my <= (y +targetheight):
                targetcount = 0
                del target
                existingTarget = False
                hit +=1
                print("you hit a target!")
                pygame.display.update()
                break

x and y is the postion of the target(randomly generated as soon as a new one is needed)

But even if I click onto the targets, nothing happens. How do I handle clicks on my pygame sprites correctly? Thanks in advance.

Qiu YU
  • 511
  • 4
  • 19
Torax
  • 13
  • 2
  • So even your `print(mx, my)` shows nothing? Then maybe it would be worth adding the code that encompasses this one as well. – Pac0 Jul 31 '20 at 12:41
  • 1
    Note that something like `del target' does not delete anything from the screen; just in case you think it does. – sloth Jul 31 '20 at 12:57
  • You can do it by calculating the distance between the target and the mouse click. Let's say that `mx` and `my` are click coordinates and `tx` and `ty` are the center coordinates of the target. Then you can use simple math: ```import math dist1 = pow(mx-tx, 2) dist2 = pow(my-ty, 2) distance = sqrt(dist1 + dist2)``` – Andrej Jul 31 '20 at 13:15
  • Are you sure your target is actually being drawn at `(x, y)` ? Could `(x, y)` be changing *after* the draw? – Kingsley Jul 31 '20 at 20:09
  • "x and y is the postion of the target" Did you check that these have the values you expect? Did you check that the values of `mx` and `my` are what you expect? Did you check that the values of `targetwidth` and `targetheight` are what you expect? – Karl Knechtel Jul 31 '20 at 20:46

0 Answers0