How to do an interaction 2

pppppp
edited November 2017 in Questions about Code

its a shotter game you shhoot bala (bullet) and kill soldat (soldier)

ArrayList bales; Bala bala1; PImage img;

void setup() {
  size(800, 450);
  stroke(255);
  fill(255, 255, 255);
  bales = new ArrayList();
  bala1 = new Bala();
  img = loadImage("pa.jpg");
  bales.add( bala1 );
}

void draw() {
  image(img, 0, 0);

  line(400, 450, mouseX, 2 * mouseY);
  strokeWeight(mouseY/10);


  for ( int i = 0; i < bales.size(); i++) {
    bala1  = (Bala)bales.get(i);
    bala1.balesmoviment();
  }
}

class Bala {
  float balapx, balapy;
  float baladx, balady;
  Bala() {//creamos una bala
    balapx = width/2.0;
    balapy = height;
    baladx = map(mouseX, 0, width, -10, 10);
    balady = -10;
  }

  void balesmoviment() {
    balapx += baladx;
    balapy += balady;
    ellipse( balapx, balapy, 2, 2);
  }
}


void mousePressed() {
  if ( bales.size() > 9 ) bales.remove(0);
  bales.add( new Bala() );
}
{
  ArrayList soldats;
  Soldat soldat1;
  {

    void setup() {
      soldats= new ArrayList();
      soldat1 = new Soldat();
      soldats.add (soldat1);
    }
    void draw() {
      for ( int i = 0; i < bales.size(); i++) {
        soldat1  = (Soldat)soldats.get(i);
        soldat1.soldatsmoviment();
      }


      boolean choque( float balaX, float balaY, float balaR, float soldatX, float soldatY, float soldatW, float soldatH ) {
        if (balaX==soldatX)&&(balaY==soldatY)&&(balaR==(soldatW*soldatH)) {//collision of 2 bullets
          choque=true;

          else() {
            choque= false;
          }
        }

        class Soldat { // Here the problem, how to make rectangles that move and avance in the map
          float soldatpx, soldatpy;
          float i;
          Soldat() {//creamos unsoldat
            soldatpx = width+i;
            soldatpy = 0;
          }
          void soldatsmoviment() {
            soldatpx +=i;

            rect( soldatpx, balapy, 10, 20);
          }
        }
      }
    }
  }
}
«1

