Def中的mouse mouseClicked()不能正常工作,如何解决?

时间:2019-07-16 21:12:44

标签: python processing

我正在做一个有趣的dnd应用程序,char类是每个字符的意思,但是我似乎无法上班

我尝试注释掉部分代码,但是 init 函数似乎有问题

这是我的代码:

global mouseY
global x
x = 0

chars =[]



def setup():
    global tsize 
    tsize = 50    


def draw():
    global x
    if x == 0:
        def mouseon(x,y,xs,ys):
            return(mouseX <= xs and mouseX >= x and mouseY <= ys and mouseY >= y)
        class char:
            def __init__(self, name, HP, img, CLASS, SIZE, id):
                self.xp = 0
                self.yp = 0
                self.SIZE = SIZE
                self.name = name
                self.HP = HP
                self.img = img
                self.CLASS = CLASS
                self.id = id
                self.Hover = False
                chars.append(self)

        dog = char("john", 100, "x.png", "archer", 50, 0)
        print("l") 
        x = 2

    else:
        fill(255)
        background(255)
        stroke(0)
        strokeWeight(1)
        for i in range(height/50):
            for j in range(width/50):
                rect(i*50,j*50,49.5,49.5)
        for i in chars:
            def mouseClicked():
                    if mouseon(i.xp,i.yp,i.xp+i.SIZE,i.yp+i.SIZE):
                        i.Hover = True
                        print('h')
                    else:
                        print("n")
                        i.Hover = False
            def mouseReleased():
                    print("o")
                    i.Hover = False
            if i.Hover:
                fill(0)
                rect(i.xp+1, i.yp+1, i.xp+i.SIZE+1, i.yp+i.SIZE+1)
            fill(0,255,0)
            strokeWeight(0)
            rect(i.xp, i.yp, i.xp+i.SIZE, i.yp+i.SIZE)


预期结果: 当我按下鼠标时,我应该在控制台上打印h或n,而在释放它时,我应该得到o

实际结果: 没事

编辑: 要澄清,
抽奖每帧调用一次
首先设置一次
mouseClick单击鼠标时单击
和mouse释放鼠标时释放

1 个答案:

答案 0 :(得分:0)

好的,所以我不得不将鼠标单击并释放的功能放在底部,并将for循环放入其中,但这解决了我的问题

更新的代码是

global mouseX
global mouseY
global x
x = 0

chars =[]


def setup():
    global tsize 
    tsize = 50    


def draw():
    global x
    if x == 0:
        global mouseon
        def mouseon(x,y,xs,ys):
            return(mouseX <= xs and mouseX >= x and mouseY <= ys and mouseY >= y)
        class char:
            def __init__(self, name, HP, img, CLASS, SIZE, id):
                self.xp = 0
                self.yp = 0
                self.SIZE = SIZE
                self.name = name
                self.HP = HP
                self.img = img
                self.CLASS = CLASS
                self.id = id
                self.Hover = False
                chars.append(self)

        dog = char("john", 100, "x.png", "archer", 50, 0)
        print("l") 
        x = 2

    else:
        fill(255)
        background(255)
        stroke(0)
        strokeWeight(1)
        for i in range(height/50):
            for j in range(width/50):
                rect(i*50,j*50,49.5,49.5)
        for i in chars:
            if i.Hover:
                fill(0)
                rect(i.xp+1, i.yp+1, i.xp+i.SIZE+1, i.yp+i.SIZE+1)
            fill(0,255,0)
            strokeWeight(0)
            rect(i.xp, i.yp, i.xp+i.SIZE, i.yp+i.SIZE)

def mouseClicked():
    for i in chars:
        if mouseon(i.xp,i.yp,i.xp+i.SIZE,i.yp+i.SIZE):
            i.Hover = True
            print('h')
        else:
            print("n")
            i.Hover = False
def mouseReleased():
    for i in chars:
            print("o")
            i.Hover = False