External Exam Download Resources Web Applications Games Recycle Bin

Detect mouse click on a card

mouse click on a card.py

import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

two = pygame.image.load("2h.gif")

while not done:
  
  screen.blit(two, (150, 100)) #(x/left,y/top)
  
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True
    if event.type == pygame.MOUSEBUTTONDOWN:
      mouse_pos = pygame.mouse.get_pos()
      if two.get_rect(topleft=(150,100)).collidepoint(mouse_pos):
        print("you clicked on the image of the two")  
  
  pygame.display.flip()
  
pygame.quit()

put image file + python file into same folder: