Help With pacman Game!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help With pacman Game!!

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    Help With pacman Game!!

    Hey whats up i haev just started my pacman game as an applet in Java. I'l give u some background info on how i am doing it. I made an array and assigned numebers inside the array to images
    IE.
    //1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    {05,05,05,05,05,05,05,04,05,05,05,05,05,05,05},//1
    {05,03,04,04,04,04,04,03,04,04,04,04,04,03,05},//2
    {05,04,05,05,05,04,05,05,05,05,04,05,05,04,05},//3 {05,04,05,04,04,04,04,04,04,04,04,04,05,04,05},//4
    {05,04,05,04,05,05,05,04,05,05,05,04,05,04,05},//5 {05,04,05,04,05,04,04,04,04,04,05,04,05,04,05},//6
    {05,04,05,04,05,04,05,05,05,04,05,04,05,04,05},//7
    {04,04,04,04,04,04,05,03,05,01,05,04,04,04,05},//8
    {05,04,05,04,05,04,05,05,05,04,05,04,05,04,05},//9
    {05,04,05,04,05,04,04,04,04,04,05,04,05,04,04},//10
    {05,04,05,04,05,05,05,04,05,05,05,04,05,04,05},//11
    {05,04,05,04,04,04,04,04,04,04,04,04,05,04,05},//12
    {05,04,05,05,05,04,05,05,05,05,04,05,05,04,05},//13
    {05,03,04,04,04,04,04,03,04,04,04,04,04,03,05},//14
    {05,05,05,05,05,05,05,04,05,05,05,05,05,05,05}//15


    for(int y=0; y<board[x].length; y++){
    drawPic = getImage(getCodeBase(), "pacman" + board [x][y] + ".jpg");
    drawX = x*40;
    drawY = y*40;
    g.drawImage(drawPic,drawX,drawY, this);


    Now im going to use keyevents to move pacman and im going to do it like this.
    I will have keypressed and when this happens depending on what key was pressed i will + or - to the current column pacman is in after!! testing if the next space is not a wall. Now i need a global variable to track pacmans current possitio and this will be an int. I also need 2 ints to track his row/column il call them pacRow, pacColumn, and pacman il call pacMan.

    I am stumped though as to how to add to columns and rows inside an array. Can someone please help me with this.!! i haev all the logic i just need the code.


    thanks alot

    --K

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    I am stumped though as to how to add to columns and rows inside an array. Can someone please help me with this.!! i haev all the logic i just need the code.
    I'm not sure exactly what you mean here. I'm not what adding the columns and rows in the array is for. If you want to keep track of pacmans position you just need two variables, say X and Y. You don't need an array.

    If I was doing it i'd have a class called PacMan and it would have those two variables (along with any other information on PacMan), and when he moved i'd just change the variables accordingly.

    If you explain better what adding the numbers in the array is for I might be able to help more.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Yes.

    A PackMan and a PackManGrid, and a little more. Here I am just
    presenting some loose thought on how to go about this.

    e.g.
    Code:
    class PackManGrid extends JPanel implements KeyListener {
        public static int [][] gridImgIndex={
          // your image index matrix
        };
        PackMan pm=null;
        ArrayList gridImages=null; 
        public PackManGrid  (ArrayList gridImages /* ?? */) {
          this.gridImages=gridImages;
          pm=new PackMan(this);
          initiatePackMan(); // ?
          addKeyListener(this);
        }
        public void keyPressed(KeyEvent ev) {
           GridPos gp=getNewGridPos(ev); // ?
           if (pm.goto(gp) {
              // do moved-logic
           } else  {
              // do atWall logics
           } 
        }
    }
    class GridPos {
      public int x=-1;
      public int y=-1;
    }
    class PackMan extends GridPos {
      PackManGrid pmg=null;
      .
      .
      public PackMan (PackManGrid pmg/*and probably more params*/) {
        .
        .
        this.pmg=pmg;
      }
      public boolean goto(GridPos newPos) {
        if (pmg.isValid(newPos) {
          this.x=newPos.x;
          this.y=newPos.y;
          pmg.repaint();  //e.g.  if PackManGrid is implemented as a JPanel extension 
          return true;
        }
         return false;
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center