Question about Functions, Booleans

Hey Processing Forum,

I am working on a simple code that is supposed to look at where a user clicked and return a True or False for a function. Later on I want to use an IF statement to determine if my function has returned True or False and act accordingly.

astroid_spawn = True
rndXpos = 100
rndYpos = 100
grow = 30
strScore = "0"
score = 0
def inside_astroid(x, y, ast_x, ast_y, ast_size):
    # ast_size - Diameter of the asteroid
    x = mouseX
    y = mouseY
    fill(255, 0, 0)
    ellipse(ast_x, ast_y, ast_size, ast_size)
    if ((x > (ast_x - (ast_size/2))) and (x < (ast_x + (ast_size/2))))and((y > (ast_y - (ast_size/2)))and(y < (ast_y + (ast_size/2)))):
        return True
        
    else:
        return False

def setup():
    size (400, 400)
    background(0, 0, 0)
    return
    
def draw():
    global rndXpos, rndYpos, grow, strScore, astroid_spawn
    inside_astroid(mouseX, mouseY, 300, 300, grow)

def mouseClicked():
    global rndXpos, rndYpos, grow, strScore, score

    if inside_astroid(mouseX, mouseY, rndXpos, rndYpos, grow) == True:
        print("This is Working")
        
    else:
        return

The Problem I am having, is that in mouseClicked the

    if inside_astroid(mouseX, mouseY, rndXpos, rndYpos, grow) == True:

not only returns true, but it also draws an ellipse on the canvas. (Something I don’t want)
All I want is to check if the function has returned true. Is there a way to check if a function has returned true or did I setup def inside_astroid(x, y, ast_x, ast_y, ast_size): incorrectly?

1 Like

You are drawing the ellipse in this function. If you are not wanting to draw it, then remove these methods.

Also, since you are checking if you are in a circle … you may want to use the dist() method.

ex… return (dist(mouseX,mouseY,pos.x,pos.y) <= diameter/2);

https://processing.org/reference/dist_.html

1 Like

Gotcha, so there isn’t a way to avoid calling all of the things in the function when using it in an IF statement? I was hoping I could just ‘check’ if it was true without calling all of the things within it.

Generally speaking, you should always name your functions so it makes sense of what it does.

Naming a function “inside_astroid” that both checks and draws the astroid is not a good idea. It makes more sense to have 2 separate functions, one for drawing and one for checking if the mouse is inside.

If you want to go even further, have a look at object-oriented programing. This way you could have an astroid object defining the position, speed and so on. And some function linked to that object for drawing it, moving it, checking if the mouse is inside…

2 Likes

Yes, you can use if/else to invoke certain functions depending on state of logic.

You said you did not want the ellipse drawn, so removing that call in the function solves that.

To check if mouse is hovering asteroid you want a function checkIfInsideAsteroid() that returns true when the mouse is positioned inside that object.

so in your function you can just say … (I am not sure how to declare func in python so this example is java)

void checkIfInsideAsteroid() {
  return (dist(mouseX,mouseY,pos.x,pos.y) <= diameter/2);
}

then you can say somewhere else …

if (checkIfInsideAsteroid()) //do something;

If I have a return value and it draw an ellipse will this code not also draw an ellipse? I know its not great to get my function to do two things at once, I am just curious about checking if True.

if (checkIfInsideAsteroid()) //do something;

Will this ONLY check if True/False?

This conditional is checking if checkIfInsideAsteroid() is true.

the do something is where your code goes …

if (checkIfInsideAsteroid()) {
  pushStyle();
  fill(255,0,0);
  ellipse(pos.x,pos.y,diameter,diameter);
  popStyle();
}
1 Like