Results 1 to 2 of 2
  1. #1
    beechy34 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default java bounding box

    hi im trying to make a packman style game for an assignment but the problem im having is i cant remove the munchies which the monster is supposed to eat,
    im trying to make an array that will remove the munchies. ive made an array called munchie that i want setting to 1 if it is to be draw and if it is 0 then the munchie will disapear, ive highlighted in green the bit im having problems with.

    i hope im making sence

    please help thankyou

    Java Code:
    import javax.swing.*;
    import java.applet.AudioClip;
    import java.awt.*;
    
    /**
     *
     * <p>Title: MonsterGame</p>
     * <p>An interactive animation with Munchies and user-controlled Monster</p>
     * <p>Copyright: Copyright (c) 2005</p>
     * @author Cathy French
     * @version 1.2   September 2006
     */
    public class MonsterGame extends ICGGSPanel
    {
        // game constants
        private final int NUM_MUNCHIES = 5;   // the number of munchies to be displayed
    
        // game information
        private  int score;  // player score (not currently used)
        private  int boardWidth, boardHeight;  // game board dimensions
    
        // Munchie data
        private  int[] munchieXPos;  // x coordinate of top left corner of each munchie image
        private int[] munchieYPos;   // y coordinate of top left corner of each munchie image
        private int[] munchieAnimationCount;  // current animation counter for each munchie
        private  Image munchieImage1, munchieImage2;  // the images to display
        private int munchieWidth, munchieHeight;  // munchie dimensions
    
        
        // Monster data
        private  int monsterXPos, monsterYPos;  // coordinates of top left corner of monster
        private int monsterSize;  // size of monster
        private  int monsterSpeed;  // monster speed in pixels per frame
        private  Color monsterColour;  
        private  int monsterDirection;  // direction monster is moving
        private  int monsterAnimationCount;  // animation counter for monster
        private AudioClip monsterBumpWallSound;  // sound to play when monster hits a wall
    
        
        
        int[]munchie;
    
        int arcAngle;
        int startAngle = 0;
        int startAngle2;
        /**
         * Constructor - creates a game using the two images and audio clip
         * @param image1 Image - the first image for the munchies
         * @param image2 Image - the second image for the munchies
         * @param theAudio AudioClip - sound to be played when the monster hits a wall
         */
        public MonsterGame(Image image1, Image image2, AudioClip theAudio)
        {
            // initialise Munchie information
            // create the arrays, need one "slot" for each munchie
            munchieXPos = new int[NUM_MUNCHIES];
            munchieYPos = new int[NUM_MUNCHIES];
            munchieAnimationCount = new int[NUM_MUNCHIES];
            // store the munchie images
            munchieImage1 = image1;
            munchieImage2 = image2;
            // the munchie is the same size as the loaded image
            munchieWidth = image1.getWidth(null);
            munchieHeight = image1.getHeight(null);
            
    
            // initialise the Monster information
            monsterColour = Color.red;
            monsterBumpWallSound = theAudio;
            monsterSize = 30;
            
            // initialise background colour
            this.setBackground(Color.WHITE);
        }
    
        /**
         * Use to reset the game
         * sets score to zero
         * sets up Monster and munchies in random positions
         */
    
        public void resetGame()
        {
            // make the game board the same size as the panel
            boardWidth = this.getWidth();
            boardHeight = this.getHeight();
            score = 0;
            // reset the Monster to a random position and direction
            monsterXPos = (int)(Math.random() * (boardWidth - monsterSize));
            monsterYPos = (int)(Math.random() * (boardHeight - monsterSize));
            monsterDirection = (int)(Math.random() * 4) + 1;
            monsterSpeed = 5;
            monsterAnimationCount = 0;
    
            // reset all the munchies to random positions and different animation counts
            for (int i = 0; i < NUM_MUNCHIES; i++) {
                munchieXPos[i] = (int)(Math.random() * (boardWidth - munchieWidth));
                munchieYPos[i] = (int)(Math.random() * (boardHeight - munchieHeight));
                munchieAnimationCount[i] = i % 10;
            }
    
        }
    
        /**
         * overrides superclass method
         * processes user input
         * @param input int - code for the lastest user input
         */
        public void processInput(int input)
        {
             if (input > 0 && input < 5)  // if the input is a valid direction
                monsterDirection = input;  // set the current direction to the input
        }
    
        /**
         * overrides superclass method
         * updates the game objects
         */
        public void update()
        {
            // update moster direction and animation
             switch (monsterDirection)
            {
              case Input.RIGHT:
                  startAngle2 = startAngle = 10;
                  // monster is moving right, increase its X-coordinate
                  monsterXPos = monsterXPos + monsterSpeed;
                  if (monsterXPos > (boardWidth - monsterSize)) // has the monster hit the right wall?
                  {
                      monsterXPos = boardWidth - monsterSize;  // move it back to just touching right wall
                      monsterBumpWallSound.play();  // play an appropriate sound
                      
                  }
                  
                  monsterDirection = Input.NONE;
                  
                  break;
    
                  case Input.LEFT:
                      startAngle2 = startAngle = 185;
                      monsterXPos = monsterXPos - monsterSpeed;
    if (monsterXPos < 0) // has the monster hit the right wall?
                  {
                      monsterXPos = 0;  // move it back to just touching right wall
                      monsterBumpWallSound.play();  // play an appropriate sound
                  }
                      
                      monsterDirection = Input.NONE;
                      
                      break;
    
                      case Input.DOWN:
                          startAngle2 = startAngle = 280;
                  //monster is moving right, increase its X-coordinate
                  monsterYPos = monsterYPos + monsterSpeed;
                  if (monsterYPos > (boardWidth - monsterSize)) // has the monster hit the right wall?
                  {
                      monsterYPos = boardWidth - monsterSize;  // move it back to just touching right wall
                      monsterBumpWallSound.play();  // play an appropriate sound
                  }
                  
                  monsterDirection = Input.NONE;
                  
                   break;
    
                  case Input.UP:
                      startAngle2 = startAngle = 100;
                      monsterYPos = monsterYPos - monsterSpeed;
                            if (monsterYPos < 0) // has the monster hit the right wall?
                                {
                                    monsterYPos = 0;  // move it back to just touching right wall
                                    monsterBumpWallSound.play();  // play an appropriate sound
                                }
                      
                      monsterDirection = Input.NONE;
                      
                      
                  // to do - fill in appropriate code for other directions
            }
    
    
    
    
             if (monsterAnimationCount < 5)
            {
    
                // for frames 0 to 4, his mouth gradually closes
                arcAngle = 310 + 10 * monsterAnimationCount;
                startAngle2 = startAngle2 - 5*monsterAnimationCount;
            }
            else
            {
    
                 //for frames 5 to 9, it opens again
                arcAngle = 310 + (10  - monsterAnimationCount) * 10;
                startAngle2 = startAngle2 - (10 - monsterAnimationCount) * 5;
            }
    
    
    
    
    
             // increment the monster animation counter 
           monsterAnimationCount++;
           if (monsterAnimationCount >= 10)  // when the counter gets to 10, set it back to 0
               monsterAnimationCount = 0;
           
           // update each munchie animation
            for (int i = 0; i < NUM_MUNCHIES; i++)
            {
               // increment the animation counter 
                munchieAnimationCount[i]++;
                if (munchieAnimationCount[i] >= 10) // when the counter gets to 10, set it back to 0
                    munchieAnimationCount[i] = 0;
            }
        }
    
    
        /**
         * called by system after repaint() call (or Applet moved or resized)
         * @param g Graphics  object for painting on
         */
        public boolean collisionDetection(int xPos1, int yPos1, int Height1, int Width1, int xPos2, int yPos2, int Height2, int Width2)
    {
    
    int bottom1, bottom2;
    int top1, top2;
    int left1, left2;
    int right1,right2;
    
    left1= xPos1;
    left2 =xPos2;
    right1 = xPos1+Width1;
    right2 = xPos2+Width2;
    top1 = yPos1;
    top2 = yPos2;
    bottom1 = yPos1+Height1;
    bottom2 = yPos2+Height2;
    
    if (bottom1 < top2) return(true);
    if (top1 > bottom2) return(true);
    
    if (right1 < left2) return(true);
    if (left1 > right2) return(true);
    
    return (false);
    }
    
    
    public void paintComponent(Graphics g)
    
    {
    
    super.paintComponent(g); // paint the default panel
    
    // paint each of the Munchies in turn
    //if(munchie > 0 )
    {
    
    for (int i = 0; i < NUM_MUNCHIES; i++)
        
      [COLOR="lime"]if(munchie[i]>1 )[/COLOR]
    
    {
    Image im; // this is the image to be displayed
    if (munchieAnimationCount[i] < 5)
    {
    im = munchieImage1;
    }
    else
    {
    // display the second image when the counter is between 5 and 9
    im = munchieImage2;
    }
    
    // draw the image in the correct position
    g.drawImage(im, munchieXPos[i], munchieYPos[i], null);
    }
    
    g.setColor(monsterColour);
    g.fillArc(monsterXPos, monsterYPos, monsterSize, monsterSize, startAngle, arcAngle);
    g.setColor(Color.BLACK);
    g.drawRect(0, 0, boardWidth-1, boardHeight-1);
    
        
    
    //then to implement:
    for(int i=0;i<NUM_MUNCHIES;i++)
    
    {
    boolean Flag = collisionDetection(monsterXPos, monsterYPos, monsterSize,monsterSize ,munchieXPos[i],munchieYPos[i],20,20);
    
    if (Flag==false)
    {
    [COLOR="Lime"]munchie[i]=0;[/COLOR]
    
    score=score+100;
    Flag=true;
    }
    }
    }
    }
    }

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

    Default

    I wonder if you shouldn't be using an array of Munchie but instead an arraylist of Munchie. If a Munchie is eaten, simply remove him from the list.

    One other concern I have is that you seem to be maintaining parallel arrays here of Munchie, Munchie x position, Munchi y position, and Munchie animation count. If your Munchie class can't hold the three int values, then perhaps you want to create a wrapper class that holds a Munchie and the three ints. Then your app can have an ArrayList of this wrapper instead and won't have to worry about deleting or adding items to four separate arrays.

Posting Permissions

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