Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)

share|improve this question
    
How are you drawing this label? With Font.render? –  K.G. Oct 3 '12 at 2:58
    
Yeah sorry, what I'm calling a label is the result of font.render –  Olivier Oct 3 '12 at 3:17
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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 3 '12 at 3: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. –  K.G. Oct 3 '12 at 3:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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