I have a scaled image in my window in pygame. I need a way to find the position of the mouse over the image. The normal .pos function gives me the coordinates in relation to the whole window and not the image.
For example:
The window's (0,0) is different than the image's (0,0).
Is there a way to treat the image like its own window so I can return the coordinates of the cursor inside of the image?
You have to subtract the top left position of the image from the mouse position. e.g.:
image = pygame.image.load('image_file.jpg')
image_pos = (100, 100)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
image_mx = mouse_pos[0] - image_pos[0]
image_my = mouse_pos[1] - image_pos[1]
print(image_mx, image_my)
# [...]
screen.blit(image, image_pos)