Guest User

antipacman

a guest
Jun 19th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.04 KB | None | 0 0
  1.   import java.util.*;
  2.  
  3.   Dot little, big, fruit;
  4.   Location locPac;
  5.   Location locRed;
  6.   Location locBlue;
  7.   Location locOrange;
  8.   Location locPink;
  9.   Pacman pac;
  10.   RedGhost redd;
  11.   RedGhost blue;
  12.   RedGhost pink;
  13.   RedGhost orange;
  14.   RedGhost n ;
  15.   Location locN;
  16.   boolean moveLeft = false;
  17.  
  18.   public Location[][] maze;
  19.   int x, y;
  20.   PImage mazeimg;
  21.   PImage pacman;
  22.   PImage red;
  23.   boolean called = true;
  24.   int a = 0;
  25.    
  26.  
  27. int[] xShift = {-1, 0,  0 ,1};
  28. int[] yShift = { 0, 1, -1, 0};
  29.  
  30. int rectX, rectY;      // Position of square button
  31. int rectSize = 10;
  32. color rectHighlight;
  33. color currentColor;
  34. boolean rectOver = false;
  35. color rectColor;
  36.  
  37.   void setup() {
  38.     size(448, 576);
  39.     //size(448, 726);
  40.     //surface.setResizable(true);
  41.     background(255);
  42.     mazeimg = loadImage("mazeimg.png");
  43.     image(mazeimg, 0, 0);
  44.    
  45.   rectColor = color(0);
  46.   rectHighlight = color(51);
  47.   rectX = 0;
  48.   rectY = 0;
  49.  
  50.  
  51.     //////////System.out.println(start);
  52.     int cols = 28;
  53.     String[] lines = loadStrings("MainMaze.txt");
  54.     //String[] mazeLines = new String[lines.length];
  55.     maze = new Location[36][28];
  56.  
  57.     for (int r = 0; r < lines.length; r ++) {
  58.       for (int c = 0; c < lines[0].length(); c ++) {
  59.         //////////System.out.println("row: " + r + " col: " + c);
  60.         if (lines[r].charAt(c) == '0') {
  61.           maze[r][c] = new Wall (r, c);
  62.         } else {
  63.           maze[r][c] = new EmptySpace(r,c);
  64.         }
  65.         }
  66.       }
  67.       int x1 = 0, y1 = 0;
  68.       int c;
  69.       for (int r = 0; r < maze.length; r ++) {
  70.         for (c =  0; c < maze[r].length; c ++) {
  71.           y1 = (width / 28) * r; //+75;
  72.           x1 = (height / 36) * c;
  73.           if (!maze[r][c].isValid()) { // if it's wall
  74.             noFill();
  75.             rect (x1, y1, (width/ 28), (height/36));
  76.           }
  77.           else if (maze[r][c].isValid()){ // if it's an open space
  78.           //////////System.out.println ("x: " + r + " y: " + c);
  79.          
  80.             fill(#000000);
  81.             //noStroke(); (add this in later when we're done)
  82.             rect (x1, y1, (width/ 28), (height/36));
  83.             // for tiny dots
  84.          
  85.              
  86.           }
  87.         }  
  88.       }
  89.      
  90.     pac = new Pacman ();
  91.     locPac = maze[pac.getR()][pac.getC()];
  92.     ellipse (pac.xPixel(), pac.yPixel() , 16, 16);
  93.  
  94.     redd = new RedGhost("red");
  95.     locRed = maze[redd.getR()][redd.getC()];
  96.     redd.moveTo(maze[16][12]);
  97.     ellipse ( redd.xPixel() , redd.yPixel(), 16, 16);
  98.    
  99.      
  100.     pink = new RedGhost("pink");
  101.     locPink = maze[pink.getR()][pink.getC()];
  102.     pink.moveTo(maze[16][15]);
  103.      ellipse (pink.xPixel() , pink.yPixel(), 16, 16);
  104.      
  105.     blue = new RedGhost("blue");
  106.     locBlue = maze[blue.getR()][blue.getC()];
  107.     blue.moveTo(maze[18][12]);
  108.     ellipse ( blue.xPixel() , blue.yPixel(), 16, 16);
  109.    
  110.      
  111.     orange = new RedGhost("orange");
  112.     locOrange = maze[orange.getR()][orange.getC()];
  113.     orange.moveTo(maze[18][15]);
  114.      ellipse (orange.xPixel() , orange.yPixel(), 16, 16);
  115.      
  116.      n = redd;
  117.  
  118.  
  119.      
  120.       //System.out.println("red: "+ maze[redd.getR()][redd.getC()]);
  121.    
  122.        //  image (loadImage("pacman2", "gif"), pac.xPixel(), pac.yPixel());
  123.  
  124.  
  125.   }
  126.  
  127.   void draw() {
  128.    
  129.     //////////System.out.println (n);
  130.      a++;
  131.      if (moveLeft) {
  132.        if (n.getC() > 0 && maze[n.getR()][n.getC() - 1].isValid()) {
  133.          System.out.println("Hi!");
  134.         locN = maze[n.getR()][n.getC() - 1];
  135.        }
  136.      }
  137.  
  138.  
  139.     if (called && a == 40) {
  140.       //System.out.println (true);
  141.       redd.moveRandom();
  142.          pink.moveRandom();
  143.          blue.moveRandom();
  144.          orange.moveRandom();
  145.          called = false;
  146.     }
  147.    
  148.     update(mouseX, mouseY);
  149.     if (rectOver) {
  150.     fill(rectHighlight);
  151.      } else {
  152.     fill(rectColor);
  153.   }
  154.   stroke(255);
  155.   rect(rectX, rectY, rectSize, rectSize);
  156.  
  157.  
  158.     color col = color(0, 255, 0);
  159.     fill(col);
  160.     noStroke();
  161.     y = locPac.getR() * (width / 28) + 16;
  162.     x = locPac.getC() * (height/ 36) + 8;
  163.       background (0);
  164.      
  165.      
  166.  
  167.     background(255);
  168.     mazeimg = loadImage("mazeimg.png");
  169.     image(mazeimg, 0, 0);
  170.     for (int r = 0; r < maze.length; r ++) {
  171.       for (int c = 0; c < maze[r].length; c ++) {
  172.         if (maze[r][c].isValid() && maze[r][c].hasDot()){
  173.            fill(#FFEC00);
  174.               noStroke();
  175.               ellipse(maze[r][c].xPixel(), maze[r][c].yPixel(), 8, 8);
  176.  
  177.         }
  178.         }
  179.        
  180.        
  181.          rectColor = color(#000000 );
  182.   rectHighlight = color(51);
  183.   rectX = 0;
  184.   rectY = 0;
  185.     fill (color(0,255,0));
  186.     locPac = maze[pac.getR()][pac.getC()];
  187.     ellipse (pac.xPixel() , pac.yPixel() , 16, 16);
  188.      
  189.      
  190.     fill (color(255,0,0));
  191.     locRed= maze[redd.getR()][redd.getC()];
  192.     ellipse (redd.xPixel(), redd.yPixel(), 16, 16);
  193.      
  194.      
  195.     fill (#FFC0CB) ;
  196.     locPink = maze[pink.getR()][pink.getC()];
  197.     ellipse (pink.xPixel(), pink.yPixel(), 16, 16);
  198.      
  199.      
  200.     fill(#0000FF);
  201.     locBlue = maze[blue.getR()][blue.getC()];
  202.     ellipse (blue.xPixel(), blue.yPixel(), 16, 16);
  203.    
  204.     fill (#FFA500) ;
  205.     locOrange = maze[orange.getR()][orange.getC()];
  206.     ellipse (orange.xPixel(), orange.yPixel(), 16, 16);
  207.  
  208.     //long difference = System.nanoTime() - start;
  209.     //////////System.out.println (difference * Math.pow(10,-9));
  210.  
  211.      
  212.      
  213.    }
  214.  
  215.   if (locRed.equals(locPac) || locBlue.equals(locPac) || locOrange.equals(locPac) || locPink.equals(locPac)){
  216.     background(0);
  217.     println("game over: " + pac.score + " points earned");
  218.     noLoop();
  219.     try {
  220.         Thread.sleep(1000);
  221.     } catch (Exception e) {exit();}
  222.     exit();
  223.       }
  224.  
  225.  
  226.   }
  227.            
  228. void update(int x, int y) {
  229.   if ( overRect(rectX, rectY, rectSize, rectSize) ) {
  230.     rectOver = true;
  231.   } else {
  232.     rectOver = false;
  233.   }
  234. }
  235.  
  236. void mousePressed() {
  237.  
  238.   if (rectOver) {
  239.     draw();
  240.   }
  241. }
  242.  
  243. boolean overRect(int x, int y, int width, int height)  {
  244.   if (mouseX >= x && mouseX <= x+width &&
  245.       mouseY >= y && mouseY <= y+height) {
  246.     return true;
  247.   } else {
  248.     return false;
  249.   }
  250. }
  251.  
  252.  
  253.    
  254.       void keyPressed() {
  255.  
  256.  
  257.       // 1 = red, 2 = pink, 3 =  orange, 4 = blue;
  258.      
  259.       if (key == '1') {
  260.        
  261.         n = redd;
  262.       } else if (key == '2') {
  263.         n = pink;
  264.       } else if (key == '3') {
  265.         n = orange;      
  266.       } else if (key == '4') {
  267.         n = blue;
  268.       }
  269.  
  270.  
  271.       locN = maze[n.getR()][n.getC()];
  272.       ////////System.out.println(locN);
  273.    
  274.       //////////System.out.println(n.getLocation());
  275.         if (key == CODED) {
  276.  
  277.       if (keyCode == UP) {
  278.  
  279.         if (n.getR() > 0 && maze[n.getR() -1][n.getC()].isValid())
  280.  
  281.         locN = maze[n.getR()- 1][n.getC()];
  282.         ////////System.out.println(locN);
  283.             //////////System.out.println(n.getLocation());
  284.       } else if (keyCode == DOWN) {
  285.         if (n.getR() < 35 && maze[n.getR() + 1][n.getC()].isValid())
  286.         locN = maze[n.getR()+1][n.getC()];
  287.             //////////System.out.println(n.getLocation());
  288.       } else if (keyCode == RIGHT) {
  289.         if (n.getC() < 27 && maze[n.getR()][n.getC() + 1].isValid()) {
  290.           //////////System.out.println ("true");
  291.         locN = maze[n.getR()][n.getC() + 1];
  292.             //////////System.out.println(n.getLocation());
  293.         }
  294.         //////////System.out.println ("false");
  295.       } else if (keyCode == LEFT) {
  296.         moveLeft = true;
  297.         //if (n.getC() > 0 && maze[n.getR()][n.getC() - 1].isValid())
  298.         //locN = maze[n.getR()][n.getC() - 1];
  299.             //////////System.out.println(n.getLocation());
  300.       }
  301.      
  302.            ArrayList<Location> neighbors = new ArrayList<Location>();
  303.      neighbors.clear();
  304.      
  305.       for (int i = 0; i < xShift.length; i ++) {
  306.          if(pac.getR() + xShift[i] > 0 && pac.getR() + xShift[i] < 36 && pac.getC() + yShift[i] > 0 && pac.getC() + yShift[i] < 28) {
  307.          if ( maze[pac.getR() + xShift[i]][pac.getC() + yShift[i]].isValid()) {
  308.            neighbors.add(maze[pac.getR() + xShift[i]][pac.getC() + yShift[i]]);
  309.          }
  310.          }
  311.       }
  312.    
  313.  
  314.       for (Location n: neighbors) {
  315.        
  316.          int reddist= n.computeShortestPath(redd.getLocation());
  317.          int pinkdist = n.computeShortestPath(pink.getLocation());
  318.          int bluedist = n.computeShortestPath(blue.getLocation());
  319.          int orangedist = n.computeShortestPath(orange.getLocation());
  320.        
  321.         n.setAvgDist((reddist + pinkdist + bluedist +orangedist)/5);
  322.       }
  323.      
  324.        java.util.Collections.sort(neighbors);
  325.        
  326.        //for (Location local: neighbors) {
  327.          Location target = neighbors.get(0);
  328.        
  329.         for (Location n: neighbors) {
  330.             if (locPac.hasDot()) {
  331.      
  332.       locPac.setDot(new Dot("empty"));
  333.          pac.moveTo(n);
  334.         }
  335.         }
  336.  
  337.    
  338.         n.moveTo(maze[locN.getR()][locN.getC()]);
  339.         pac.setScore (pac.getScore() + 1);
  340.      
  341.    
  342.         }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment