Platform game: take away a life

Hello, me again. i am still making my game and was wondering if someone could help me. i am trying to make it when the bottom platform touches the falling rectangles it will take away a life (three lives) but i don’t know how to do this. Im not asking someone to do my entire game i just done understand what to do

heres my code;

float xmovement ;
float whereIsIt ;
float one = -60; 
float two = -150 ; 
int Xposition;
float three = -110; 
float four = -300 ;
float five = -130 ; 
float six = -200 ;
float seven = -450; 
float eight = -200;
float nine = -2000;
// the othe one that take for ever to fall 
float ten = -4000;
float eleven = -4000;
float twelve = -4000;
float thirteen = -4000;

void setup() { 
  size (370, 500) ;
  background (255) ;
  xmovement = -20 ;

void draw () { 

  frameRate (100) ;
  fill (0) ;
  noStroke() ;
  rectMode (CENTER) ;
  rect (185, 250, 350, 480) ;
  fill (255) ;
  rect (mouseX, 460, 35, 10) ;

  // falling obstackles 
  noStroke() ;
  rect (Xposition, one, 9, 23) ;
  rect (120, two, 9, 23) ;
  rect (100, three, 9, 23) ;
  rect (270, four, 9, 23) ;
  rect (200, five, 9, 23) ;
  rect (90, six, 9, 23) ;
  rect (330, seven, 9, 23) ;
  rect (40, eight, 9, 23) ;
  rect (160, nine, 9, 23) ;
  rect (230, ten, 9, 23) ;
  rect (130, eleven, 9, 23) ;
  rect (310, twelve, 9, 23) ;
  rect (170, thirteen, 9, 23) ;

  // logic 
  xmovement = xmovement + random(1, 6) ;
  one = one + random (2, 7) ; 
  two = two + 2 ;
  three  = three + 4.5;
  four = four + 1;
  five = five + 5 ;
  six = six + 3.5;
  seven = seven + 5;
  eight = eight + 4; 
  nine = nine + 6;
  ten = ten + 6;
  eleven = eleven + 5 ;
  twelve = twelve + 6 ;
  thirteen = thirteen + 5;

  //logic that probably wont work becuase i such a failure 
  if (one > 500 )
    one = 0 ;

  if (two > 500 )
    two = 0 ;

  if (three > 500 )
    three = -20 ;

  if (four > 500 )
    four = -400 ;

  if (five > 500 )
    five = -100 ;

  if (six > 500 )
    six = -300 ;

  if (seven > 500 )
    seven = -300 ;

  if (eight > 500 )
    eight = -1000 ;

  if (nine > 500 )
    nine = -3000 ;

  if (ten > 500 )
    ten = -4000 ;

  if (eleven > 500 )
    eleven = -4000 ;

  if (twelve > 500 )
    twelve = -4000 ;

  if (thirteen > 500 )
    thirteen = -4000 ;


i have heard that it goes something along the lines of
if (two<mouseX) {
score = -1 ;

Your code is very WET. WET stands for Write Everything Twice. Basically, you are repeating yourself a lot.

You should try to write DRY code. DRY stands for Don’t Repeat Yourself.

Consider this:

if (one > 500 )
one = 0 ;

if (two > 500 )
two = 0 ;

if (three > 500 )
three = -20 ;

if (four > 500 )
four = -400 ;

if (five > 500 )
five = -100 ;

if (six > 500 )
six = -300 ;

if (seven > 500 )
seven = -300 ;

Do you see a pattern here? Has it occurred to you that there might be a better way?

There IS a better way. The better way is to use an ARRAY.

An array is like a list of numbers, instead of just one number (like a normal variable). But wait, if there is more than one number in an array, how do you access them all? Easy: you specify an index into the list of numbers.

int[] list_of_numbers = { 0, 10, 40 };
println( list_of_numbers[1] );
list_of_numbers[1] = 90
println( list_of_numbers[1] );
println( list_of_numbers[2] );

See how that works?

Once you have arrays of variables, it is much easier to write loops for them!

float[] start_y_positions = { -60, -150, -110, -300, -130, -200, -450, -200, -2000, -4000, -4000, -4000, -4000 };
float[] y_positions = new float[13];
for ( int i = 0; i < y_positions.length; i++) {
  if ( y_positions[i] > 500 ) {
    y_positions[i] = start_y_positions[i];
1 Like

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.


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 :smiley:

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.

//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
 //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!