0

Ok so I am working on a home screen for my game and I have an image that acts as a button. So when you click some where on the image the image changes then imports the next part of the game. But what I want to do as well is when the mouse hovers over the image it will play a sound. But how do I get it to detect when the mouse is being hovered over the button image?

Here is a copy of my code for the home screen. ps. I figured out my problem now you can seem my code here.(This is all the code so far for my home screen. Any was thanks to any one who can help me make it detect when the mouse in hovering over the image.)

import pygame, sys, random
import time

B_images = ["startbutton.png", "startbuttonpush.png"]

class BClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("startbutton.png")
        self.rect = self.image.get_rect()
        self.rect.center = [310, 500]




def animate():
    screen.fill([0,0,0])
    screen.blit(B.image,B.rect)
    pygame.display.flip()



pygame.init()
x = y = 0
pos = pygame.mouse.get_pos()
pygame.display.set_caption("Skier")
screen = pygame.display.set_mode([640,640])
B = BClass()
font = pygame.font.Font(None, 50)
ButtonSound = pygame.mixer.Sound('ButtonSound.ogg')

while True:
    animate()
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                import End.py
        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            if ( x in range(229,391)) and (y in range(470,530)):
                B.image = pygame.image.load("startbuttonpush.png")
                animate()
                time.sleep(0.1)
                import skier.py
PersianGulf
  • 2,845
  • 6
  • 47
  • 67
user2883997
  • 231
  • 1
  • 3
  • 9
  • 5
    A couple things: 1) You document is not public. 2) I (and others most likely) are wary of downloading unknown files onto my machine. What problems did you encounter posting your code ? – Hunter McMillen Oct 15 '13 at 20:38
  • Please, paste a relevant snippet of your code so that we can help you with your problem. – Trein Oct 15 '13 at 20:51
  • it kept saying it look like you post contains code. and it was telling me to skip a line and indent all code by 4 spaces. I tried to do what it was telling me but it kept giving me the same message – user2883997 Oct 16 '13 at 16:40
  • I fixed my problem you can now see the code here – user2883997 Oct 16 '13 at 16:52

2 Answers2

2

To detect a mouse hover, do the same thing as detecting the mouse click, except do it on the pygame.MOUSEMOTION event. This is called each time a mouse movement is detected.

    if event.type == pygame.MOUSEMOTION:
        x, y = event.pos
        if ( x in range(229,391)) and (y in range(470,530)):
            print "Hovering over image!"

Also note that x in range(229, 391) is super-inefficient, and you should do 229 <= x <= 391 instead. And you should not hard-code those coordinates, eventually.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • Instead of range, you can use [Rect.collidepoint()](http://www.pygame.org/docs/ref/rect.html#Rect.collidepoint) – ninMonkey Oct 16 '13 at 18:39
  • ok so that helps but it also makes it to where every time I move the mouse in side the image it plays the sound and it plays it more than once. What I want it to do is detect when it is on the image and only play the sound once and if I move the mouse when it is still on the image it will not play the sound again until you take the mouse off the image and then put it back on the image. – user2883997 Oct 17 '13 at 20:22
  • @user2883997: you'll need a state variable, `in_image`. If there's a hit and `in_image` is False, then it just moved into the image. If there's a hit and `in_image` is True, then it's still in the image from before. If there's no hit and `in_image` is True, the mouse just hovered out. If there's no hit and `in_image` is False, then the mouse stayed out of the image. do what you want for each case – Claudiu Oct 17 '13 at 20:24
0

I used to do this on my buttons in pygame as well. You should consider this code:

def is_hovering():
    mouse = pygame.mouse.get_pos()
    if button_object.rect.collidepoint(mouse):
        return True
    else:
        return False
LMCuber
  • 113
  • 1
  • 11