Answers

  • still not finished, i see...

  • edited November 2017

    Sigh. I said I was done. But I can still run your mess of code through the cleaner without doing your collision function for you. So look at this:

    // ----- Global Variables -----
    
    ArrayList<Bala> bales = new ArrayList();
    ArrayList<Soldat> soldats = new ArrayList();
    //PImage img;
    
    
    
    
    // ----- Main Sketch Functions -----
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      //img = loadImage("pa.jpg");
      // Create initital Soldats
      for( int i = 0; i < 10; i++){
        soldats.add( new Soldat( random(width-40), -30*i ) );
      }
    }
    
    void draw() {
      background(128); 
      //image(img, 0, 0);
      stroke(255);
      line(width/2.0, height, mouseX, mouseY);
      // Remove old Bales.
      for( int i = bales.size()-1; i >= 0; i--){
        if( bales.get(i).px < -20 || bales.get(i).px > width + 20 || bales.get(i).py < -20 ) {
          bales.remove(i);
        }
      }
      // Remove old Soldats
      for( int i = soldats.size()-1; i >= 0; i--){
        if( soldats.get(i).py > height ){
          soldats.remove(i);
        }
      }
      // Create new Soldats.
      while(soldats.size() < 10){
        soldats.add( new Soldat( random(width-40), -30 ) );
      }
      // Move and show all Soldats.
      for ( Soldat s : soldats ) {
        s.move();
        s.show();
      }
      // Move and show all Bales.
      for ( Bala b : bales ) {
        b.move();
        b.show();
      }  
      // Look for collisions.
      for ( int i = bales.size()-1; i >= 0; i-- ) {
        Bala b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j-- ) {
          Soldat s = soldats.get(j);
          if ( choque( b.px, b.py, b.r, s.px, s.py, s.w, s.h ) ){
            // TODO: Make things happen when there is a collision. You should write this!
            /// ...?
    
    
    
          }
        }
      }
    }
    
    void mousePressed() {
      bales.add( new Bala() );
    }
    
    
    
    
    // ----- Collision Check Function -----
    
    boolean choque( float balaX, float balaY, float balaR, float soldatX, float soldatY, float soldatW, float soldatH ){
      // TODO: Fix this. You should write this function!
      /// ...?
    
    
    
      return( false ); // Placeholder - should not always return false!
    }
    
    
    
    
    // ----- Bala Class -----
    
    class Bala {
      float px, py;
      float vx, vy;
      float r;
      Bala() {
        px = width/2.0;
        py = height;
        PVector pv = new PVector(mouseX - px, mouseY - py);
        pv.normalize();
        vx = 5 * pv.x;
        vy = 5 * pv.y;
        r = 10;
      }
      void move() {
        px += vx;
        py += vy;
      }
      void show() {
        fill(255);
        noStroke();
        ellipse( px, py, r, r);
      }
    }
    
    
    
    
    // ----- Soldat Class -----
    
    class Soldat {
      float px, py;
      float w, h;
      Soldat(float ipx, float ipy) {
        px = ipx;
        py = ipy;
        w = 40;
        h = 30;
      }
      void move() {
        py++;
      }
      void show() {
        fill(200, 0, 0);
        stroke(0);
        rect(px, py, w, h);
      }
    }
    

    This is missing two things. First, the body of the collision function. To fill it in, check out this link:

    http://www.jeffreythompson.org/collision-detection/circle-rect.php

    Second, it is missing what should happen when there is a collision. Maybe you remove the Soldat s and make the Bala b bounce? Or remove it too? Whatever. That's up to you.

    I also commented out your background image because I don't have the image. Leave this commented out so it is easier for us to help you.

  • for ( Bala b : bales ) {

    this function? ok

  • this is the image

  • for ( Bala b : bales ) {

    this function? ok

    What are you asking? What function? That's a loop. It loops over each Bala in the bales ArrayList.

    Your image is not the issue here. How are you getting along with the collisions?

  • i'm doing, you will hallucinate

  • what it means this loop?

      for( int i = bales.size()-1; i >= 0; i--){
        if( bales.get(i).px < -20 || bales.get(i).px > width + 20 || bales.get(i).py < -20 ) {
          bales.remove(i);
        }
    
  • // ----- Global Variables -----
    
    ArrayList<Bala> bales = new ArrayList();
    ArrayList<Soldat> soldats = new ArrayList();
    //PImage img;
    
    // ----- Main Sketch Functions -----
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
    
    
      // Create initital Soldats
      for ( int i = 0; i < 10; i++) {
        soldats.add( new Soldat( random(width-40), -30*i ) );
      }
    }
    
    void draw() {
      background(128); 
      //image(img, 0, 0);
      stroke(255);
      line(width/2.0, height, mouseX, mouseY);
    
      // Remove old Bales.
      for ( int i = bales.size()-1; i >= 0; i--) {
        if ( bales.get(i).px < -20 || bales.get(i).px > width + 20 || bales.get(i).py < -20 ) {
          bales.remove(i);
        }
      }
      // Remove old Soldats
      for ( int i = soldats.size()-1; i >= 0; i--) {
        if ( soldats.get(i).py > height ) {
          soldats.remove(i);
        }
      }
      // Create new Soldats.
      while (soldats.size() < 10) {
        soldats.add( new Soldat( random(width-40), -30 ) );
      }
    
      for ( int i = bales.size()-1; i >= 0; i--) {
        Bala  b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j--) {
          Soldat s = soldats.get(j);
          if (dist(s.px, s.py, b.px, b.py) <= 20)
          {
            // matem soldat i hauria d´apareixer mort
            soldats.remove(j);
            bales.remove(i);
          }
        }
      } 
      // Move and show all Soldats.
      for ( Soldat s : soldats ) {
        s.move();
        s.show();
      }
      // Move and show all Bales.
      for ( Bala b : bales ) {//el loop actua sobre cada Bala en les bales de l'array list
        b.move();
        b.show();
      }  
    
      // Look for collisions.
      for ( int i = bales.size()-1; i >= 0; i-- ) {
        Bala b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j-- ) {
          Soldat s = soldats.get(j);
        }
      }
    }
    
    void mousePressed() {
      bales.add( new Bala() );
    }
    
    // ----- Bala Class -----
    
    class Bala {
      float px, py;
      float vx, vy;
      float r;
      Bala() {
        px = width/2.0;
        py = height;
        PVector pv = new PVector(mouseX - px, mouseY - py);
        pv.normalize();
        vx = 5 * pv.x;
        vy = 5 * pv.y;
        r = 10;
      }
      void move() {
        px += vx;
        py += vy;
      }
      void show() {
        fill(255);
        noStroke();
        ellipse( px, py, r, r);
      }
    }
    
    // ----- Soldat Class -----
    
    class Soldat {
      float px, py;
      float w, h;
      Soldat(float ipx, float ipy) {
        px = ipx;
        py = ipy;
        w = 40;
        h = 30;
      }
      void move() {
        py++;
      }
      void show() {
        fill(200, 0, 0);
        stroke(0);
        rect(px, py, w, h);
      }
    }
    
  • I will write in the top of the code this //developed with the help of the TfGuy of the processing forum.

  • // ----- Global Variables -----

    Why in the first page of the game to click start, don't work the stat 1 and 0. If YOU CLICK on the zone that I write, draw de void game.

    //developed with the help of the TfGuy of the processing forum. ArrayList bales = new ArrayList(); ArrayList soldats = new ArrayList(); PImage img;
    PImage pantinici; int joc; int estat;

    // ----- Main Sketch Functions -----
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
    
      img = loadImage("pau.jpg");
      pantinici= loadImage("PORTADA.jpg");
    
      // Create initital Soldats
      for ( int i = 0; i < 10; i++) {
        soldats.add( new Soldat( random(width-40), -30*i ) );
      }
    }
    
    void draw() {
    
    
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
    }
    
    //estat= 0;
    
    void pantinici() {
    
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          estat = 1;
          println("posX");
        }
      }
     // if (mousePressed  && (mouseButton==LEFT)) {
     //   if (mouseX >= 370 && mouseX <= 390 && mouseY >=420  && mouseY <= 410) {
     //     estat = 1;
      //  }
     // }
    }
    
    void joc()
    {
      background(128); 
      //image(img, 0, 0);
      stroke(255);
      line(width/2.0, height, mouseX, mouseY);
    
      // Remove old Bales.
      for ( int i = bales.size()-1; i >= 0; i--) {
        if ( bales.get(i).px < -20 || bales.get(i).px > width + 20 || bales.get(i).py < -20 ) {
          bales.remove(i);
        }
      }
      // Remove old Soldats
      for ( int i = soldats.size()-1; i >= 0; i--) {
        if ( soldats.get(i).py > height ) {
          soldats.remove(i);
        }
      }
      // Create new Soldats.
      while (soldats.size() < 10) {
        soldats.add( new Soldat( random(width-40), -30 ) );
      }
    
      for ( int i = bales.size()-1; i >= 0; i--) {
        Bala  b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j--) {
          Soldat s = soldats.get(j);
          if (dist(s.px, s.py, b.px, b.py) <= 20)
          {
            // matem soldat i hauria d´apareixer mort
            soldats.remove(j);
            bales.remove(i);
          }
        }
      } 
      // Move and show all Soldats.
      for ( Soldat s : soldats ) {
        s.move();
        s.show();
      }
      // Move and show all Bales.
      for ( Bala b : bales ) {//el loop actua sobre cada Bala en les bales de l'array list
        b.move();
        b.show();
      }  
    
      // Look for collisions.
      for ( int i = bales.size()-1; i >= 0; i-- ) {
        Bala b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j-- ) {
          Soldat s = soldats.get(j);
        }
      }
    }
    
    void mousePressed() {
      bales.add( new Bala() );
    }
    
    // ----- Bala Class -----
    
    class Bala {
      float px, py;
      float vx, vy;
      float r;
      Bala() {
        px = width/2.0;
        py = height;
        PVector pv = new PVector(mouseX - px, mouseY - py);
        pv.normalize();
        vx = 5 * pv.x;
        vy = 5 * pv.y;
        r = 10;
      }
      void move() {
        px += vx;
        py += vy;
      }
      void show() {
        fill(255);
        noStroke();
        ellipse( px, py, r, r);
      }
    }
    
    // ----- Soldat Class -----
    
    class Soldat {
      float px, py;
      float w, h;
      Soldat(float ipx, float ipy) {
        px = ipx;
        py = ipy;
        w = 40;
        h = 30;
      }
      void move() {
        py++;
      }
      void show() {
        fill(200, 0, 0);
        stroke(0);
        rect(px, py, w, h);
    
            //for (int i=0; i< soldats.lenth; i ++) {
        // soldats[i]= loadImage("soldat"+i+".png");
    
      }
    }
    
  • jugar is play

  • Alright. One note: The loops on line 95 to 102 aren't doing anything, and can be removed. You have your collision checking elsewhere now.

  • pppppp
    edited December 2017

    and why don' t do the void joc? when I click on play

      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          estat = 1;
          println("posX");
        }
      }
    
  •   if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
    }
    
    //estat= 0;
    
    void pantinici() {
    
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          estat = 1;
          println("posX");
        }
      }
    
  • Sorry,this

  • with out print ln

  • void draw() {
    
    
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
    }
    
    //estat= 0;
    
    void pantinici() {
    
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          estat = 1;
          println("posX");
        }
      }
    
    void joc()
    
  • No one, myself included, has any idea what you are talking about. What are you asking? What code is that? Is that a different sketch?

  • nono, look at your gmail, execute the code, and you will understand, is the first page of the game when you click on play and the game run

  • I personally have accepted ppp as our lord and savior

  • tfguy, is the last question

  • Tf Guy, in the email you have the images to understand what I have talk to you

  • But where I talk to you? The mail is for you to understan what im doing. When you play a game first there is a first page where put play, you click on play and then the game start. I wanna do this.

  • Well, jugar is play, and when i click in the zone of jugar(play), the estat is 1 and then execute void joc (that is the game execute the game.

    void draw() {
    
    
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
    }
    
    //estat= 0;
    
    void pantinici() {
    
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          estat = 1;
    
        }
      }
    
    void joc()
    
  • How I can start a new discussion with this problem? Because this is so long

  • Oh, okay, I think I get what you're trying to do. You want a start screen with a play button, and when you click the play button, then the game starts.

    So you will need a variable to remember which state your sketch is in.

    int state;
    

    Initially, your sketch will be in state #0, the start screen state.

    // in setup():
    state = 0;
    

    Now in draw(), you will need to either do the start screen (if the state is 0) or run the game (if the state is 1).

    // in draw():
    if( state == 0 ){
      draw_start_screen();
    } else if( state == 1 ){
      run_and_draw_game();
    }
    

    Finally, you will need to check if the start button is clicked.

    // in mousepressed():
    if( state == 0 ){
      if( mouse_on_play_button() ){
        state = 1;
      }
    }
    

    If you can explain what you want to do with words first, it is much easier to be able to write the code that does it! Please learn how to describe things better!

  • yes, is this, but i do this and don't work

  • This was I did, first (if the state is 0) draw pantinici and if estate is 1 execute joc(joc means game) so execute the game. And in the mousePressed, i locate the rectangle of the button Play (300,400,400,420)

    void draw() {
    
    
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
    }
    
    
    
    void pantinici() {
    
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          estat = 1;
    
  • i Draw the int and nothing, I don't know

  • For these reason i pass the images in mail, for you to understan the botton for play(I don't know if I'm doing incorrectly the position of the botton play(in spanish jugar)

  • There is a difference between mousePressed (the boolean) and mousePressed() (the function). Use the function.

    int state;
    
    void setup(){
      size(200,200);
      state = 0;
    }
    
    void draw(){
      if( state == 0 ){
        state0();
      } else if( state == 1 ){
        state1();
      }
    }
    
    void state0(){
      background(0);
    }
    
    void state1(){
      background(255,0,0);
    }
    
    void mousePressed(){
      if( state == 0 ){
        state = 1;
      }
    }
    
  • What? but I do this with the exception of the state0 and state1 I write pantinici and joc

  • And in the mousePressed I need to to the bolean because the mouse must clik on the button play

  • The difference is that my code shows the global state variable, and closes the functions. The code snippet you have posted looks like you grabbed a chunk out of the middle of something.

    Please always post complete examples. That way we can copy, paste, and run them easily.

  • I do this int state;

    void setup(){
      size(200,200);
      state = 0;
    }
    
    void draw() {
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
     // if (estat==2) {
       // game over();
      }
    }
    void pantinici() {
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          if (estat==0) {
            estat = 1;
    
  • The example is long // ----- Global Variables ----- //developed with the help of TfGuy

    ArrayList<Bala> bales = new ArrayList();
    ArrayList<Soldat> soldats = new ArrayList();
    PImage img;  
    PImage pantinici;
    int joc;
    int estat;
    int game over;
    
    // ----- Main Sketch Functions -----
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      estat= 0;
      img = loadImage("pau.jpg");
      pantinici= loadImage("PORTADA.jpg");
    
      // Create initital Soldats
      for ( int i = 0; i < 10; i++) {
        soldats.add( new Soldat( random(width-40), -30*i ) );
      }
    }
    
    void draw() {
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
     // if (estat==2) {
       // game over();
      }
    }
    void pantinici() {
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          if (estat==0) {
            estat = 1;                
            println("estat");
          }
        }
        // if (mousePressed  && (mouseButton==LEFT)) {
        //   if (mouseX >= 370 && mouseX <= 390 && mouseY >=420  && mouseY <= 410) {
        //     estat = 1;
        //  }
        // }
      }
    
      void joc()
      {
        background(128); 
        //image(img, 0, 0);
        stroke(255);
        line(width/2.0, height, mouseX, mouseY);
    
        // Remove old Bales.
        for ( int i = bales.size()-1; i >= 0; i--) {
          if ( bales.get(i).px < -20 || bales.get(i).px > width + 20 || bales.get(i).py < -20 ) {
            bales.remove(i);
          }
        }
        // Remove old Soldats
        for ( int i = soldats.size()-1; i >= 0; i--) {
          if ( soldats.get(i).py > height ) {
            soldats.remove(i);
          }
        }
        // Create new Soldats.
        while (soldats.size() < 10) {
          soldats.add( new Soldat( random(width-40), -30 ) );
        }
    
        for ( int i = bales.size()-1; i >= 0; i--) {
          Bala  b = bales.get(i);
          for ( int j = soldats.size()-1; j >= 0; j--) {
            Soldat s = soldats.get(j);
            if (dist(s.px, s.py, b.px, b.py) <= 20)
            {
              // matem soldat i hauria d´apareixer mort
              soldats.remove(j);
              bales.remove(i);
            }
          }
        } 
    
    
        // Look for collisions.
        for ( int i = bales.size()-1; i >= 0; i-- ) {
          Bala b = bales.get(i);
          for ( int j = soldats.size()-1; j >= 0; j-- ) {
            Soldat s = soldats.get(j);
          }
        }
      }
    
      void mousePressed() {
        bales.add( new Bala() );
      }
    
      // ----- Bala Class -----
    
      class Bala {
        float px, py;
        float vx, vy;
        float r;
        Bala() {
          px = width/2.0;
          py = height;
          PVector pv = new PVector(mouseX - px, mouseY - py);
          pv.normalize();
          vx = 5 * pv.x;
          vy = 5 * pv.y;
          r = 10;
        }
        void move() {
          px += vx;
          py += vy;
        }
        void show() {
          fill(255);
          noStroke();
          ellipse( px, py, r, r);
        }
      }
    
      // ----- Soldat Class -----
    
      class Soldat {
        float px, py;
        float w, h;
        Soldat(float ipx, float ipy) {
          px = ipx;
          py = ipy;
          w = 40;
          h = 30;
        }
        void move() {
          py++;
        }
        void show() {
          fill(200, 0, 0);
          stroke(0);
          rect(px, py, w, h);
    
          //for (int i=0; i< soldats.lenth; i ++) {
      // soldats[i]= loadImage("soldat"+i+".png");
    }
    

    }

    //void game over() {

  • Ctrl + t is your friend. It will auto-format your code. That way you can hopefully realize that you are defining your joc() function inside your pantinici() function. You MUST learn to put closing brackets where they belong!

  • camon, there is the last question and you will never see me aagain, please help mee

  • I've already told you what's wrong. You can fix it.

    I don't want to never see you again. I want you to get good at programming and stick around to help other people learn.

  • yes, i help my friends an another people

  • // ----- Global Variables ----- //developed with the help of TfGuy

    ArrayList bales = new ArrayList(); ArrayList soldats = new ArrayList(); PImage img;
    PImage pantinici; int joc; int estat; //int game over;

    // ----- Main Sketch Functions -----
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      estat= 0;
      img = loadImage("pau.jpg");
      pantinici= loadImage("PORTADA.jpg");
    
      // Create initital Soldats
      for ( int i = 0; i < 10; i++) {
        soldats.add( new Soldat( random(width-40), -30*i ) );
      }
    }
    
    void draw() {
      if (estat==0) {
        pantinici();
      }   
      if (estat==1) {
        joc();
      }
      // if (estat==2) {
      // game over();
    }
    
    void pantinici() {
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=440  && mouseY <= 420) {
          if (estat==0) {
            estat = 1;                
            println("estat");
          }
        }
      }
    }
    // if (mousePressed  && (mouseButton==LEFT)) {
    //   if (mouseX >= 370 && mouseX <= 390 && mouseY >=420  && mouseY <= 410) {
    //     estat = 1;
    //  }
    // }
    //}
    
    void joc()
    {
      background(128); 
      //image(img, 0, 0);
      stroke(255);
      line(width/2.0, height, mouseX, mouseY);
    
      // Remove old Bales.
      for ( int i = bales.size()-1; i >= 0; i--) {
        if ( bales.get(i).px < -20 || bales.get(i).px > width + 20 || bales.get(i).py < -20 ) {
          bales.remove(i);
        }
      }
      // Remove old Soldats
      for ( int i = soldats.size()-1; i >= 0; i--) {
        if ( soldats.get(i).py > height ) {
          soldats.remove(i);
        }
      }
      // Create new Soldats.
      while (soldats.size() < 10) {
        soldats.add( new Soldat( random(width-40), -30 ) );
      }
    
      for ( int i = bales.size()-1; i >= 0; i--) {
        Bala  b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j--) {
          Soldat s = soldats.get(j);
          if (dist(s.px, s.py, b.px, b.py) <= 20)
          {
            // matem soldat i hauria d´apareixer mort
            soldats.remove(j);
            bales.remove(i);
          }
        }
      } 
    
    
      // Look for collisions.
      for ( int i = bales.size()-1; i >= 0; i-- ) {
        Bala b = bales.get(i);
        for ( int j = soldats.size()-1; j >= 0; j-- ) {
          Soldat s = soldats.get(j);
        }
      }
    }
    
    void mousePressed() {
      bales.add( new Bala() );
    }
    
    // ----- Bala Class -----
    
    class Bala {
      float px, py;
      float vx, vy;
      float r;
      Bala() {
        px = width/2.0;
        py = height;
        PVector pv = new PVector(mouseX - px, mouseY - py);
        pv.normalize();
        vx = 5 * pv.x;
        vy = 5 * pv.y;
        r = 10;
      }
      void move() {
        px += vx;
        py += vy;
      }
      void show() {
        fill(255);
        noStroke();
        ellipse( px, py, r, r);
      }
    }
    
    // ----- Soldat Class -----
    
    class Soldat {
      float px, py;
      float w, h;
      Soldat(float ipx, float ipy) {
        px = ipx;
        py = ipy;
        w = 40;
        h = 30;
      }
      void move() {
        py++;
      }
      void show() {
        fill(200, 0, 0);
        stroke(0);
        rect(px, py, w, h);
    
        //for (int i=0; i< soldats.lenth; i ++) {
        // soldats[i]= loadImage("soldat"+i+".png");
      }
    }
    
    //void game over() {
    
  • The button and the first image appears, but he don't execute the void game

  • mouseY >=440  && mouseY <= 420
    

    The mouseY variable can never satisfy both of these conditions at the same time. Any number greater than 440 will never also be less than 420.

  • ouuuuu yesyes

  • void pantinici() {
      scale(1);
      image (pantinici, -10, -80);
      if (mousePressed  && (mouseButton==LEFT)) {
        if (mouseX >=370 && mouseX <=400 && mouseY >=420  && mouseY <= 430) {
          if (estat==0) {
            estat = 1;                
            println("estat");
          }
    
  • if (mouseX >=400 && mouseX <=370 && mouseY >=420 && mouseY <= 430) {

This discussion has been closed.