First off, please consider both @TfGuy44 and @kfrajer and their responses. Repetitive code is more prone to mistakes, and non-formatted code is more prone to getting a “format your code” reminder
Joke attempts aside, what you want is something that checks for collisions. In psuedo-code, what you’re basically looking for is something like
if(my rectangle hits the bottom of the screen)
{
take away a life
}
Of course this isn’t actual code, but just psuedo-code, as it’s name implies, is just so you can see what it is that you are actually trying to accomplish. So how would we get started with it? Well first, we have to look at how are we going to check if the rectangle hits the bottom of the screen?
I would first look at where your rectangles are. Considering that you have a lot of them, I would very much highly consider that you make a class out of these rectangles. Checking for each one of your fourteen or so rectangles will be too much of a task, and useless effort too.
Start like this:
RectangleObjects[] rects = new RectangleObjects[14];
Where above your void setup(), you make a new class of “RectangleObjects” (a name I just made up) called rects (also another name I made up). I made this into an array, as you can tell by the square brackets after RectangleObjects. I would the declare that there will be 14 of these objects.
I do not know how well versed you are in OOP and class construction, so after this response I will look for a tutorial on that to supplement my response. As of now, hopefully these explanations suffice.
After declaring your class, I would then initialize all of my RectangleObjects. Now, I again do not know if you are versed with class construction, so I will skip this step for now, and go to creating the RectangleObjects class and constructor.
The basic setup of your class would look like this:
class RectangleObjects
{
int rectX;
int rectY;
int rectW;
int rectH ;
public RectangleObjects() //notice how this is the same name as the class
{
//using the "this." keyword, I set the variables of each specific object.
//as of now, I set rectX, and rectY to zero, by DEFAULT, you will change these later
this.rectX = 0;
this.rectY = 0;
this.rectW = 9; //notice I define these because from what I can tell,
this.rectH = 23; //each of your rectangles has the same width and height.
}
//in each of these functions, you can set each variable. For example, if you had a RectangleObject
//called "r" and you wanted to set it's rectX, you would do r.setX(20); for example
public void setX(int x) { this.rectX = x; }
public void setY(int y) { this.rectY = y; }
public void setW(int w) { this.rectW = w; }
public void setH(int h) { this.rectH = h; }
//in these functions, I will now **get** each of the variables. Again like with set, you can specify a
//rectangle object, and get a variable from it. r.getX() will return whatever value that specific
//object for it's rectX variable.
//NOTE: the return values are ints because the variables are ints.
int getX() { return this.rectX; } //notice the "this." keyword getting the specific variable from the object
int getY() { return this.rectY; }
int getW(){ return this.rectW; }
int getH(){ return this.rectH; }
//finally after being able to "get" and "set" your variables, you can also display your rectangle, which //you will only have to do once because with the array you can create multiple objects
void display() //doesn't have to be called display. Just remember what you call it, of course
{
rect(rectX,rectY,rectW,rectH); //add in your colors and all that as well
}
}//end of RectangleObjects class
Now let’s go back to void setup() so we can initialize everything. You would do this by doing a for loop:
for(int i = 0; i< rects.length; i++)
{
rects[i] = new RectangleObjects();
}
Right after doing that, in setup you can also set all your specifics.
rects[0].setX(Xposition);
rects[0].setY(one);
//and so on
You don’t have to set the width and height, because if you remember from our class in the constructor, we did this:
this.rectW = 9; //notice I define these because from what I can tell,
this.rectH = 23; //each of your rectangles has the same width and height.
So by default each rectangle has a rectW and a rectH of 9 and 23, respectively. You can of course change those with your set functions.
After setting all of your variables, the next thing you want to do is actually display all of them. So go into void draw(), and run another loop to display all of the RectangleObjects.
for(int i=0; i<rects.length; i++)
{
rects[i].display(); //remember the display function in the class?
}
Now, you are almost done. Now it is time to check for collisions. In your class, you can add another function called checkForCollisions()
like this:
void checkForCollisions()
{
}
Remember what I said earlier?
if(my rectangle hits the bottom of the screen)
{
take away a life
}
Now let’s make the psuedo code, some actual code. How can we check if the rectangle is at the bottom of the screen? I’m going to assume that you mean the whole rectangle is out of the screen, so wouldn’t that be when rectY is greater than height? By height, I mean, the window’s height? So simply you could check like this:
void checkForCollisions()
{
if(this.rectY > height)
{
lives--; //of course you declare this variable and display it and such
}
}
And where would you call this function? well BEFORE drawing the new frame, so in my void display() function, I would just add this right before everything, like this:
void display() //doesn't have to be called display. Just remember what you call it, of course
{
checkForCollisions();
//add everything else
rect(rectX,rectY,rectW,rectH); //add in your colors and all that as well
}
Now, when you want to specify your xmovement variable as I see you have there, or all of that stuff, you can either add the movement amount as another variable in your class, so you can specify it? But I will see if you can figure out doing that. So far, I pretty much did everything that would have to do with checking for collisions with your RectangleObjects as objects.
Hopefully this helps, and if you have any more questions, I would be glad to help answer them!
EnhancedLoop7