I try to build a game using pygame and have issues with mouse events. Basically even if I click the exactly same place, two different kinds of events will register at different time. For example if I click on monster.rect, if may register as I click another rect, which have no intersection with monster.rect, then if I move my mouse a little bit and move back and re-click on the exact same position and then it registered correctly. Here is my code:
def check_events(screen, monster, buttons, button_status):
"""Check mouse and keyboard evetns"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if monster.rect.collidepoint(pygame.mouse.get_pos()): #If click monster cards
button_status.monster_initial_active() #active monster initial button
print('click')
elif buttons:
for button in buttons:
if button.rect.collidepoint(pygame.mouse.get_pos()):
if button.text == 'Play':
monster_play()
elif button.text == 'Level up':
monster_level_up()
elif button.text == 'Back':
monster_back(button_status)
print(button.rect)
elif event.type == pygame.MOUSEMOTION:
print(pygame.mouse.get_pos())
elif event.type == pygame.MOUSEBUTTONUP:
print('0')
Please help me if you can, thanks! Code in the main loop:
while True:
gf.check_events(screen, monster, buttons, button_status)
gf.update_screen(ai_settings, screen, character_1, character_2, monster, tactic, buttons, button_status)
clock.tick(25)