Can someone explain me the "colide" function and what is a function?

I saw this function mentioned in a post i was reading, and it got me interested, because right now I’m working on a puzzle game and if you touch any walls you respawn. Well, that is where i want that colide function, and because I’m a noob, can someone please explain me what a function is?

Simple
//first rectangle hit box is Called A
Rect rectA{x,y,width,height}
Rect rectB{x2,y2,width2,hieght2}
Void loop
{
if(arduboy.collide(rectA,rectB))
{
//whatever
}
}
Now it being messy x is an int y too and x2 as well as y2 also please don’t judge my writing😬

1 Like

Thanks alot! But, now, can you explain me what is a function? I’m kinnda confused. And how does it exactly work?

@Pharap explained that in your other post. It if a standalone routine that, when called, returns a value. The function typically has calling parameters as well.

http://www.cprogramming.com/tutorial/lesson4.html

2 Likes

https://www.arduino.cc/en/Reference/FunctionDeclaration

The above is from the Arduino Language Reference
https://www.arduino.cc/en/Reference/HomePage

2 Likes

Sorry to nitpick, but that code won’t actually compile (and it’s not in a code block), so I’ve neatened it up a bit:

// The first rectangle hit box is called rectA
Rect rectA { x, y, width, height };

// The second rectangle hit box is called rectB
Rect rectB { x2, y2, width2, height2 };

void loop
{
  if(arduboy.collide(rectA,rectB))
  {
    // Any code in here runs when rectA and rectB collide
  }
}
1 Like

A function is just a collection of code.

Imagine it like a box with some code written in it. The code performs one specific function (hence the name).

You call up that function whenever you want your program to do that specific thing.

If you find in your code that you keep wanting to do the same thing, you would write a function to do that thing, and then simply call the function every time you wanted that thing to happen.

Functions can have data passed to them and returned from them.

2 Likes