3

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)

Olivier
  • 33
  • 4

1 Answers1

4

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.

  • 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. – Olivier Oct 03 '12 at 03:39
  • 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. –  Oct 03 '12 at 03:45