Continuous movement?

edited June 2017 in Questions about Code

Hi, so I'm making a version of Pacman where you play as the ghosts instead of Pacman, and your goal is to try and catch Pacman. My problem right now is that when you press the arrow keys to move, the ghost will move over 1 cell on the grid, but it won't go all the way to the end. So for example, how it should work is that if you are at the bottom left corner and you press the right arrow key, it should move to the right until it detects a wall. Right now, if I press the right arrow key, it would just move one space over and that's it. Any ideas why this is happening?

Here is some of the code: (I am not sure what is the issue so I just posted a section of the stuff I wrote, but let me know if you need more information/code.)

  void setup() {
    size(448, 576);
    //size(448, 726);
    //surface.setResizable(true);
    background(255);
    mazeimg = loadImage("mazeimg.png");
    image(mazeimg, 0, 0);

  rectColor = color(0);
  rectHighlight = color(51);
  rectX = 0;
  rectY = 0;


    //////////System.out.println(start);
    int cols = 28;
    String[] lines = loadStrings("MainMaze.txt");
    //String[] mazeLines = new String[lines.length];
    maze = new Location[36][28];

    for (int r = 0; r < lines.length; r ++) { 
      for (int c = 0; c < lines[0].length(); c ++) { 
        //////////System.out.println("row: " + r + " col: " + c);
        if (lines[r].charAt(c) == '0') { 
          maze[r][c] = new Wall (r, c); 
        } else {
          maze[r][c] = new EmptySpace(r,c); 
        }
        }
      }
      int x1 = 0, y1 = 0; 
      int c;
      for (int r = 0; r < maze.length; r ++) {
        for (c =  0; c < maze[r].length; c ++) { 
          y1 = (width / 28) * r; //+75;
          x1 = (height / 36) * c; 
          if (!maze[r][c].isValid()) { // if it's wall
            noFill();
            rect (x1, y1, (width/ 28), (height/36));
          } 
          else if (maze[r][c].isValid()){ // if it's an open space
          //////////System.out.println ("x: " + r + " y: " + c);

            fill(#000000); 
            //noStroke(); (add this in later when we're done)
            rect (x1, y1, (width/ 28), (height/36));
            // for tiny dots


          }
        }   
      }

    pac = new Pacman (); 
    locPac = maze[pac.getR()][pac.getC()]; 
    ellipse (pac.xPixel(), pac.yPixel() , 16, 16);

    redd = new RedGhost("red");
    locRed = maze[redd.getR()][redd.getC()]; 
    redd.moveTo(maze[16][12]);
    ellipse ( redd.xPixel() , redd.yPixel(), 16, 16);


    pink = new RedGhost("pink");
    locPink = maze[pink.getR()][pink.getC()]; 
    pink.moveTo(maze[16][15]);
     ellipse (pink.xPixel() , pink.yPixel(), 16, 16);

    blue = new RedGhost("blue");
    locBlue = maze[blue.getR()][blue.getC()]; 
    blue.moveTo(maze[18][12]);
    ellipse ( blue.xPixel() , blue.yPixel(), 16, 16);

    orange = new RedGhost("orange");
    locOrange = maze[orange.getR()][orange.getC()]; 
    orange.moveTo(maze[18][15]);
     ellipse (orange.xPixel() , orange.yPixel(), 16, 16);

     n = redd;

  }

  void draw() {

    //////////System.out.println (n);
     a++; 


    if (called && a == 40) { 
      //System.out.println (true);
      redd.moveRandom(); 
         pink.moveRandom(); 
         blue.moveRandom(); 
         orange.moveRandom();
         called = false;
    } 

    update(mouseX, mouseY);
    if (rectOver) {
    fill(rectHighlight);
     } else {
    fill(rectColor);
  }
  stroke(255);
  rect(rectX, rectY, rectSize, rectSize);


    color col = color(0, 255, 0);
    fill(col);
    noStroke();
    y = locPac.getR() * (width / 28) + 16;
    x = locPac.getC() * (height/ 36) + 8;
      background (0); 



    background(255);
    mazeimg = loadImage("mazeimg.png");
    image(mazeimg, 0, 0);
    for (int r = 0; r < maze.length; r ++) { 
      for (int c = 0; c < maze[r].length; c ++) { 
        if (maze[r][c].isValid() && maze[r][c].hasDot()){ 
           fill(#FFEC00);
              noStroke();
              ellipse(maze[r][c].xPixel(), maze[r][c].yPixel(), 8, 8);

        } 
        }


         rectColor = color(#000000 );
  rectHighlight = color(51);
  rectX = 0;
  rectY = 0;
    fill (color(0,255,0));
    locPac = maze[pac.getR()][pac.getC()]; 
    ellipse (pac.xPixel() , pac.yPixel() , 16, 16);


    fill (color(255,0,0));
    locRed= maze[redd.getR()][redd.getC()]; 
    ellipse (redd.xPixel(), redd.yPixel(), 16, 16);


    fill (#FFC0CB) ;
    locPink = maze[pink.getR()][pink.getC()];
    ellipse (pink.xPixel(), pink.yPixel(), 16, 16);


    fill(#0000FF);
    locBlue = maze[blue.getR()][blue.getC()]; 
    ellipse (blue.xPixel(), blue.yPixel(), 16, 16);

    fill (#FFA500) ;
    locOrange = maze[orange.getR()][orange.getC()]; 
    ellipse (orange.xPixel(), orange.yPixel(), 16, 16);

   }

  if (locRed.equals(locPac) || locBlue.equals(locPac) || locOrange.equals(locPac) || locPink.equals(locPac)){ 
    background(0);
    println("game over: " + pac.score + " points earned");
    noLoop();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {exit();}
    exit();
      } 

        }

void update(int x, int y) {
  if ( overRect(rectX, rectY, rectSize, rectSize) ) {
    rectOver = true;
  } else {
    rectOver = false;
  }
}

void mousePressed() {

  if (rectOver) {
    draw(); 
  }
}

boolean overRect(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

      void keyPressed() {

      // 1 = red, 2 = pink, 3 =  orange, 4 = blue;

      if (key == '1') { 

        n = redd; 
      } else if (key == '2') { 
        n = pink; 
      } else if (key == '3') { 
        n = orange;       
      } else if (key == '4') { 
        n = blue;
      } 

      locN = maze[n.getR()][n.getC()];
      ////////System.out.println(locN);

      //////////System.out.println(n.getLocation());
        if (key == CODED) {

      if (keyCode == UP) {

        if (n.getR() > 0 && maze[n.getR() -1][n.getC()].isValid()) 

        locN = maze[n.getR()- 1][n.getC()];
        ////////System.out.println(locN);
            //////////System.out.println(n.getLocation());
      } else if (keyCode == DOWN) {
        if (n.getR() < 35 && maze[n.getR() + 1][n.getC()].isValid()) 
        locN = maze[n.getR()+1][n.getC()];
            //////////System.out.println(n.getLocation());
      } else if (keyCode == RIGHT) { 
        if (n.getC() < 27 && maze[n.getR()][n.getC() + 1].isValid()) { 
          //////////System.out.println ("true");
        locN = maze[n.getR()][n.getC() + 1];
            //////////System.out.println(n.getLocation());
        }
        //////////System.out.println ("false");
      } else if (keyCode == LEFT) { 
        if (n.getC() > 0 && maze[n.getR()][n.getC() - 1].isValid()) 
        locN = maze[n.getR()][n.getC() - 1];
            //////////System.out.println(n.getLocation());
      }

           ArrayList<Location> neighbors = new ArrayList<Location>();
     neighbors.clear();

      for (int i = 0; i < xShift.length; i ++) { 
         if(pac.getR() + xShift[i] > 0 && pac.getR() + xShift[i] < 36 && pac.getC() + yShift[i] > 0 && pac.getC() + yShift[i] < 28) { 
         if ( maze[pac.getR() + xShift[i]][pac.getC() + yShift[i]].isValid()) { 
           neighbors.add(maze[pac.getR() + xShift[i]][pac.getC() + yShift[i]]); 
         } 
         }
      }


      for (Location n: neighbors) {

         int reddist= n.computeShortestPath(redd.getLocation()); 
         int pinkdist = n.computeShortestPath(pink.getLocation()); 
         int bluedist = n.computeShortestPath(blue.getLocation());
         int orangedist = n.computeShortestPath(orange.getLocation()); 

        n.setAvgDist((reddist + pinkdist + bluedist +orangedist)/5);
      }

       java.util.Collections.sort(neighbors);

       //for (Location local: neighbors) { 
         Location target = neighbors.get(0);

        for (Location n: neighbors) {
            if (locPac.hasDot()) { 

      locPac.setDot(new Dot("empty")); 
         pac.moveTo(n);
        }
        }
        n.moveTo(maze[locN.getR()][locN.getC()]);
        pac.setScore (pac.getScore() + 1);
        }
}

Our creature class looks like this:

  abstract class  Creature {

  int r, c; 
  int xPixel () { 
      return c * 16  + 8;
    }
     int yPixel () { 
      return r * 16 + 8; 
    }  
  }
Tagged:

Answers

  • In keyPressed you can set a boolean moveLeft to true (and others to false)

    Evaluate moveLeft in draw with if and do the movement there (what you have in keyPressed now)

  • Thank you for the tip, but unfortunately, it still isn't working. Do you have any other ideas on what might be the problem?

    Here's what I put in my draw:

    if (moveLeft) { if (n.getC() > 0 && maze[n.getR()][n.getC() - 1].isValid()) { System.out.println("Hi!"); locN = maze[n.getR()][n.getC() - 1]; } }

    And this is what the code for moveLeft in keyPressed looks like right now: else if (keyCode == LEFT) { moveLeft = true; //if (n.getC() > 0 && maze[n.getR()][n.getC() - 1].isValid()) //locN = maze[n.getR()][n.getC() - 1]; //////////System.out.println(n.getLocation()); }

    I also have moveLeft set equal to false originally. When I implemented the code above, the program will print out "Hi!", but it won't moveLeft at all (it'll just stay stuck there.)

  • please edit the initial post, highlight the code and press ctrl-o to format it.

  • edited June 2017

    also, we don't have MainMaze.txt or mazeimg.png so can't run the code anyway

  • edited June 2017

    ok, your modifications look ok. there's probably another problem.

    but without those files we can't run the code.

  • I didn't post all the files because there are a lot haha, but here are all of them:

    AntiPacman: https://pastebin.com/qbfMq0x5

    Creature class: https://pastebin.com/9KNiDTRw

    Dot class:https://pastebin.com/apMJ6AxU

    Empty space class: https://pastebin.com/FjC58q1E

    Ghost class: https://pastebin.com/qj5tKcJv

    Location class: https://pastebin.com/atbXsqcw

    Pacman class: https://pastebin.com/2jEq1Twd

    Red ghost class: https://pastebin.com/KeU3fCxe

    Wall class: https://pastebin.com/CvGdebBX

    MainMaze.txt: https://pastebin.com/Qkj203is

    mazeimg.png: mazeimg

  • so we have to download a dozen java files, one of which is mis-named? what's up with a zip file?

  • ALL those files needed renaming.

  • The file "redghost.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

  • Okay, I updated the dropbox link with all the correct files, but some images are not in use yet.

  • and learn to format your code, it's all over the place.

    things are so much easier if you indent things properly.

  • And this is what the code for moveLeft in keyPressed looks like right now

    you had to do the same thing with up, down and right too - set a flag in keypressed(), act on it in draw()

  • mazeimg = loadImage("mazeimg.png");
    

    don't load images in draw().

  • ok, i'm giving up

    i have no idea what i'm looking at and no idea what it's doing when i press a key.

    there are 804 lines and exactly 0 comments.

Sign In or Register to comment.