How do I check if pressed on a text?

This is how I draw the text:

# Full code on https://github.com/Zwengel/bunny-badgers
height = pygame.display.Info().current_h
font = pygame.font.Font("freesansbold.ttf", 24)
spText = "Single Player"
sp = font.render(spText, True, (0,0,0))
spRect = sp.get_rect()
spRect.centerx = screen.get_rect().centerx
spRect.centery = height/12*5
screen.blit(sp, spRect)
  • \$\begingroup\$ I don't know anything about pygame but normally you would get click coordinates and check if they are inside text bounding box. \$\endgroup\$ Commented Aug 27, 2015 at 14:32
  • \$\begingroup\$ @nikoliazekter Yeah, I can get the mouse position with pygame.mouse.get_pos() and store it to a list called mpos with mpos = pygame.mouse.get_pos(). \$\endgroup\$
    – wb9688
    Commented Aug 28, 2015 at 5:25

1 Answer 1


You can use pygame.Rect.collidepoint() to get that the text was clicked on. The documentation on the function is here.

Here is an example of how you can use the function to your desired effect:

mpos = pygame.mouse.get_pos() # Get mouse position
if spRect.collidepoint(mpos): # Check if position is in the rect
    # Code for what you wish the click to do

This code will check if you clicked on the desired rect object, and execute whatever code your heart desires.

I hope this helps. (:


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .