How do i make the movement function for the player count only once so that they can be executed one by one only once?

I have a game currently which has a player and some obstacles, I want to navigate this player with some commands, but the problem is that the command get executed but the rectangle does not get updated so I have to do some weird while logic with break and a global variable, because otherwise the player would jump. Is there a way to make the functions only count once and maybe loop with for and while normally? this is my code so far:

int border = 20;
int sqsize = 96;
int i = 0;

ArrayList<Player> Players = new ArrayList<Player>();
ArrayList<Obstacle> Obstacles = new ArrayList<Obstacle>();

void setup() {
  size(1000, 1000);
  players.add(new player(9, 0));
  obstacles.add(new obstacle(4, 0));
  obstacles.add(new obstacle(4, 1));
  obstacles.add(new obstacle(4, 2));
  obstacles.add(new obstacle(4, 3));
  obstacles.add(new obstacle(4, 4));
  obstacles.add(new obstacle(4, 5));
}
void draw() {
  background(#767C7C);
  for (int l = 0; l < 10; l++) {
    for (int w = 0; w < 10; w++) {
      fill(#F6F9EF);
      stroke(#BABAB6);
      strokeWeight(0.5);
      rect(border + l*sqsize, border + w*sqsize, sqsize, sqsize);
    }
  }
  Players.get(0).draw();

  for ( int i = Obstacles.size()-1; i>= 0; i--) {
    Obstacles.get(i).draw();
  }
  for ( int i = Obstacles.size()-1; i>= 0; i--) {
    Obstacles.get(i).ask();
  }

  while (i < 5) {
    Players.get(0).left();
    Players.get(0).down();
    break;
  }
  i++;
}


class Player {
  int x, y;
  player(int ix, int iy) {
    x = border + ix*sqsize;
    y = border + iy*sqsize;
  }

  void draw() {
    fill(255, 0, 0);
    rect(x, y, sqsize, sqsize);
  }

  void right() {

    if (x < 1000-2*sqsize) {
      x = x + sqsize;
    }
  }
  void left() {
    if (x > 20) {
      x = x - sqsize;
    }
  }
  void up() {

    if (y > 20+sqsize) {
      y = y + sqsize;
    }
  }
  void down() {
    if (y < 1000-2*sqsize) {
      y = y + sqsize;
    }
  }

  void destroy() {
    textSize(64);
    text("Game Over", 500-2*sqsize, 500-sqsize/2); 
    noLoop();
  }
  int[] request() {
    int[] pos = {x, y};

    return pos;
  }
}


class obstacle {
  int x, y;
  obstacle(int ix, int iy) {
    x = border + ix*sqsize;
    y = border + iy*sqsize;
  }

  void draw() {
    fill(255, 0, 0);
    rect(x, y, sqsize, sqsize);
  }

  void right() {

    if (x < 1000-2*sqsize) {
      x = x + sqsize;
    }
  }
  void left() {
    if (x > 20) {
      x = x - sqsize;
    }
  }
  void up() {

    if (y > 20+sqsize) {
      y = y + sqsize;
    }
  }
  void down() {
    if (y < 1000-2*sqsize) {
      y = y + sqsize;
    }
  }

  void ask() {

    for ( int i = Players.size()-1; i>= 0; i--) {
      int[] check = Players.get(i).request();
      if (/*dist(check[0]+sqsize, check[1]+sqsize, x, y) == 0 || dist(check[0]-sqsize, check[1]-sqsize, x, y) == 0 ||*/ dist(check[0], check[1], x, y) == 0) {

        players.get(i).destroy();
      }
    }
  }
}

Is there a way also to detect which side of the player has touched the obstacle.

thank you very much (Iā€™m a newbie).

1 Like

Hi,

The function links() and unten() does not exist for the player class, could you provide the code?

Also if you take a look at the code conventions for the Java programming language, this is the usage for class names :

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

So use class Player and class Obstacle :wink:

2 Likes

obstacle class is just a copy of player class except it can destroy player

sorry for not changing
links = left
rechts = right
unten = down
oben = up

I also changed the names :grinning:

this is how far I have come till now:

int border = 20;
int sqsize = 96;
int i = 0;
//boolean state = true;
int count = -1;


// go to line 67

ArrayList<Player> Players = new ArrayList<Player>();
ArrayList<Obstacle> Obstacles = new ArrayList<Obstacle>();

void setup() {
  size(1000, 1000);
  Players.add(new player(9, 0));
  Obstacles.add(new Obstacle(4, 0));
  Obstacles.add(new Obstacle(4, 1));
  Obstacles.add(new Obstacle(4, 2));
  Obstacles.add(new Obstacle(4, 3));
  Obstacles.add(new Obstacle(4, 4));
  Obstacles.add(new Obstacle(4, 5));
}
void draw() {
  background(#767C7C);
  for (int l = 0; l < 10; l++) {
    for (int w = 0; w < 10; w++) {
      fill(#F6F9EF);
      stroke(#BABAB6);
      strokeWeight(0.5);
      rect(border + l*sqsize, border + w*sqsize, sqsize, sqsize);
    }
  }
  Players.get(0).draw();

  for ( int i = Obstacles.size()-1; i>= 0; i--) {
    Obstacles.get(i).draw();
  }
  for ( int i = Obstacles.size()-1; i>= 0; i--) {
    Obstacles.get(i).ask();
  }


  Players.get(0).links();//left
  Players.get(0).links();//left
}


class Player {
  boolean[] run = new boolean[4];
  boolean[] perma = new boolean[100];
  int x, y;
  boolean[] set = new boolean[100];
  Track tracker = new Track();
  Player(int ix, int iy) {
    x = border + ix*sqsize;
    y = border + iy*sqsize;
    for (int i = 0; i < 4; i++) {
      run[i] = false;
      perma[i] = false;
    }
  }

  void draw() {
    fill(255, 0, 0);
    rect(x, y, sqsize, sqsize);
  }
// start of the testing part
  void rechts() { //right
      int I = 0; // it keeps on updating because of this need it to stay constant
      if(!set[I]){
         I = tracker.tell();
      }
      
        if (x < 1000-2*sqsize && tracker.ask(I)) { // I is the "id" for the "instance"
          x = x + sqsize;
          count++;
        }
  }
  // this will be in every operation (I need to copy paste it)
// end of the testing part  
  
  void links() {
        if (x > 20 ) {
          x = x - sqsize;
          count++;
        }
  }

  void oben() {
        if (y > 20+sqsize ) {
          y = y + sqsize;
          count++;
        }
  }

  void unten() {
        if (y < 1000-2*sqsize) {
          y = y - sqsize;
          count++;
          println(count);
        }
  }
  void destroy() {
    textSize(64);
    text("Game Over", 500-2*sqsize, 500-sqsize/2); 
    noLoop();
  }

  int[] request() {
    int[] pos = {x, y};

    return pos;
  }
  
  class Track {
    int[][] state = new int[100][];
    boolean s2 = false;
    
    Track(){}
    
    int tell(){
      int[] var = {1,0};// 1st place is for set and 2nd is for permanent stop.
      count++;
      set[count] = true;
      state[count] = var;  
      return count;
      
    }
    
    boolean ask(int i){
      if(!s2 && state[i][2] != 1){
        
        return true;
        
      }else{
        
        return false;
      }
      
      
    }
    
  }
}






class Obstacle {
  int x, y;
  Obstacle(int ix, int iy) {
    x = border + ix*sqsize;
    y = border + iy*sqsize;
  }

  void draw() {
    fill(255, 0, 0);
    rect(x, y, sqsize, sqsize);
  }

  void rechts() {
    state = false;
    boolean run = false;
    if (run) {
      run = false;
    }
    if (x < 1000-2*sqsize) {
      x = x + sqsize;
    }
  }
  void links() {
    if (x > 20) {
      x = x - sqsize;
    }
  }
  void oben() {

    if (y > 20+sqsize) {
      y = y + sqsize;
    }
  }
  void unten() {
    if (y < 1000-2*sqsize) {
      y = y + sqsize;
    }
  }

  void ask() {

    for ( int i = Players.size()-1; i>= 0; i--) {
      int[] check = Players.get(i).request();
      if (/*dist(check[0]+sqsize, check[1]+sqsize, x, y) == 0 || dist(check[0]-sqsize, check[1]-sqsize, x, y) == 0 ||*/ dist(check[0], check[1], x, y) == 0) {

        Players.get(i).destroy();
      }
    }
  }
}

the names

links = left
rechts = right
oben = up
unten = down

This is how I solved my question ( I used cases):

int border = 20;
int sqsize = 96;
int n = 0;
boolean f = true;
long lastTime = 0;
int count = 0;
int[] list = new int[100];

ArrayList<Player> Players = new ArrayList<Player>();
ArrayList<Obstacle> Obstacles = new ArrayList<Obstacle>();

void setup() {
  size(1000, 1000);
  Players.add(new Player(9, 0));
  Obstacles.add(new Obstacle(4, 0));
  Obstacles.add(new Obstacle(4, 1));
  Obstacles.add(new Obstacle(4, 2));
  Obstacles.add(new Obstacle(2, 3));
  Obstacles.add(new Obstacle(4, 4));
  Obstacles.add(new Obstacle(4, 5));
  lastTime = millis();
}
void draw() {
  background(#767C7C);
  for (int l = 0; l < 10; l++) {
    for (int w = 0; w < 10; w++) {
      fill(#F6F9EF);
      stroke(#BABAB6);
      strokeWeight(0.5);
      rect(border + l*sqsize, border + w*sqsize, sqsize, sqsize);
    }
  }

  /*Players.get(0).left();
   Players.get(0).down();*/

  if (f) {

    count = 0;
    list = new int[50];
    player();
    f = false;
  }

  Players.get(0).draw();

  for ( int i = Obstacles.size()-1; i>= 0; i--) {
    Obstacles.get(i).draw();
  }
  for ( int i = Obstacles.size()-1; i>= 0; i--) {
    Obstacles.get(i).ask();
  }
}


class Player {
  int x, y;
  int posCase = 0;

  Player(int ix, int iy) {
    x = border + ix*sqsize;
    y = border + iy*sqsize;
  }

  void draw() {
    while (n < count) {

      if ( millis() - lastTime > 600 ) {

        posCase = list[n];
        println(n);

        if (posCase == 5) {
          noLoop();
        }


        switch(posCase) {

        case 1 :
          if (x < 1000-2*sqsize) {
            x = x + sqsize;
          }
          break;


        case 2 :
          if (x > 20) {
            x = x - sqsize;
          }
          break; 


        case 3 :
          if (y > 20+sqsize) {
            y = y + sqsize;
          }
          break;


        case 4 :
          if (y < 1000-2*sqsize) {
            y = y + sqsize;
          }
          break;
        }
        n++;

        if ( n == count) {
          println("yes");
          f = true;
        }


        lastTime = millis();
      }

      break;
    }
    fill(255, 0, 0);
    rect(x, y, sqsize, sqsize);
  }

  void right() {
    list[count] = 1;
    count++;

    /*f (x < 1000-2*sqsize) {
     x = x + sqsize;
     redraw();
     }*/
  }
  void left() {
    list[count] = 2;
    count++;
    /*if (x > 20) {
     x = x - sqsize;
     redraw();
     }*/
  }
  void up() {
    list[count] = 3;
    count++;
    /*if (y > 20+sqsize) {
     y = y + sqsize;
     redraw();
     }*/
  }
  void down() {
    list[count] = 4;
    count++;
    /*if (y < 1000-2*sqsize) {
     y = y + sqsize;
     redraw();*/
  }
  void ende() {

    list[count] = 5;
    count++;
  }
  void destroy() {
    textSize(64);
    text("Game Over", 500-2*sqsize, 500-sqsize/2); 
    noLoop();
  }
  int[] request() {
    int[] pos = {x, y};

    return pos;
  }
}


class Obstacle {
  int x, y;
  int posCase = 0;

  Obstacle(int ix, int iy) {
    x = border + ix*sqsize;
    y = border + iy*sqsize;
  }

  void draw() {
    while (n < count) {

      if ( millis() - lastTime > 600 ) {

        posCase = list[n];
        println(n);

        if (posCase == 5) {
          noLoop();
        }


        switch(posCase) {

        case 1 :
          if (x < 1000-2*sqsize) {
            x = x + sqsize;
          }
          break;


        case 2 :
          if (x > 20) {
            x = x - sqsize;
          }
          break; 


        case 3 :
          if (y > 20+sqsize) {
            y = y + sqsize;
          }
          break;


        case 4 :
          if (y < 1000-2*sqsize) {
            y = y + sqsize;
          }
          break;
        }
        n++;

        if ( n == count) {
          println("yes");
          f = true;
        }


        lastTime = millis();
      }

      break;
    }
    fill(255, 0, 0);
    rect(x, y, sqsize, sqsize);
  }

  void right() {
    list[count] = 1;
    count++;

    /*f (x < 1000-2*sqsize) {
     x = x + sqsize;
     redraw();
     }*/
  }
  void left() {
    list[count] = 2;
    count++;
    /*if (x > 20) {
     x = x - sqsize;
     redraw();
     }*/
  }
  void up() {
    list[count] = 3;
    count++;
    /*if (y > 20+sqsize) {
     y = y + sqsize;
     redraw();
     }*/
  }
  void down() {
    list[count] = 4;
    count++;
    /*if (y < 1000-2*sqsize) {
     y = y + sqsize;
     redraw();*/
  }
  void ende() {

    list[count] = 5;
    count++;
  }
  void ask() {

    for ( int i = Players.size()-1; i>= 0; i--) {
      int[] check = Players.get(i).request();
      if ( dist(check[0], check[1], x, y) == 0) {

        Players.get(i).destroy();
      }
    }
  }
  
}
2 Likes