python中的按钮功能,self.clicked

时间:2018-08-27 09:18:05

标签: python pygame

在编写按钮功能之前,self.clicked不会在MOUSEBUTTONUP之后保持其值,是否有更好的方法或方法来保持self.clicked的值?

代码:

def check_clicked(self,event):
    if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.clicked = True
    elif event.type == pg.MOUSEBUTTONUP:
            if self.clicked == True:
                return True
            else:
                return False

2 个答案:

答案 0 :(得分:0)

您的缩进已关闭,尽管这会引发错误;

def check_clicked(self,event):
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
        if self.rect.collidepoint(event.pos):
            self.clicked = True
elif event.type == pg.MOUSEBUTTONUP: #<<<<< Change indentation here
        if self.clicked == True:
            return True
        else:
            return False

假设您的缩进正确,我认为问题在于您实际上并没有定义self.clicked,只是检查它是否正确。

尝试以下操作;

elif event.type == pg.MOUSEBUTTONUP:
    self.clicked = True
    if self.clicked == True:
        return True
    else:
        return False

尽管在定义self.clicked之前检查def check_clicked(self,event): if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: if self.rect.collidepoint(event.pos): self.clicked = True elif event.type == pg.MOUSEBUTTONUP: self.clicked = True return True 是否为真是多余的。只需使用;

Regex.Matches( "Your String" , System.Environment.NewLine).Count ;

答案 1 :(得分:0)

点对点-您甚至可能要检查UP事件的碰撞点,以确保用户想要点击或他/她改变了主意。其次,您可以使用“ self.down”变量来跟踪按钮是否已按下,并仅在看到UP事件时将其设置为“ clicked”。

def check_clicked(self,event):
    if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            self.clicked = False
            self.down = False
            if self.rect.collidepoint(event.pos):
                self.down = True
    elif event.type == pg.MOUSEBUTTONUP:
            if self.down and self.rect.collidepoint(event.pos):
                self.clicked = True
            else:
                self.clicked = False
            self.down = False

    return self.clicked