# Having trouble making more than 1 clickable object

I am a student trying to figure out how to make multiple clickable objects in pygame.

I was able to make 1 working clickable object (dx,dy) work, but when I tried to make another clickable object (qx,qy), every time I run my code and click on where the object should be, nothing happens.

I don't know what I am doing wrong, so it would be great if someone could give me some advice.

import pygame,sys
from pygame.locals import *
display = pygame.display.set_mode([1000,600])
pygame.init()
clock = pygame.time.Clock()
dx,dy=105,470
qx,qy=750,465

while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse = pygame.mouse.get_pos()
if mouse[0]in range (dx,dy) and mouse[1] in range (dx,dy+50):
print("Hello love.")
elif mouse[0] in range (qx,qy+50) and mouse[1] in range (qx,qy+50):
print("No")

pygame.font.init()
fontcolor = pygame.Color("white")
myfont = pygame.font.SysFont("Comic Sans MS",35,True,False)
label1 = myfont.render("Draw Card",1,(200,200,200))
d = display.blit(label1, (dx,dy))
label2 = myfont.render("Quite",1,(200,200,200))
q = display.blit(label2, (qx,qy))
pygame.display.flip()

• I don't really understand why even your first clickable object works, since you seem to be mixing up X and Y components in your checks. – Tyyppi_77 May 25 '18 at 16:41

Your ranges make no sense. range(qx,qy+50) and range(qx,qy+50) are empty because the first argument is greater than the second. The dx, dy range checks also don't work correctly.

You have to check if mouse[0] is between dx and dx+50 and if mouse[1] is between dy and dy+50 and the same for qx, qy.

if mouse[0] in range(dx, dx+50) and mouse[1] in range(dy, dy+50):
print("Hello love.")
elif mouse[0] in range(qx, qx+50) and mouse[1] in range(qy, qy+50):
print("No")


Note that the size of the clickable area will only be 50*50 px and you should better figure out how big the text surfaces are instead of just adding 50 to dx, etc..

It would be even better to use pygame.Rects. You can create the rects with the get_rect method of the text surfaces and then use the collidepoint method for the collision detection and just blit the surfaces at the corresponding rects.

Here's a minimal, complete example:

import sys
import pygame

pygame.init()
display = pygame.display.set_mode([1000,600])
clock = pygame.time.Clock()
myfont = pygame.font.SysFont("Comic Sans MS",35,True,False)
fontcolor = pygame.Color("white")
label1 = myfont.render("Draw Card",1,(200,200,200))
label2 = myfont.render("Quite",1,(200,200,200))
label1_rect = label1.get_rect(topleft=(105, 470))
label2_rect = label2.get_rect(topleft=(750, 465))

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if label1_rect.collidepoint(event.pos):  # event.pos is the mouse position.
print("Hello love.")
elif label2_rect.collidepoint(event.pos):
print("No")

display.fill((30, 30, 30))
d = display.blit(label1, label1_rect)
q = display.blit(label2, label2_rect)
pygame.display.flip()
clock.tick(30)