2012-08-07 5 views
4

에서 이미지 마우스를 움직일 감지가?나는 이미지가 파이 게임

button_x = 0 
button_y = 0 
newGameButton = pygame.image.load("images/newGameButton.png").convert_alpha() 
x_len = newGameButton.get_width() 
y_len = newGameButton.get_height() 
mos_x, mos_y = pygame.mouse.get_pos() 
if mos_x>button_x and (mos_x<button_x+x_len): 
    x_inside = True 
else: x_inside = False 
if mos_y>button_y and (mos_y<button_y+y_len): 
    y_inside = True 
else: y_inside = False 
if x_inside and y_inside: 
    #Mouse is hovering over button 
screen.blit(newGameButton, (button_x,button_y)) 

, 더 mouse in pygame에뿐만 아니라 surfaces in pygame으로 읽기 :

답변

11

사용 Surface.get_rect는하는 RectSurface의 경계를 설명 얻을 마우스 커서가이 Rect 안에 있는지 확인하는 .collidepoint()를 사용합니다.


예 :

if newGameButton.get_rect().collidepoint(pygame.mouse.get_pos()): 
    print "mouse is over 'newGameButton'" 
+0

그리고 두 개 이상의 버튼이나 표면이있는 경우, 그것을 당신은 커서를 이동 매번에 대해 검사 할 목록을 확인하십시오. –

2

내가 여기 이렇게 더 파이썬 방법이 확신하지만, 간단한 예입니다.

또한 here과 밀접한 관련이 있습니다.