MousePressed question...

edited August 28 in Python Mode

I would like to find a method so that when you press the mouse button and release it, it will add a score of 1, like the cookie clicker type game. The challenge I have is that when I try this with a rectangle, press the mouse button, the score is continual and the score goes up rapidly each time you press it. Here is the example code:

def setup():
    global score
    size(200, 200)
    fill(0)
    background(255) # white
    textSize(32)
    score=0


def draw():
    global score
    background(255) # white
    text(score,110, 190)
    text("Score:", 10, 190)
    if (mouseX >75) and (mouseX < 125) and (mouseY > 25) and (mouseY < 125):
        fill(255,0,0)
        rect(75,25,50,100)
        if mousePressed :
            fill(1,1,255) 
            rect(75,25,50,100)
            text("pressed", 40, 160)
            score=score+1

    else:
        fill(0)
        rect(75,25,50,100)
Tagged:

Answers

  • edited August 28

    Awesome, thank you, that worked perfectly. Now, if I want to access the moveclicked only under certain circumstances, how do I get that to work? For example, mouseclicked is always being checked to see if it is being clicked - but I only want it to check if the mouse is hovering over the rectangle (cookie in the cookie-clicker game) and then check. I saw that in the reference, I can set an EVENT, but I wasn't sure what that meant. If there was a way to check the mouseclick ONLY if a conditional is met, but I am not sure how to get that to work right now. Here is the reference for the mouseClicked and my code below:

    def mouseClicked(): statements

    def mouseClicked(event): statements

    My code below. If the person clicks anywhere on the screen, it adds a point. I want it to add a point only if it is inside the rectangle AND they press the mouse button:

    def setup():
        global count
        count=0
        size(200, 200)
        fill(0)
        background(255) # white
        textSize(20)
    
    
    def draw():
        background(255) # white
        text("COUNTING :", 10, 190) # this is a label for the count
        text(count, 150, 190) # this is the actual variable for the count
        if (mouseX >75) and (mouseX < 125) and (mouseY > 25) and (mouseY < 125):
            fill(255,0,0)
            rect(75,25,50,100)
        else:
            fill(0)
            rect(75,25,50,100) 
    
    def mouseClicked():
        global count
        fill(1,1,255) 
        rect(75,25,50,100)
        text("pressed", 40, 160)
        count=count+1
    
  • Well, define a def which checks out mouseX & mouseY coords. are within your rect().
    Then invoke that def from within mouseClicked().

  • Ok, great idea. I hadn't thought about that. I tried creating a variable that is click=1. Then I set up a conditional that checked to see if click=1 inside the bounding box BUT I also changed the click=2 if it was outside. It works, but probably not the best method. I will try your idea next:

    def setup():
        global count, click
        count=0
        click=0
        size(200, 200)
        fill(0)
        background(255) # white
        textSize(20)
    
    
    def draw():
        global click, count
        background(255) # white
        text("COUNTING :", 10, 190) # this is a label for the count
        text(count, 150, 190) # this is the actual variable for the count
        if (mouseX >75) and (mouseX < 125) and (mouseY > 25) and (mouseY < 125) :
            fill(255,0,0)
            rect(75,25,50,100)
            if click==1:
                text("pressed", 40, 160)
                count=count+1
                click=2
    
        else:
            fill(0)
            rect(75,25,50,100) 
            click=2
    
    
    def mouseClicked():
        global click, count
        click=1
    
Sign In or Register to comment.