Advertisement
PaytenFriend

Untitled

Nov 27th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.94 KB | None | 0 0
  1. //
  2. // PacMan
  3. // Another 1 day game (created in 5 hours).
  4. //
  5. // (C)2000
  6. // Brian Postma
  7. // b.postma@hetnet.nl
  8. // http://www.homepages.hetnet.nl/~brianpostma
  9. //
  10.  
  11. import java.awt.*;
  12. import java.applet.Applet;
  13. import java.util.Random;
  14.  
  15. public class PacMan extends Applet implements Runnable {
  16.  
  17.     Random r = new Random();
  18.     Dimension d;
  19.     Font largefont = new Font("Helvetica", Font.BOLD, 24);
  20.     Font smallfont = new Font("Helvetica", Font.BOLD, 14);
  21.     FontMetrics fmsmall, fmlarge;
  22.     Graphics goff;
  23.     Image ii;
  24.     Thread thethread;
  25.     MediaTracker thetracker = null;
  26.     Color dotcolor = new Color(192, 192, 0);
  27.     int bigdotcolor = 192;
  28.     int trolldotcolor = 150;
  29.     int dbigdotcolor = -2;
  30.     Color mazecolor;
  31.     boolean ingame = false;
  32.     boolean showtitle = true;
  33.     boolean scared = false;
  34.     boolean dying = false;
  35.     boolean right, left, up, down;
  36.     final int screendelay = 120;
  37.     final int blocksize = 24;
  38.     final int nrofblocks = 23;
  39.     final int scrsize = nrofblocks * blocksize;
  40.     final int animdelay = 8;
  41.     final int pacanimdelay = 2;
  42.     final int ghostanimcount = 2;
  43.     final int pacmananimcount = 4;
  44.     final int maxghosts = 120;
  45.     final int pacmanspeed = 6;
  46.     int animcount = animdelay;
  47.     int pacanimcount = pacanimdelay;
  48.     int pacanimdir = 1;
  49.     int count = screendelay;
  50.     int ghostanimpos = 0;
  51.     int pacmananimpos = 0;
  52.     int nrofghosts = 6;
  53.     int pacsleft, score;
  54.     int deathcounter;
  55.     int[] dx, dy;
  56.     int[] ghostx, ghosty, ghostdx, ghostdy, ghostspeed;
  57.     Image ghost1, ghost2, ghostscared1, ghostscared2;
  58.     Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;
  59.     Image pacman3up, pacman3down, pacman3left, pacman3right;
  60.     Image pacman4up, pacman4down, pacman4left, pacman4right;
  61.     int pacmanx, pacmany, pacmandx, pacmandy;
  62.     int reqdx, reqdy, viewdx, viewdy;
  63.     int scaredcount, scaredtime;
  64.     final int maxscaredtime = 120;
  65.     final int minscaredtime = 20;
  66.     final short level1data[] = { // 1 left wall, 2 top wall, 4 right wall, 8 bottom wall, 16 is a dot, 32 is a big dot
  67.         35, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22,
  68.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  69.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  70.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  71.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  72.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  73.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  74.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  75.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  76.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  77.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  78.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 7, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  79.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  80.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  81.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  82.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  83.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  84.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  85.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  86.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  87.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  88.         17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 64, 20,
  89.         25, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 28, };
  90.    
  91.     final int validspeeds[] = {1, 2, 3, 4, 6, 8};
  92.     final int maxspeed = 6;
  93.     int currentspeed = 3;
  94.     short[] screendata;
  95.  
  96.     public String getAppletInfo()
  97.     {
  98.         return ("Pacman - by Brian Postma");
  99.     }
  100.  
  101.     public void init()
  102.     {
  103.         setSize(549,600);
  104.         short i;
  105.         GetImages();
  106.  
  107.         screendata = new short[nrofblocks * nrofblocks];
  108.  
  109.         Graphics g;
  110.         d = size();
  111.         setBackground(Color.black);
  112.         g = getGraphics();
  113.         g.setFont(smallfont);
  114.         fmsmall = g.getFontMetrics();
  115.         g.setFont(largefont);
  116.         fmlarge = g.getFontMetrics();
  117.         ghostx = new int[maxghosts];
  118.         ghostdx = new int[maxghosts];
  119.         ghosty = new int[maxghosts];
  120.         ghostdy = new int[maxghosts];
  121.         ghostspeed = new int[maxghosts];
  122.         dx = new int[4];
  123.         dy = new int[4];
  124.         GameInit();
  125.     }
  126.  
  127.     public void GameInit() {
  128.         pacsleft = 3;
  129.         score = 0;
  130.         scaredtime = maxscaredtime;
  131.         LevelInit();
  132.         nrofghosts = 6;
  133.         currentspeed = 3;
  134.         scaredtime = maxscaredtime;
  135.     }
  136.  
  137.     public void LevelInit() {
  138.         int i;
  139.         for (i = 0; i < nrofblocks * nrofblocks; i++) {
  140.             screendata[i] = level1data[i];
  141.         }
  142.  
  143.         LevelContinue();
  144.     }
  145.  
  146.     public void LevelContinue() {
  147.         short i;
  148.         int dx = 1;
  149.         int random;
  150.  
  151.         for (i = 0; i < nrofghosts; i++) {
  152.             ghosty[i] = 8 * blocksize;
  153.             ghostx[i] = 11 * blocksize;
  154.             ghostdy[i] = 0;
  155.             ghostdx[i] = dx;
  156.             dx = -dx;
  157.             random = (int) (Math.random() * (currentspeed + 1));
  158.             if (random > currentspeed) {
  159.                 random = currentspeed;
  160.             }
  161.             ghostspeed[i] = validspeeds[random];
  162.         }
  163.         screendata[7 * nrofblocks + 6] = 00;
  164.         screendata[7 * nrofblocks + 8] = 00;
  165.         pacmanx =  11  * blocksize;
  166.         pacmany =  11 * blocksize;
  167.         pacmandx = 0;
  168.         pacmandy = 0;
  169.         reqdx = 0;
  170.         reqdy = 0;
  171.         viewdx = -1;
  172.         viewdy = 0;
  173.         dying = false;
  174.         scared = false;
  175.     }
  176.  
  177.     public void GetImages() {
  178.         thetracker = new MediaTracker(this);
  179.  
  180.         ghost1 = getImage(getDocumentBase(), "pacpix/Ghost1.gif");
  181.         thetracker.addImage(ghost1, 0);
  182.         ghost2 = getImage(getDocumentBase(), "pacpix/Ghost2.gif");
  183.         thetracker.addImage(ghost2, 0);
  184.         ghostscared1 = getImage(getDocumentBase(), "pacpix/GhostScared1.gif");
  185.         thetracker.addImage(ghostscared1, 0);
  186.         ghostscared2 = getImage(getDocumentBase(), "pacpix/GhostScared2.gif");
  187.         thetracker.addImage(ghostscared2, 0);
  188.  
  189.         pacman1 = getImage(getDocumentBase(), "pacpix/PacMan1.gif");
  190.         thetracker.addImage(pacman1, 0);
  191.         pacman2up = getImage(getDocumentBase(), "pacpix/PacMan2up.gif");
  192.         thetracker.addImage(pacman2up, 0);
  193.         pacman3up = getImage(getDocumentBase(), "pacpix/PacMan3up.gif");
  194.         thetracker.addImage(pacman3up, 0);
  195.         pacman4up = getImage(getDocumentBase(), "pacpix/PacMan4up.gif");
  196.         thetracker.addImage(pacman4up, 0);
  197.  
  198.         pacman2down = getImage(getDocumentBase(), "pacpix/PacMan2down.gif");
  199.         thetracker.addImage(pacman2down, 0);
  200.         pacman3down = getImage(getDocumentBase(), "pacpix/PacMan3down.gif");
  201.         thetracker.addImage(pacman3down, 0);
  202.         pacman4down = getImage(getDocumentBase(), "pacpix/PacMan4down.gif");
  203.         thetracker.addImage(pacman4down, 0);
  204.  
  205.         pacman2left = getImage(getDocumentBase(), "pacpix/PacMan2left.gif");
  206.         thetracker.addImage(pacman2left, 0);
  207.         pacman3left = getImage(getDocumentBase(), "pacpix/PacMan3left.gif");
  208.         thetracker.addImage(pacman3left, 0);
  209.         pacman4left = getImage(getDocumentBase(), "pacpix/PacMan4left.gif");
  210.         thetracker.addImage(pacman4left, 0);
  211.  
  212.         pacman2right = getImage(getDocumentBase(), "pacpix/PacMan2right.gif");
  213.         thetracker.addImage(pacman2right, 0);
  214.         pacman3right = getImage(getDocumentBase(), "pacpix/PacMan3right.gif");
  215.         thetracker.addImage(pacman3right, 0);
  216.         pacman4right = getImage(getDocumentBase(), "pacpix/PacMan4right.gif");
  217.         thetracker.addImage(pacman4right, 0);
  218.  
  219.         try {
  220.             thetracker.waitForAll();
  221.         } catch (InterruptedException e) {
  222.             return;
  223.         }
  224.     }
  225.  
  226.     public boolean keyDown(Event e, int key) {
  227.         if (ingame) {
  228.             if (key == Event.LEFT) {
  229.                 reqdx = -1;
  230.                 reqdy = 0;
  231.             } else if (key == Event.RIGHT) {
  232.                 reqdx = 1;
  233.                 reqdy = 0;
  234.             } else if (key == Event.UP) {
  235.                 reqdx = 0;
  236.                 reqdy = -1;
  237.             } else if (key == Event.DOWN) {
  238.                 reqdx = 0;
  239.                 reqdy = 1;
  240.             } else if (key == Event.ESCAPE) {
  241.                 ingame = false;
  242.             }
  243.         } else {
  244.             if (key == 's' || key == 'S') {
  245.                 ingame = true;
  246.                 GameInit();
  247.             }
  248.         }
  249.         return true;
  250.     }
  251.  
  252.     public boolean keyUp(Event e, int key) {
  253.         if (key == Event.LEFT || key == Event.RIGHT || key == Event.UP || key == Event.DOWN) {
  254.             reqdx = 0;
  255.             reqdy = 0;
  256.         }
  257.         return true;
  258.     }
  259.  
  260.     public void paint(Graphics g) {
  261.         String s;
  262.         Graphics gg;
  263.  
  264.         if (goff == null && d.width > 0 && d.height > 0) {
  265.             ii = createImage(d.width, d.height);
  266.             goff = ii.getGraphics();
  267.         }
  268.         if (goff == null || ii == null) {
  269.             return;
  270.         }
  271.  
  272.         goff.setColor(Color.black);
  273.         goff.fillRect(0, 0, d.width, d.height);
  274.  
  275.         DrawMaze();
  276.         DrawScore();
  277.         DoAnim();
  278.         if (ingame) {
  279.             PlayGame();
  280.         } else {
  281.             PlayDemo();
  282.         }
  283.  
  284.         g.drawImage(ii, 0, 0, this);
  285.     }
  286.  
  287.     public void DoAnim() {
  288.         animcount--;
  289.         if (animcount <= 0) {
  290.             animcount = animdelay;
  291.             ghostanimpos++;
  292.             if (ghostanimpos >= ghostanimcount) {
  293.                 ghostanimpos = 0;
  294.             }
  295.         }
  296.         pacanimcount--;
  297.         if (pacanimcount <= 0) {
  298.             pacanimcount = pacanimdelay;
  299.             pacmananimpos = pacmananimpos + pacanimdir;
  300.             if (pacmananimpos == (pacmananimcount - 1) || pacmananimpos == 0) {
  301.                 pacanimdir = -pacanimdir;
  302.             }
  303.         }
  304.     }
  305.  
  306.     public void PlayGame() {
  307.         if (dying) {
  308.             Death();
  309.         } else {
  310.             CheckScared();
  311.             MovePacMan();
  312.             DrawPacMan();
  313.             MoveGhosts();
  314.             CheckMaze();
  315.         }
  316.     }
  317.  
  318.     public void PlayDemo() {
  319.         CheckScared();
  320.         MoveGhosts();
  321.         ShowIntroScreen();
  322.     }
  323.  
  324.     public void Death() {
  325.         int k;
  326.  
  327.         deathcounter--;
  328.         k = (deathcounter & 15) / 4;
  329.         switch (k) {
  330.             case 0:
  331.                 goff.drawImage(pacman4up, pacmanx + 1, pacmany + 1, this);
  332.                 break;
  333.             case 1:
  334.                 goff.drawImage(pacman4right, pacmanx + 1, pacmany + 1, this);
  335.                 break;
  336.             case 2:
  337.                 goff.drawImage(pacman4down, pacmanx + 1, pacmany + 1, this);
  338.                 break;
  339.             default:
  340.                 goff.drawImage(pacman4left, pacmanx + 1, pacmany + 1, this);
  341.         }
  342.         if (deathcounter == 0) {
  343.             pacsleft--;
  344.             if (pacsleft == 0) {
  345.                 ingame = false;
  346.             }
  347.             LevelContinue();
  348.         }
  349.     }
  350.  
  351.     public void MoveGhosts() {
  352.         short i;
  353.         int pos;
  354.         int count;
  355.  
  356.         for (i = 0; i < nrofghosts; i++) {
  357.             if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {
  358.                 pos = ghostx[i] / blocksize + nrofblocks * (int) (ghosty[i] / blocksize);
  359.  
  360.                 count = 0;
  361.                 if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) {
  362.                     dx[count] = -1;
  363.                     dy[count] = 0;
  364.                     count++;
  365.                 }
  366.                 if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {
  367.                     dx[count] = 0;
  368.                     dy[count] = -1;
  369.                     count++;
  370.                 }
  371.                 if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {
  372.                     dx[count] = 1;
  373.                     dy[count] = 0;
  374.                     count++;
  375.                 }
  376.                 if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {
  377.                     dx[count] = 0;
  378.                     dy[count] = 1;
  379.                     count++;
  380.                 }
  381.                 if (count == 0) {
  382.                     if ((screendata[pos] & 15) == 15) {
  383.                         ghostdx[i] = 0;
  384.                         ghostdy[i] = 0;
  385.                     } else {
  386.                         ghostdx[i] = -ghostdx[i];
  387.                         ghostdy[i] = -ghostdy[i];
  388.                     }
  389.                 } else {
  390.                     count = (int) (Math.random() * count);
  391.                     if (count > 3) {
  392.                         count = 3;
  393.                     }
  394.                     ghostdx[i] = dx[count];
  395.                     ghostdy[i] = dy[count];
  396.                 }
  397.             }
  398.             ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
  399.             ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
  400.             DrawGhost(ghostx[i] + 1, ghosty[i] + 1);
  401.  
  402.             if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12)
  403.                     && pacmany > (ghosty[i] - 12) && pacmany < (ghosty[i] + 12) && ingame) {
  404.                 if (scared) {
  405.                     score += 10;
  406.                     ghostx[i] = 11 * blocksize;
  407.                     ghosty[i] = 9 * blocksize;
  408.                 } else {
  409.                     dying = true;
  410.                     deathcounter = 64;
  411.                 }
  412.             }
  413.         }
  414.     }
  415.  
  416.     public void DrawGhost(int x, int y) {
  417.         if (ghostanimpos == 0 && !scared) {
  418.             goff.drawImage(ghost1, x, y, this);
  419.         } else if (ghostanimpos == 1 && !scared) {
  420.             goff.drawImage(ghost2, x, y, this);
  421.         } else if (ghostanimpos == 0 && scared) {
  422.             goff.drawImage(ghostscared1, x, y, this);
  423.         } else if (ghostanimpos == 1 && scared) {
  424.             goff.drawImage(ghostscared2, x, y, this);
  425.         }
  426.     }
  427.  
  428.     public void MovePacMan() {
  429.         int pos;
  430.         short ch;
  431.  
  432.         if (reqdx == -pacmandx && reqdy == -pacmandy) {
  433.             pacmandx = reqdx;
  434.             pacmandy = reqdy;
  435.             viewdx = pacmandx;
  436.             viewdy = pacmandy;
  437.         }
  438.         if (pacmanx % blocksize == 0 && pacmany % blocksize == 0) {
  439.             pos = pacmanx / blocksize + nrofblocks * (int) (pacmany / blocksize);
  440.             ch = screendata[pos];
  441.             if ((ch & 16) != 0) {
  442.                 screendata[pos] = (short) (ch & 15);
  443.                 score++;
  444.             }
  445.             if ((ch & 32) != 0) {
  446.                 scared = true;
  447.                 scaredcount = scaredtime;
  448.                 screendata[pos] = (short) (ch & 15);
  449.                 score += 5;
  450.             }
  451.             if ((ch & 64) != 0) {
  452.                 scared = false;
  453.                 screendata[pos] = (short) (ch & 15);
  454.                 score -= 100;
  455.                 pacmanx =  r.nextInt(22) * blocksize;
  456.                 pacmany =  r.nextInt(22) *  blocksize;
  457.             }
  458.  
  459.             if (reqdx != 0 || reqdy != 0) {
  460.                 if (!((reqdx == -1 && reqdy == 0 && (ch & 1) != 0)
  461.                         || (reqdx == 1 && reqdy == 0 && (ch & 4) != 0)
  462.                         || (reqdx == 0 && reqdy == -1 && (ch & 2) != 0)
  463.                         || (reqdx == 0 && reqdy == 1 && (ch & 8) != 0))) {
  464.                     pacmandx = reqdx;
  465.                     pacmandy = reqdy;
  466.                     viewdx = pacmandx;
  467.                     viewdy = pacmandy;
  468.                 }
  469.             }
  470.  
  471.             // Check for standstill
  472.             if ((pacmandx == -1 && pacmandy == 0 && (ch & 1) != 0)
  473.                     || (pacmandx == 1 && pacmandy == 0 && (ch & 4) != 0)
  474.                     || (pacmandx == 0 && pacmandy == -1 && (ch & 2) != 0)
  475.                     || (pacmandx == 0 && pacmandy == 1 && (ch & 8) != 0)) {
  476.                 pacmandx = 0;
  477.                 pacmandy = 0;
  478.             }
  479.         }
  480.         pacmanx = pacmanx + pacmanspeed * pacmandx;
  481.         pacmany = pacmany + pacmanspeed * pacmandy;
  482.     }
  483.  
  484.     public void DrawPacMan() {
  485.         if (viewdx == -1) {
  486.             DrawPacManLeft();
  487.             left = true;
  488.             up = false;
  489.             down = false;
  490.             right = false;
  491.         } else if (viewdx == 1) {
  492.             DrawPacManRight();
  493.             right = true;
  494.             left = false;
  495.             up = false;
  496.             down = false;
  497.         } else if (viewdy == -1) {
  498.             DrawPacManUp();
  499.             up = true;
  500.             right = false;
  501.             left = false;
  502.             down = false;
  503.         } else if (viewdy == 1) {
  504.             DrawPacManDown();
  505.             right = false;
  506.             left = false;
  507.             up = false;
  508.             down = true;
  509.         } else {
  510.             if (left) {
  511.                 DrawPacManLeft();
  512.             }
  513.             else if (right) {
  514.                 DrawPacManRight();
  515.             }
  516.             else if (up) {
  517.                 DrawPacManUp();
  518.             }
  519.             else if (down) {
  520.                 DrawPacManDown();
  521.             }
  522.             else {
  523.                 DrawPacManDown();
  524.             }
  525.         }        
  526.     }
  527.  
  528.     public void DrawPacManUp() {
  529.         switch (pacmananimpos) {
  530.             case 1:
  531.                 goff.drawImage(pacman2up, pacmanx + 1, pacmany + 1, this);
  532.                 break;
  533.             case 2:
  534.                 goff.drawImage(pacman3up, pacmanx + 1, pacmany + 1, this);
  535.                 break;
  536.             case 3:
  537.                 goff.drawImage(pacman4up, pacmanx + 1, pacmany + 1, this);
  538.                 break;
  539.             default:
  540.                 goff.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
  541.                 break;
  542.         }
  543.     }
  544.  
  545.     public void DrawPacManDown() {
  546.         switch (pacmananimpos) {
  547.             case 1:
  548.                 goff.drawImage(pacman2down, pacmanx + 1, pacmany + 1, this);
  549.                 break;
  550.             case 2:
  551.                 goff.drawImage(pacman3down, pacmanx + 1, pacmany + 1, this);
  552.                 break;
  553.             case 3:
  554.                 goff.drawImage(pacman4down, pacmanx + 1, pacmany + 1, this);
  555.                 break;
  556.             default:
  557.                 goff.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
  558.                 break;
  559.         }
  560.     }
  561.  
  562.     public void DrawPacManLeft() {
  563.         switch (pacmananimpos) {
  564.             case 1:
  565.                 goff.drawImage(pacman2left, pacmanx + 1, pacmany + 1, this);
  566.                 break;
  567.             case 2:
  568.                 goff.drawImage(pacman3left, pacmanx + 1, pacmany + 1, this);
  569.                 break;
  570.             case 3:
  571.                 goff.drawImage(pacman4left, pacmanx + 1, pacmany + 1, this);
  572.                 break;
  573.             default:
  574.                 goff.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
  575.                 break;
  576.         }
  577.     }
  578.  
  579.     public void DrawPacManRight() {
  580.         switch (pacmananimpos) {
  581.             case 1:
  582.                 goff.drawImage(pacman2right, pacmanx + 1, pacmany + 1, this);
  583.                 break;
  584.             case 2:
  585.                 goff.drawImage(pacman3right, pacmanx + 1, pacmany + 1, this);
  586.                 break;
  587.             case 3:
  588.                 goff.drawImage(pacman4right, pacmanx + 1, pacmany + 1, this);
  589.                 break;
  590.             default:
  591.                 goff.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
  592.                 break;
  593.         }
  594.     }
  595.    
  596.     public void DrawMaze() {
  597.         short i = 0;
  598.         int x, y;
  599.  
  600.         bigdotcolor = bigdotcolor + dbigdotcolor;
  601.         if (bigdotcolor <= 64 || bigdotcolor >= 192) {
  602.             dbigdotcolor = -dbigdotcolor;
  603.         }
  604.  
  605.         for (y = 0; y < scrsize; y += blocksize) {
  606.             for (x = 0; x < scrsize; x += blocksize) {
  607.                 goff.setColor(mazecolor);
  608.                 if ((screendata[i] & 1) != 0) {
  609.                     goff.drawLine(x, y, x, y + blocksize - 1);
  610.                 }
  611.                 if ((screendata[i] & 2) != 0) {
  612.                     goff.drawLine(x, y, x + blocksize - 1, y);
  613.                 }
  614.                 if ((screendata[i] & 4) != 0) {
  615.                     goff.drawLine(x + blocksize - 1, y, x + blocksize - 1, y + blocksize - 1);
  616.                 }
  617.                 if ((screendata[i] & 8) != 0) {
  618.                     goff.drawLine(x, y + blocksize - 1, x + blocksize - 1, y + blocksize - 1);
  619.                 }
  620.                 if ((screendata[i] & 16) != 0) {
  621.                     goff.setColor(dotcolor);
  622.                     goff.fillOval(x + 11, y + 11, 2, 2);
  623.                 }
  624.                 if ((screendata[i] & 32) != 0) {
  625.                     goff.setColor(new Color(224, 224 - bigdotcolor, bigdotcolor));
  626.                     goff.fillOval(x + 8, y + 8, 8, 8);
  627.                 }
  628.                 if ((screendata[i] & 64) != 0) {
  629.                     goff.setColor(new Color (224, 224 - trolldotcolor, trolldotcolor));
  630.                     goff.fillOval(x + 8, y + 8, 8, 8);
  631.                 }
  632.                 i++;
  633.             }
  634.         }
  635.     }
  636.    
  637.     public void ShowIntroScreen() {
  638.         String s;
  639.  
  640.         goff.setFont(largefont);
  641.  
  642.         goff.setColor(new Color(0, 32, 48));
  643.         goff.fillRect(16, scrsize / 2 - 40, scrsize - 32, 80);
  644.         goff.setColor(Color.white);
  645.         goff.drawRect(16, scrsize / 2 - 40, scrsize - 32, 80);
  646.  
  647.         if (showtitle) {
  648.             s = "I wanna be the Pacman";
  649.             scared = false;
  650.  
  651.             goff.setColor(Color.white);
  652.             goff.drawString(s, (scrsize - fmlarge.stringWidth(s)) / 2 + 2, scrsize / 2 - 20 + 2);
  653.             goff.setColor(new Color(96, 128, 255));
  654.             goff.drawString(s, (scrsize - fmlarge.stringWidth(s)) / 2, scrsize / 2 - 20);
  655.  
  656.             s = "(c)2005 by Brian Postma";
  657.             goff.setFont(smallfont);
  658.             goff.setColor(new Color(255, 160, 64));
  659.             goff.drawString(s, (scrsize - fmsmall.stringWidth(s)) / 2, scrsize / 2 + 10);
  660.         } else {
  661.             goff.setFont(smallfont);
  662.             goff.setColor(new Color(96, 128, 255));
  663.             s = "'S' to start game";
  664.             goff.drawString(s, (scrsize - fmsmall.stringWidth(s)) / 2, scrsize / 2 - 10);
  665.             goff.setColor(new Color(255, 160, 64));
  666.             s = "Use cursor keys to move";
  667.             goff.drawString(s, (scrsize - fmsmall.stringWidth(s)) / 2, scrsize / 2 + 20);
  668.             scared = true;
  669.         }
  670.         count--;
  671.         if (count <= 0) {
  672.             count = screendelay;
  673.             showtitle = !showtitle;
  674.         }
  675.     }
  676.  
  677.     public void DrawScore() {
  678.         int i;
  679.         String s;
  680.  
  681.         goff.setFont(smallfont);
  682.         goff.setColor(new Color(96, 128, 255));
  683.         s = "Score: " + score;
  684.         goff.drawString(s, scrsize / 2 + 96, scrsize + 16);
  685.         for (i = 0; i < pacsleft; i++) {
  686.             goff.drawImage(pacman3left, i * 28 + 8, scrsize + 1, this);
  687.         }
  688.     }
  689.  
  690.     public void CheckScared() {
  691.         scaredcount--;
  692.         if (scaredcount <= 0) {
  693.             scared = false;
  694.         }
  695.  
  696.         if (scared && scaredcount >= 30) {
  697.             mazecolor = new Color(192, 32, 255);
  698.         } else {
  699.             mazecolor = new Color(32, 192, 255);
  700.         }
  701.  
  702.         if (scared) {
  703.             screendata[15 * nrofblocks + 14] = 00;
  704.             screendata[15 * nrofblocks + 16] = 00;
  705.         } else {
  706.             screendata[15 * nrofblocks + 14] = 00;
  707.             screendata[15 * nrofblocks + 16] = 00;
  708.         }
  709.     }
  710.  
  711.     public void CheckMaze() {
  712.         short i = 0;
  713.         boolean finished = true;
  714.  
  715.         while (i < nrofblocks * nrofblocks && finished) {
  716.             if ((screendata[i] & 48) != 0) {
  717.                 finished = false;
  718.             }
  719.             i++;
  720.         }
  721.         if (finished) {
  722.             score += 50;
  723.             DrawScore();
  724.             try {
  725.                 Thread.sleep(3000);
  726.             } catch (InterruptedException e) {
  727.             }
  728.             if (nrofghosts < maxghosts) {
  729.                 nrofghosts++;
  730.             }
  731.             if (currentspeed < maxspeed) {
  732.                 currentspeed++;
  733.             }
  734.             scaredtime = scaredtime - 20;
  735.             if (scaredtime < minscaredtime) {
  736.                 scaredtime = minscaredtime;
  737.             }
  738.             LevelInit();
  739.         }
  740.     }
  741.  
  742.     public void run() {
  743.         long starttime;
  744.         Graphics g;
  745.  
  746.         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  747.         g = getGraphics();
  748.  
  749.         while (true) {
  750.             starttime = System.currentTimeMillis();
  751.             try {
  752.                 paint(g);
  753.                 starttime += 40;
  754.                 Thread.sleep(Math.max(0, starttime - System.currentTimeMillis()));
  755.             } catch (InterruptedException e) {
  756.                 break;
  757.             }
  758.         }
  759.     }
  760.  
  761.     public void start() {
  762.         if (thethread == null) {
  763.             thethread = new Thread(this);
  764.             thethread.start();
  765.         }
  766.     }
  767.  
  768.     public void stop() {
  769.         if (thethread != null) {
  770.             thethread.stop();
  771.             thethread = null;
  772.         }
  773.     }
  774. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement