Question about what a number does in an if statement

for(int i=pacdot.size()-1;i>=0;i--){
     if(abs(pacdots.get(i).x-x)<10&&(abs(pacdots.get(i).y-y)<10)
          pacdots.remove(i);
}

what does the 10 do in the if statement?

Tagged:

Answers

  • pacdots.get(i).x is a coordinate. The second x is a variable. The difference is the distance between the two. abs() is absolute value (it makes negative values positive). So, it checks if the distance between the pacdot and the variable x is less than 10. Same for y.

  • edited June 2015
    PImage title, map;
    int gamemode=0;
    int dotType=0;
    int totalDots;
    int dotsEaten=0;
    int i=0;
    int points=0;
    //height is 1080 width is 1920
    //gamemode 1 is title, 2 is map, 3 is cherries,4 is finished successsfully, 5 is dead
    
    map loadmap=new map();
    newGame game=new newGame(0, 0);
    //write wr=new write(0);
    ArrayList<Pacdot> pacdots;
    pacmanSprite pac=new pacmanSprite(25, 25, 0, 0);
    
    void setup() {
    
      frameRate(30);
      size(displayWidth, displayHeight);
      gamemode=1; //gamemode is started
      pacdots=new ArrayList<Pacdot>();
      for (int y=25; y<height-25; y+=25) {
        int x=25;
        pacdots.add(new Pacdot(x, y));
      }
      for (int x=25; x<width-25; x+=25) { //top bar of circles
        int y=25;
        pacdots.add(new Pacdot(x, y));
      }
      for (int y=25; y<height-25; y+=25) {
        int x=width-25;
        pacdots.add(new Pacdot(x, y));
      }
      for (int x=25+25; x<width-25; x+=25) {
        int y=height-25;
        pacdots.add(new Pacdot(x, y));
      }
      for (int x=25+25; x<width-25; x+=25) {
        int y=height/2;
        pacdots.add(new Pacdot(x, y));
      }
      for (int y=25+25; y<height/2-25; y+=25) {
        int x=width/2;
        pacdots.add(new Pacdot(x, y));
      }
      for (int y=height/2+25; y<height-25; y+=25) {
        int x=width/2;
        pacdots.add(new Pacdot(x, y));
      }
    }
    
    void draw() {
      game.display();
      loadmap.loadMap();
      gamemode();
      if (mousePressed==true) {
        println(mouseX+","+mouseY);
      }
      // wr.setupW();
      //wr.output();
      pac.display();
     pac.eatDot();
    }
    void keyPressed() {
    
      if (key==' '&&gamemode==1) {
        gamemode=2;//gamemode map is started
        println(gamemode);
      } else if ((key=='a'&&gamemode==2)||(key=='A'&&gamemode==2)) {
        gamemode=3;
        println(gamemode);
      }
    }
    
    void gamemode() {
      if (gamemode==1) {
        text("press space to continue", width/2, height/2);
      }
      if (gamemode==2) {
        text("press a to spawn map  ", 15, 15);
        fill(0);
        text("points"+points,100,100);
      }
      if (gamemode==3) { 
      for (int i=0; i<pacdots.size (); i++)
        pacdots.get(i).display(); 
      }
    
    }
    
    
    class map {
      public map() {
      }
      void loadMap() {
        if (gamemode==2) {
          int size=2; //background start
          for (int h = 0; h < width; h += size) {
            for (int v = 0; v < height; v += size) {
              if ((h+v) % 20 ==0) { 
                fill(0,0,150);
              } else {
                fill(0,0,255);
              }
              rect (h, v, size, size);
            }
          }
        } //background end
        //draw the 4 rects that pac man can not go through
        //map is under gamemode stuff
        if(gamemode==3){
          rectMode(CORNERS);
          fill(#C9610C);
          rect(50,50,width/2-25,height/2-25);
          rect(50,height/2+25,width/2-25,height-50);
          rect(width/2+25,50,width-50,height/2-25);
          rect(width/2+25,height/2+25,width-50,height-50);
        }
      }
    }
    
    
    class newGame{
      private int x,y;
      public newGame(int xLocationTitleScreen,int yLocationTitleScreen){
        x=xLocationTitleScreen;
        y=yLocationTitleScreen;
      }
      void display(){
       // title=loadImage("title.png");
        //if(gamemode==1){//if gamemode is started
          //image(title,x,y,width,height);
        }
      }
    }
    
    
    public class Pacdot {
      float x, y;
      public Pacdot(float xpos, float ypos) {
        x=xpos;
        y=ypos;
      }
      void display() {
        if (gamemode==3) {
          fill(#F8FC00);
          ellipseMode(CENTER);
          ellipse(x, y, 15, 15);
        }
      }
    }
    
    
    class pacmanSprite {
      private float x, y, speedx, speedy;
      public pacmanSprite(float xpos, float ypos, float speedXpos, float speedYpos) {
        x=xpos;
        y=ypos;
        speedx=speedXpos;
        speedy=speedYpos;
      }
      void display() {
        println(x+","+y+","+speedx+","+speedy);
        float px=x, py=y;
        if (gamemode==3) {
          fill(0);
          ellipse(x, y, 25, 25);
          x+=speedx;
          y+=speedy;
          if (keyPressed==true) {
            if (key=='w'&&boundary()==false) {
              speedy=-10;
              speedx=0;
            }
            else if (key=='a'&&boundary()==false) {
              speedx=-10;
              speedy=0;
            }
            else if (key=='s'&&boundary()==false) {
              speedy=10;
              speedx=0;
            }
            //if a boundary is ahead get him back on track
            else if (key=='s'&&boundary()==true) {
              speedy=0;
              speedx=10;
              println(boundary());
            }
            else if (key=='d'&&boundary()==false) {
              speedx=10;
              speedy=0;
            }
          }
        }
      }
      void eatDot() {
      for(int i=pacdot.size()-1;i>=0;i--){
     if(abs(pacdots.get(i).x-x)<10&&(abs(pacdots.get(i).y-y)<10)
          pacdots.remove(i);
      }
      }
      boolean boundary() {
        if((y<=25)){
        return false;
        }
        else{
          return true;
        }
      }
    }
    

    what should 10 be?

  • edited June 2015

    pacdots.get(i).x-x

    it would be better readable if you would name the 2nd x as pacmanX (or so)

    do you know what abs() is? Look it up.

    we are talking about eat dot (eatDot()) here: When pacman is close to a dot, the dot gets eaten (it's removed)

    when it's not working try 20 instead of 10 or so

    also remember to hit ctrl-t in processing more often

    ;-)

  • 10 is just the distance for x and for y

    when both distances are smaller than 10, pacman eats the dot

  • @Chrisir can you help me witha new issue?

    class Grid {
      private int wid, heig, sqw;
      private color c;
      public Grid(int widthh, int heightt, int sqwidth, color col){
        wid=widthh;
        heig=heightt;
        sqw=sqwidth;
        c=col;
      }
        void display() {
        for (int q=0; q < height/heig; q += 0) {//y
          for ( int w=0; w < width/wid; w += 0) {//x
          println(height/heig);
            fill(c);
            rect(q, w, sqw, sqw);
            w+=sqw;
          }
          q+=sqw;
        }
      }
    }
    

    i am trying to create a gird of squares the size of wid and heig. Like block wise. How would I do that?

  • Answer ✓
    int cellSize = 16;
    
    void setup() { 
      size(400, 400); 
      background(255);
    } // func
    
    void draw() {
      background(255); 
      for (int a = 0; a < width/cellSize; a++) { 
        for (int b = 0; b < height/cellSize; b++) { 
          stroke(255); 
          fill(0); 
          if (mousePressed && 
            mouseX > a*cellSize && mouseX < a*cellSize + cellSize && 
            mouseY > b*cellSize && mouseY < b*cellSize + cellSize) {
            fill(255, 2, 2);
          } // if 
          rect(a*cellSize, b*cellSize, 
          cellSize, cellSize);
        } // for
      } // for
    } // func
    // 
    
  • edited June 2015

    @Chrisir can you recreate this with variables cellWidth and cellHeight?

  • Answer ✓

    you don't have a clue, uh?

  • Answer ✓
    int cellSizeX = 16;
    int cellSizeY = 10;
    
    void setup() { 
      size(600, 600); 
      background(255);
      stroke(255, 2, 2); 
      fill(0);
    } // func
    
    void draw() {
      background(255); 
      for (int x = 0; x < 400/cellSizeX; x++) { 
        for (int y = 0; y < 400/cellSizeY; y++) { 
          rect(x*cellSizeX+12, y*cellSizeY+19, 
          cellSizeX, cellSizeY);
        } // for
      } // for
    } // func
    // 
    
  • @Chrisir nope, only my first year programming....

  • @Chrisir would you be willing to walk me through a game when I am having troubles? It is due tomorrow... I have been working really hard but, have been struggling...

  • edited June 2015

    okay, @Chrisir my skype is classified

  • Answer ✓

    solved.

Sign In or Register to comment.