# Pygame polygon click detection

Here is my problem: I am creating a game and I have a board made out of polygons (hexagons). I want to be able to click on the different tiles but I can't find a simple method. I understood how to detect a click on basic surfaces such as rectangle or pictures, but not with more complex shapes like hexagons. Is it possible to do so?

I first searched for a Pygame function close to this but I didn't find any. Then I searched for the sprites, and I think it is possible, but very long. And the last way I found was to simply detect the color. However I don't want to change the color of the tiles.

How can I detect a click on a polygon? (Hexagons specifically)

• Do you have a regular tiling grid? If so, you can just apply some math to your clicked point to round it to the nearest hexagonal cell, without doing polygon tests on every displayed tile sprite. If you want help concocting such a function, you should show us your game's perspective, alignment of the hexagon grid, and the coordinate systems you use for screen space and tile space. – DMGregory Apr 16 '20 at 8:44

I am currently using PyGame to build a small RPG game project, and I can personally attest that there is no built-in way to do this, at least nothing that I could find in the documentation or forums. Which is obviously very annoying and took me a long time to figure out.

The way I solved it was by:

1. Having a game loop that detects all hardware inputs from the mouse or keyboard:

for event in pygame.event.get():


2. Checking if a particular event is a mouse click event:

if event.type == pygame.MOUSEBUTTONUP:


3. Getting the position on screen where the mouse clicked:

pygame.mouse.get_pos()


4. Checking if anything with a special "clickable" attribute is on the screen at that position, and doing appropriate actions. For example, if I wanted to check for a particular object to see if the cursor was over that object at the moment of the click, I would do:

if clickableObject.collidepoint(mousePosition):


Hopefully this helps!

Edit: I reread your question and am now realizing you wanted to know specifically about polygon detection, rather than general shape detection. Apologies if I was not answering your specific question. You may want to use something like Box2D for anything more complicated than a rectangular collision area, but most polygons should just be a matter of doing some more math.

First, I want to thank you for your answer. I tried a method using the first 3 steps you gave, it worked (approximately) but it was not so optimized. I couldn't figur how to create a clickableObject using polygons. Then I discovered the masks, and using a mask on a polygon picture worked with the .collidepoint(pos). I hope it will work for my 70 polygons on the screen, but it worked just fine for one...

In my case I was using hexagons and I admit it is a very regular shape, I just find it strange that we could not use the "normal method".

Thanks again, and I hope I will one day play your RPG Pygame game.

~Definity