We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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)
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:
Well, define a
defwhich checks out mouseX & mouseY coords. are within your rect().
Then invoke that
deffrom 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: