3
\$\begingroup\$

I want to be able to pop up something when the user hovers over particular words that are on a label in pygame.

Can anyone give me a good (or even bad) idea on how to implement this (doesn't have to be pygame specific)? Do I have to put each word in a different label and place them one after the other?

(what I'm calling a label is the result of font.render)

\$\endgroup\$
  • \$\begingroup\$ How are you drawing this label? With Font.render? \$\endgroup\$ – user14497 Oct 3 '12 at 2:58
  • \$\begingroup\$ Yeah sorry, what I'm calling a label is the result of font.render \$\endgroup\$ – Olivier Oct 3 '12 at 3:17
4
\$\begingroup\$

When you use Surface.blit(), a rectangle is returned which you can associate with your label. You could consider keeping a list of these, or a list of tuples of the returned Rect and the text or label callback. In your main loop, you can iterate over this list and call Rect.collidepoint(pygame.mouse.get_pos()) to find out if the mouse is in the current iteration step's Rect.

\$\endgroup\$
  • \$\begingroup\$ Thanks! This is much nicer than what I was about to try. Part of the problem is I want to find which words within a sentence are being hovered over, but I will blit them all separately so I know which is which. \$\endgroup\$ – Olivier Oct 3 '12 at 3:39
  • \$\begingroup\$ Yeah, you don't need to hang on to the surface created by Font.render(), you just need the Rect that the blit operation returns, so this shouldn't add too much overhead. Alternatively, you can take a look at Font.metrics, but I think having to parse the list returned from that would be more work than it was worth. \$\endgroup\$ – user14497 Oct 3 '12 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.