Results 1 to 9 of 9

Thread: pacman code

  1. #1
    tarareddy is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default pacman code

    this is a code i found
    i need some help in understanding few things in it
    // Bit representations for game map
    // 1 -> Left wall
    // 2 -> Top wall
    // 4 -> Right wall
    // 8 -> Bottom wall
    // 16 -> Regular pellet
    //32->power pellet
    Java Code:
    public class Board extends JPanel implements ActionListener  {
        Dimension d;
        Font smallfont = new Font("Helvetica", Font.BOLD, 14);
    
        FontMetrics fmsmall, fmlarge;
        Image ii;
        Color dotcolor = new Color(192, 192, 0);
        Color mazecolor;
    
        boolean ingame = false;
        boolean dying = false;
    
        final int blocksize = 24;
        final int nrofblocks = 15;
        final int scrsize = nrofblocks * blocksize;
        final int pacanimdelay = 2;
        final int pacmananimcount = 4;
        final int maxghosts = 12;
        final int pacmanspeed = 6;
    
        int pacanimcount = pacanimdelay;
        int pacanimdir = 1;
        int pacmananimpos = 0;
        int nrofghosts = 6;
        int pacsleft, score;
        int deathcounter;
        int[] dx, dy;
        int[] ghostx, ghosty, ghostdx, ghostdy, ghostspeed;
    
        Image ghost;
        Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;
        Image pacman3up, pacman3down, pacman3left, pacman3right;
        Image pacman4up, pacman4down, pacman4left, pacman4right;
    
        int pacmanx, pacmany, pacmandx, pacmandy;
        int reqdx, reqdy, viewdx, viewdy;
    
        final short leveldata[] =
        { 19, 26, 26, 26, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22,
          21, 0,  0,  0,  17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
          21, 0,  0,  0,  17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 
          21, 0,  0,  0,  17, 16, 16, 24, 16, 16, 16, 16, 16, 16, 20, 
          17, 18, 18, 18, 16, 16, 20, 0,  17, 16, 16, 16, 16, 16, 20,
          17, 16, 16, 16, 16, 16, 20, 0,  17, 16, 16, 16, 16, 24, 20, 
          25, 16, 16, 16, 24, 24, 28, 0,  25, 24, 24, 16, 20, 0,  21, 
          1,  17, 16, 20, 0,  0,  0,  0,  0,  0,  0,  17, 20, 0,  21,
          1,  17, 16, 16, 18, 18, 22, 0,  19, 18, 18, 16, 20, 0,  21,
          1,  17, 16, 16, 16, 16, 20, 0,  17, 16, 16, 16, 20, 0,  21, 
          1,  17, 16, 16, 16, 16, 20, 0,  17, 16, 16, 16, 20, 0,  21,
          1,  17, 16, 16, 16, 16, 16, 18, 16, 16, 16, 16, 20, 0,  21,
          1,  17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0,  21,
          1,  25, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 18, 20,
          9,  8,  8,  8,  8,  8,  8,  8,  8,  8,  25, 24, 24, 24, 28 };
    
        final int validspeeds[] = { 1, 2, 3, 4, 6, 8 };
        final int maxspeed = 6;
    
        int currentspeed = 3;
        short[] screendata;
        Timer timer;
    
    
        public Board() {
    
            GetImages();
    
            addKeyListener(new TAdapter());
    
            screendata = new short[nrofblocks * nrofblocks];
            mazecolor = new Color(5, 100, 5);
            setFocusable(true);
    
            d = new Dimension(400, 400);
    
            setBackground(Color.black);
            setDoubleBuffered(true);
    
            ghostx = new int[maxghosts];
            ghostdx = new int[maxghosts];
            ghosty = new int[maxghosts];
            ghostdy = new int[maxghosts];
            ghostspeed = new int[maxghosts];
            dx = new int[4];
            dy = new int[4];
            timer = new Timer(40, this);
            timer.start();
        }
    
        public void addNotify() {
            super.addNotify();
            GameInit();
        }
    
    
        public void DoAnim() {
            pacanimcount--;
            if (pacanimcount <= 0) {
                pacanimcount = pacanimdelay;
                pacmananimpos = pacmananimpos + pacanimdir;           
                if (pacmananimpos == (pacmananimcount - 1) || pacmananimpos == 0)
                    pacanimdir = -pacanimdir;
            }
        }
    
    
        public void PlayGame(Graphics2D g2d) {
            if (dying) {
                Death();
            } else {
                MovePacMan();
                DrawPacMan(g2d);
                moveGhosts(g2d);
                CheckMaze();
            }
        }
    
    
        public void ShowIntroScreen(Graphics2D g2d) {
    
            g2d.setColor(new Color(0, 32, 48));
            g2d.fillRect(50, scrsize / 2 - 30, scrsize - 100, 50);
            g2d.setColor(Color.white);
            g2d.drawRect(50, scrsize / 2 - 30, scrsize - 100, 50);
    
            String s = "Press s to start.";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);
    
            g2d.setColor(Color.white);
            g2d.setFont(small);
            g2d.drawString(s, (scrsize - metr.stringWidth(s)) / 2, scrsize / 2);
        }
    
    
        public void DrawScore(Graphics2D g) {
            int i;
            String s;
    
            g.setFont(smallfont);
            g.setColor(new Color(96, 128, 255));
            s = "Score: " + score;
            g.drawString(s, scrsize / 2 + 96, scrsize + 16);
            for (i = 0; i < pacsleft; i++) {
                g.drawImage(pacman3left, i * 28 + 8, scrsize + 1, this);
            }
        }
    
    
        public void CheckMaze() {
            short i = 0;
            boolean finished = true;
    
            while (i < nrofblocks * nrofblocks && finished) {
                if ((screendata[i] & 48) != 0)    
                    finished = false;
                i++;
            }
    
            if (finished) {
                score += 50;
    
                if (nrofghosts < maxghosts)
                    nrofghosts++;
                if (currentspeed < maxspeed)
                    currentspeed++;
                LevelInit();
            }
        }
    
        public void Death() {
    
            pacsleft--;
            if (pacsleft == 0)
                ingame = false;
            LevelContinue();
        }
    
    
        public void moveGhosts(Graphics2D g2d) {
            short i;
            int pos;
            int count;
    
            for (i = 0; i < nrofghosts; i++) {
                if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {
                    pos =ghostx[i] / blocksize + nrofblocks * (int)(ghosty[i] / blocksize); 
                    count = 0;
                    if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) 
                        dx[count] = -1;
                        dy[count] = 0;
                        count++;
    //is the above part checking for left position?
    Java Code:
                    }
                    if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {
                        dx[count] = 0;
                        dy[count] = -1;
                        count++;
                    }
                    if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {
                        dx[count] = 1;
                        dy[count] = 0;
                        count++;
                    }
                    if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {
                        dx[count] = 0;
                        dy[count] = 1;
                        count++;
                    }
    what does this part of the code do
    Java Code:
                    if (count == 0) {
                        if ((screendata[pos] & 15) == 15) {
                            ghostdx[i] = 0;
                            ghostdy[i] = 0;
                        } else {
                            ghostdx[i] = -ghostdx[i];
                            ghostdy[i] = -ghostdy[i];
                        }
                    } else {
                        count = (int)(Math.random() * count);
                        if (count > 3)
                            count = 3;
                        ghostdx[i] = dx[count];
                        ghostdy[i] = dy[count];
                    }
    
                }
                ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
                ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
                DrawGhost(g2d, ghostx[i] + 1, ghosty[i] + 1);
    
                if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12) &&
                    pacmany > (ghosty[i] - 12) && pacmany < (ghosty[i] + 12) &&
                    ingame) {
    
                    dying = true;
                    deathcounter = 64;
    
                }
            }
        }
    till here
    Java Code:
        public void DrawGhost(Graphics2D g2d, int x, int y) {
            g2d.drawImage(ghost, x, y, this);
        }
    
    
        public void MovePacMan() {
            int pos;
            short ch;
    
            if (reqdx == -pacmandx && reqdy == -pacmandy) {
    what does this part of the code do
    Java Code:
    
                pacmandx = reqdx;
                pacmandy = reqdy;
                viewdx = pacmandx;
                viewdy = pacmandy;
            }
            if (pacmanx % blocksize == 0 && pacmany % blocksize == 0) {
                pos =
     pacmanx / blocksize + nrofblocks * (int)(pacmany / blocksize);
                ch = screendata[pos];
    what does this do?
    Java Code:
                if ((ch & 16) != 0) { 
                    screendata[pos] = (short)(ch & 15);
                    score++;
                }
    
                if (reqdx != 0 || reqdy != 0) {
                    if (!((reqdx == -1 && reqdy == 0 && (ch & 1) != 0) ||
                          (reqdx == 1 && reqdy == 0 && (ch & 4) != 0) ||
                          (reqdx == 0 && reqdy == -1 && (ch & 2) != 0) ||
                          (reqdx == 0 && reqdy == 1 && (ch & 8) != 0))) {
                        pacmandx = reqdx;
                        pacmandy = reqdy;
                        viewdx = pacmandx;
                        viewdy = pacmandy;
                    }
                }
    
                // Check for standstill
                if ((pacmandx == -1 && pacmandy == 0 && (ch & 1) != 0) ||
                    (pacmandx == 1 && pacmandy == 0 && (ch & 4) != 0) ||
                    (pacmandx == 0 && pacmandy == -1 && (ch & 2) != 0) ||
                    (pacmandx == 0 && pacmandy == 1 && (ch & 8) != 0)) {
                    pacmandx = 0;
                    pacmandy = 0;
                }
            }
            pacmanx = pacmanx + pacmanspeed * pacmandx;
            pacmany = pacmany + pacmanspeed * pacmandy;
        }
    till here
    Java Code:
    
        public void DrawPacMan(Graphics2D g2d) {
            if (viewdx == -1)
                DrawPacManLeft(g2d);
            else if (viewdx == 1)
                DrawPacManRight(g2d);
            else if (viewdy == -1)
                DrawPacManUp(g2d);
            else
                DrawPacManDown(g2d);
        }
    
        public void DrawPacManUp(Graphics2D g2d) {
            switch (pacmananimpos) {
            case 1:
                g2d.drawImage(pacman2up, pacmanx + 1, pacmany + 1, this);
                break;
            case 2:
                g2d.drawImage(pacman3up, pacmanx + 1, pacmany + 1, this);
                break;
            case 3:
                g2d.drawImage(pacman4up, pacmanx + 1, pacmany + 1, this);
                break;
            default:
                g2d.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
                break;
            }
        }
    
    
        public void DrawPacManDown(Graphics2D g2d) {
            switch (pacmananimpos) {
            case 1:
                g2d.drawImage(pacman2down, pacmanx + 1, pacmany + 1, this);
                break;
            case 2:
                g2d.drawImage(pacman3down, pacmanx + 1, pacmany + 1, this);
                break;
            case 3:
                g2d.drawImage(pacman4down, pacmanx + 1, pacmany + 1, this);
                break;
            default:
                g2d.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
                break;
            }
        }
    
    
        public void DrawPacManLeft(Graphics2D g2d) {
            switch (pacmananimpos) {
            case 1:
                g2d.drawImage(pacman2left, pacmanx + 1, pacmany + 1, this);
                break;
            case 2:
                g2d.drawImage(pacman3left, pacmanx + 1, pacmany + 1, this);
                break;
            case 3:
                g2d.drawImage(pacman4left, pacmanx + 1, pacmany + 1, this);
                break;
            default:
                g2d.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
                break;
            }
        }
    
    
        public void DrawPacManRight(Graphics2D g2d) {
            switch (pacmananimpos) {
            case 1:
                g2d.drawImage(pacman2right, pacmanx + 1, pacmany + 1, this);
                break;
            case 2:
                g2d.drawImage(pacman3right, pacmanx + 1, pacmany + 1, this);
                break;
            case 3:
                g2d.drawImage(pacman4right, pacmanx + 1, pacmany + 1, this);
                break;
            default:
                g2d.drawImage(pacman1, pacmanx + 1, pacmany + 1, this);
                break;
            }
        }
    
    
        public void DrawMaze(Graphics2D g2d) {
            short i = 0;
            int x, y;
    
            for (y = 0; y < scrsize; y += blocksize) {
                for (x = 0; x < scrsize; x += blocksize) {
                    g2d.setColor(mazecolor);
                    g2d.setStroke(new BasicStroke(2));
    
                    if ((screendata[i] & 1) != 0) // draws left
                    {
                        g2d.drawLine(x, y, x, y + blocksize - 1);
                    }
                    if ((screendata[i] & 2) != 0) // draws top
                    {
                        g2d.drawLine(x, y, x + blocksize - 1, y);
                    }
                    if ((screendata[i] & 4) != 0) // draws right
                    {
                        g2d.drawLine(x + blocksize - 1, y, x + blocksize - 1,
                                     y + blocksize - 1);
                    }
                    if ((screendata[i] & 8) != 0) // draws bottom
                    {
                        g2d.drawLine(x, y + blocksize - 1, x + blocksize - 1,
                                     y + blocksize - 1);
                    }
                    if ((screendata[i] & 16) != 0) // draws point
                    {
                        g2d.setColor(dotcolor);
                        g2d.fillRect(x + 11, y + 11, 2, 2);
                    }
                    i++;
                }
            }
        }
    
        public void GameInit() {
            pacsleft = 3;
            score = 0;
            LevelInit();
            nrofghosts = 6;
            currentspeed = 3;
        }
    
    
        public void LevelInit() {
            int i;
            for (i = 0; i < nrofblocks * nrofblocks; i++)
                screendata[i] = leveldata[i];
    
            LevelContinue();
        }
    
    
    
        public void LevelContinue() {
            short i;
            int dx = 1;
            int random;
    
            for (i = 0; i < nrofghosts; i++) {
                ghosty[i] = 4 * blocksize;
                ghostx[i] = 4 * blocksize;
                ghostdy[i] = 0;
                ghostdx[i] = dx;
                dx = -dx;
                random = (int)(Math.random() * (currentspeed + 1));
                if (random > currentspeed)
                    random = currentspeed;
                ghostspeed[i] = validspeeds[random];
            }
    
            pacmanx = 7 * blocksize;
            pacmany = 11 * blocksize;
            pacmandx = 0;
            pacmandy = 0;
            reqdx = 0;
            reqdy = 0;
            viewdx = -1;
            viewdy = 0;
            dying = false;
        }
    
        public void GetImages()
        {
    
          ghost = new ImageIcon("ghost.png").getImage();
          pacman1 = new ImageIcon("pacman.png").getImage();
          pacman2up = new ImageIcon("up1.png").getImage();
          pacman3up = new ImageIcon("up2.png").getImage();
          pacman4up = new ImageIcon("up3.png").getImage();
          pacman2down = new ImageIcon("down1.png").getImage();
          pacman3down = new ImageIcon("down2.png").getImage(); 
          pacman4down = new ImageIcon("down3.png").getImage();
          pacman2left = new ImageIcon("left1.png").getImage();
          pacman3left = new ImageIcon("left2.png").getImage();
          pacman4left = new ImageIcon("left3.png").getImage();
          pacman2right = new ImageIcon("right1.png").getImage();
          pacman3right = new ImageIcon("right2.png").getImage();
          pacman4right = new ImageIcon("right3.png").getImage();
    
        }
    
        public void paint(Graphics g)
        {
          super.paint(g);
    
          Graphics2D g2d = (Graphics2D) g;
    
          g2d.setColor(Color.black);
          g2d.fillRect(0, 0, d.width, d.height);
    
          DrawMaze(g2d);
          DrawScore(g2d);
          DoAnim();
          if (ingame)
            PlayGame(g2d);
          else
            ShowIntroScreen(g2d);
    
          g.drawImage(ii, 5, 5, this);
          Toolkit.getDefaultToolkit().sync();
          g.dispose();
        }
    
        class TAdapter extends KeyAdapter {
            public void keyPressed(KeyEvent e) {
    
              int key = e.getKeyCode();
    
              if (ingame)
              {
                if (key == KeyEvent.VK_LEFT)
                {
                  reqdx=-1;
                  reqdy=0;
                }
                else if (key == KeyEvent.VK_RIGHT)
                {
                  reqdx=1;
                  reqdy=0;
                }
                else if (key == KeyEvent.VK_UP)
                {
                  reqdx=0;
                  reqdy=-1;
                }
                else if (key == KeyEvent.VK_DOWN)
                {
                  reqdx=0;
                  reqdy=1;
                }
                else if (key == KeyEvent.VK_ESCAPE && timer.isRunning())
                {
                  ingame=false;
                }
                else if (key == KeyEvent.VK_PAUSE) {
                    if (timer.isRunning())
                        timer.stop();
                    else timer.start();
                }
              }
              else
              {
                if (key == 's' || key == 'S')
              {
                  ingame=true;
                  GameInit();
                }
              }
          }
    
              public void keyReleased(KeyEvent e) {
                  int key = e.getKeyCode();
    
                  if (key == Event.LEFT || key == Event.RIGHT || 
                     key == Event.UP ||  key == Event.DOWN)
                  {
                    reqdx=0;
                    reqdy=0;
                  }
              }
          }
    
        public void actionPerformed(ActionEvent e) {
            repaint();  
        }
    
    
    }
    Last edited by tarareddy; 11-04-2012 at 08:03 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: pacman code

    Wow. That's a lot of unformatted code to dump into the forum. Consider formatting the code by using [code][/code] tags (see the link in my signature below). Consider testing small bits of the code in your own programs to see what it does. Consider writing a similar program yourself to gain a better understanding of the issues involved.

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: pacman code

    I'm w/ Fubarable on this... but I can help a bit (I think)

    pacmananimpos = pacmananimpos + pacanimdir; what are we doing here
    This appears to be updating the sprite location for the Character Animation. A sprite sheet is an image made up of smaller images (typically the same size, but doesn't have to be) so that the larger image can be loaded, then display a certain part of the image for each movement. (Someone more game oriented will be able to explain this better, but do a search for "sprite sheet" you should be able to quickly see what I'm alluding to).

    if ((screendata[i] & 48) != 0) //why r we doing bit and operation ?is it checking for 6th bit if so y?
    ... Not quite. 48 = 0011 0000
    At the top 16 = regular pellet... I'm assuming 32 = power pellet... so... This is checking to make sure all the squares are empty.

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: pacman code

    Actually... Here's a sprite sheet... in setting up the dog's run animation, as the dog is moving, you'd increase a counter from 0 to 8 and set the graphic accordingly. This image is 552x300... each dog then is 184x100 so the first frame would be 184x100 starting at 0x0, second frame would be 184x100 starting at 184x0 ... fourth frame would be 184x100 starting at 0x100...

    pacman code-dogrun.png

    (I know this isn't really new java stuff, but was bored and thought a couple people might find it interesting.)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default off topic

    A famous variant of the dog picture is this series of pictures of a horse galloping taken by Eadweard Muybridge. He was commissioned to do this by Leland Stanford (eponymous founder of the university) in order to settle the question of whether all four legs were off the ground at the same time. (In fact the original work was done with a trotter, but only woodcuts survive. The galloping horse - and the dog? - show the legs together when they're off the ground rather than splayed apart, fore and aft, as was popularly imagined.)

    Moving pictures arose as a result.

  6. #6
    tarareddy is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: pacman code

    @Fubarable
    oh thank you i am new to this hence i didnt know
    Last edited by tarareddy; 11-04-2012 at 07:39 AM.

  7. #7
    tarareddy is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: pacman code

    @SJF thank u :)

  8. #8
    uchihaharvey is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: pacman code

    why always put an error . anyone can give me full code for pacman plsss. im just new here

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: pacman code

    Quote Originally Posted by uchihaharvey View Post
    anyone can give me full code
    This is a forum, not a code factory. Recommended reading: How to ask questions the smart way

    Also, don't resurrect old dead threads, and especially not to ask for code.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Pacman Game
    By Hollowsoul in forum Java 2D
    Replies: 13
    Last Post: 07-16-2011, 11:31 PM
  2. Making PacMan game, need help!
    By Mondos in forum Java Gaming
    Replies: 14
    Last Post: 06-03-2011, 03:24 PM
  3. pacman 2d array help please
    By Java26 in forum Advanced Java
    Replies: 2
    Last Post: 05-11-2011, 05:55 PM
  4. Help implementing Pacman
    By scheng12 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 10:26 PM
  5. need help with pacman game!!
    By Newbie_Javaer in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •