鼠标X& Y位置为pygame中的get_rect.collidepoint

时间:2016-08-28 14:06:48

标签: python pygame

我正在尝试在PyGame中创建一个click事件,但我遇到了一个错误:

AttributeError: 'NoneType' object has no attribute 'get_rect'

代码:

mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()

if mouse_pressed[0] and play_btn.get_rect().collidepoint(mouse_pos[0],
                                                         mouse_pos[1]):
    print "Mouse clicked play"

按钮......

play_btn = button("PLAY NOW", 210, 150, 300, 80, white, grey)

1 个答案:

答案 0 :(得分:0)

根据提供的信息,button不返回任何内容。它返回None

根据您之前的question,您的功能button如下所示:

def button(btn_txt_img,posx,posy):
    btn_txt_img = pygame.image.load(("/Users/wolf/Desktop/python/img/",btn_txt_img,".png")).convert_alpha()
    btn_txt_img_hover = pygame.image.load(("/Users/wolf/Desktop/python/img/",btn_txt_img,"_hover.png")).convert_alpha()

    if((w_size[0]/3.3)+btn_txt_img.get_rect().width > mouse_pos[0] > (w_size[0]/3.3) and posy+btn_txt_img.get_rect().height > mouse_pos[1] > posy):
        return main_screen.blit(btn_txt_img_hover, [(w_size[0]/3.3),posy,btn_txt_img.get_rect().width,btn_txt_img.get_rect().height])
    else:
        return main_screen.blit(btn_txt_img, [(w_size[0]/3.3),posy,btn_txt_img.get_rect().width,btn_txt_img.get_rect().height])
    pygame.display.update()

Blitting直接在对象上工作,因此返回None。由于您的函数会返回main_screen.blit...返回的内容,因此它还会返回None