Using the Grid in a pacman based game

So I as I understand how to load a grid file from a text file I come about to ask how do I make the player stop against these barriers properly ?

Tagged:

Answers

  • You want the player to ignore the barriers? Well, okay.

    Each frame, Pacman tries to move. He does this by saving his current position, using his direction of movement to update his current position, then checking to see if this new position - after having applies the movement - is not inside a square that is a barrier. If he is now in a barrier, his position reverts back to the saved position. If he's not in a barrier, the new position persists.

    You just need to remove the check for being in the barrier. That way he will always move.

  • ... Just reread your question and I totally misunderstood you. You want the Pacman to OBEY the barriers. Okay.

    Each frame, Pacman tries to move. He does this by saving his current position, using his direction of movement to update his current position, then checking to see if this new position - after having applies the movement - is not inside a square that is a barrier. If he is now in a barrier, his position reverts back to the saved position. If he's not in a barrier, the new position persists.

    This way a barrier stops Pacman from moving.

  • The code will look like this:

    class Pacman {
      // ...
      void move(){
        float px = x, py = y;
        x+=dx[direction];
        y+=dy[direction];
        if( onBarrier() ){
          x = px;
          y = py;
        }
      }
      boolean onBarrier(){
        // Check your grid. Is the position (x,y) on a barrier?
      }
      // ...
    }
    
  • From here: https://forum.processing.org/two/discussion/26345/best-way-to-make-world-borders-for-a-game

    This really didn't need to be a new Question. Try to keep things together - it gets confusing otherwise

  • @koogs Will do from now on.

  • edited March 2018

    Alright so I played around with the stuff you said @TfGuy44 but can't get around it here's some code which I did out. I know I am wrong but I can't pinpoint it myself so any possible solution ?

        int rows=0;//Initals for grid
        int columns=0;
        int[][] grid;
        Pacman player;
        void setup()
        {
          size(600, 600);  
          String[] loadedText=loadStrings("grid.txt");
          columns=loadedText[1].length();//Length of one line in the text file
          rows=loadedText.length;//number of lines
          grid=new int[rows][columns];
          player=new Pacman();
          //println(columns);
          // println(rows);
          for (int i=0; i<rows; i++)
          {
    
            for (int j=0; j<rows; j++)
            {
              grid[i][j]=loadedText[i].charAt(j);
            }
          }
        }
    
    void draw()
    {
      background(0, 0, 0);
      for (int i=0; i<rows; i++)
      {
        for (int j=0; j<columns; j++)
        {
          if (grid[i][j]=='1')//Draw the barriers
          {
            float sz=5;
            fill(0, 255, 0);
            rect(i*sz, j*sz, sz, sz);
          }
          if (grid[i][j]=='0')//Draw the movement place for the player 
          {
            float rx=5;
            fill(255, 255, 0);
            rect(i*rx, j*rx, rx, rx);
          }
        }
      }
      player.move();
      player.onBarrier();
    }
    //Pacman class
    float x=width/2, y=height/2; 
    float dx[]={2};
    float dy[]={2};
    int direction;
    boolean run;
    class Pacman
    {
      Pacman()
      {
        run=true;
        direction=0;
        x=width/2;
        y=height/2;
      }
      void move()
      {
        float px=x, py=y;
        if (key==CODED)
        {
          if (keyCode==LEFT)
          {
            x-=dx[direction];
          } else if (keyCode==RIGHT)
          {
            x+=dx[direction];
          } else if (keyCode==UP)
          {
            y-=dy[direction];
          } else if (keyCode==DOWN)
          {
            y+=dy[direction];
          }
        }
        if (onBarrier())
        {
          x=px;
          y=py;
        }
        fill(255, 0, 0);
        rect(x, y, 5, 5);
        onBarrier();
      }
      boolean onBarrier()
      {
        boolean isitrue=false;
        for (int i=0; i<rows; i++)
        {
          for (int j=0; j<columns; j++)
          {
            if (grid[i][j]=='1')
            {
              if (grid[i][j]==x&&grid[i][j]==y)
              {
                isitrue=true;
              }
            }
          }
        }
        return isitrue;
      }
    }
    

    (Sorry for so much code but it gives a good idea of what I am trying to do)

  • You should describe what happens falsely now and what you want to happen instead.

    Which line numbers are involved and are potential suspects?

  • Answer ✓

    Your onBarrier() function doesn't do what you want. And you are calling it in several places where its result is just not used. You should also put the variables for the Pacman class inside the class. Here's a fixed version:

    int rows=0;//Initals for grid
    int columns=0;
    int[][] grid;
    Pacman player;
    
    void setup()
    {
      size(600, 600);  
      String[] loadedText=loadStrings("grid.txt");
      columns=loadedText[1].length();//Length of one line in the text file
      rows=loadedText.length;//number of lines
      grid=new int[rows][columns];
      player=new Pacman();
      //println(columns);
      // println(rows);
      for (int i=0; i<rows; i++)
      {
    
        for (int j=0; j<rows; j++)
        {
          grid[i][j]=loadedText[i].charAt(j);
        }
      }
    }
    
    void draw()
    {
      background(0, 0, 0);
      for (int i=0; i<rows; i++)
      {
        for (int j=0; j<columns; j++)
        {
          if (grid[i][j]=='1')//Draw the barriers
          {
            float sz=5;
            fill(0, 255, 0);
            rect(i*sz, j*sz, sz, sz);
          }
          if (grid[i][j]=='0')//Draw the movement place for the player 
          {
            float rx=5;
            fill(255, 255, 0);
            rect(i*rx, j*rx, rx, rx);
          }
        }
      }
      player.move();
    }
    
    class Pacman
    {
      float x=width/2, y=height/2; 
      float dx[]={2};
      float dy[]={2};
      int direction;
      boolean run;
      Pacman()
      {
        run=true;
        direction=0;
        x=width/2;
        y=height/2;
      }
      void move()
      {
        float px=x, py=y;
        if (key==CODED)
        {
          if (keyCode==LEFT)
          {
            x-=dx[direction];
          } else if (keyCode==RIGHT)
          {
            x+=dx[direction];
          } else if (keyCode==UP)
          {
            y-=dy[direction];
          } else if (keyCode==DOWN)
          {
            y+=dy[direction];
          }
        }
        if (onBarrier())
        {
          x=px;
          y=py;
        }
        fill(255, 0, 0);
        rect(x, y, 5, 5);
      }
      boolean onBarrier()
      {
        int sz = 5;
        if ( int(x/sz) >= 0 && int(x/sz) < rows && int(y/sz) >= 0 && int(y/sz) < columns ) { 
          return( grid[int(x/sz)][int(y/sz)] == '1' );
        } else {
          return(false);
        }
      }
    }
    

    See also:

    https://forum.processing.org/two/discussion/11156/pac-man-game-not-completed-asking-for-assistance

Sign In or Register to comment.