Results 1 to 9 of 9
  1. #1
    alex7841 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Creating a Pac-Man Game

    I am trying to make a Pac Man game and I'm stuck. I am having trouble figuring out the 2D array, and was wondering where to go from here.

    This is what I have so far:

    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.io.*;
    import java.net.*;
    
    public class PacMan extends Applet
    {
    	String fileToRead = "maze.txt";
    	StringBuffer strBuff;
    	char map;
    	char[] mapArray;
    	Image[] images = new Image[12];
    	int mapWidth = 28, mapHeight = 31;
        URL base; 
        MediaTracker mt;
        
        public void init()
        {    	
        	mt = new MediaTracker(this);
        	try
        	{
        		base = getDocumentBase();
        	}
        	catch (Exception e) {}
        	
        	//get Images
        	images[0] = getImage(base,"Pac-Images/Tile-Space.JPG");
        	images[1] = getImage(base,"Pac-Images/Tile-Wall.JPG");
        	images[2] = getImage(base,"Pac-Images/Tile-Dot.JPG");
        	images[3] = getImage(base,"Pac-Images/Tile-Horizontal.JPG");
        	images[4] = getImage(base,"Pac-Images/Tile-Vertical.JPG");
        	images[5] = getImage(base,"Pac-Images/Tile-TopRight.JPG");
        	images[6] = getImage(base,"Pac-Images/Tile-TopLeft.JPG");
        	images[7] = getImage(base,"Pac-Images/Tile-BotRight.JPG");
        	images[8] = getImage(base,"Pac-Images/Tile-BotLeft.JPG");
        	images[9] = getImage(base,"Pac-Images/Tile-PowerDot1.JPG");
        	images[10] = getImage(base,"Pac-Images/Tile-PowerDot2.JPG");
        	images[11] = getImage(base,"Pac-Images/Tile-PowerDot3.JPG");
        	
        	//set Image ID
        	mt.addImage(images[0],0);
        	mt.addImage(images[1],1);
        	mt.addImage(images[2],2);
        	mt.addImage(images[3],3);
        	mt.addImage(images[4],4);
        	mt.addImage(images[5],5);
        	mt.addImage(images[6],6);
        	mt.addImage(images[7],7);
        	mt.addImage(images[8],8);
        	mt.addImage(images[9],9);
        	mt.addImage(images[10],10);
        	mt.addImage(images[11],11);
        	
        	String prHtml = this.getParameter("fileToRead");
        	if (prHtml != null) fileToRead  = new String(prHtml);
        	readMap();
        	
        	//mapArray = strBuff.toString().toCharArray();
        	//mapArray = new char[map.length()];
        	//map.getChars(0, map.length(),mapArray,0);
        	
        	try
        	{
        		mt.waitForAll();
        	}
        	catch (InterruptedException e) {}
        }
        
        public void readMap() //throws MalformedURLException
        {
        	String line;
        	URL url = null;
        	try
        	{
        		url = new URL(getCodeBase(), fileToRead);
        	}
        	catch(MalformedURLException e){}
        	
        	try
        	{
        		InputStream in = url.openStream();
        		BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        		strBuff = new StringBuffer();
        		while((line = bf.readLine()) != null)
        		{
        			strBuff.append(line + "\n");
        		}
        	}
        	catch(IOException e)
        	{
        		e.printStackTrace();
        	}
        }
       
        public void paint(Graphics g)
        {
        	
        	for (int y = 0; y < mapHeight; y++)
        		for (int x = 0; x < mapWidth; x++)
        		{
        			char c = map;
        			int im;
        			switch(c)
        			{
        				case ' ' : im = 0; break;
        				case 'X' : im = 1; break;
        				case '.' : im = 2; break;
        				case '-' : im = 3; break;
        				case '|' : im = 4; break;
        				case '[' : im = 5; break;
        				case ']' : im = 6; break;
        				case '(' : im = 7; break;
        				case ')' : im = 8; break;
        				case 'O' : im = 9; break;
        				default  : im = 0;
        			};
        			g.drawImage(images[im],x*16,y*16,this);
        		}
        }
    }
    Last edited by Norm; 02-29-2012 at 02:59 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Creating a Pac-Man Game

    where to go from here.
    Big question.
    Where are you having problems?
    Do you have any specific questions?

  3. #3
    alex7841 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating a Pac-Man Game

    Yes, I am having trouble actually drawing the map onto the screen. It will only draw one image for the entire for() loop.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Creating a Pac-Man Game

    Go through the logic on switch block, weather it prevent adding images to the panel.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Creating a Pac-Man Game

    It will only draw one image for the entire for() loop.
    How does the program chose different images in the for loop? What controls which image is drawn?

  6. #6
    alex7841 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating a Pac-Man Game

    Sorry for not being clear before. I need to take the "maze.txt" file, which is being read in, and put it into a 2D char array. In the for() loop, it should say
    char c = mapArray[x][y].

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Creating a Pac-Man Game

    Did that change fix your problem?

  8. #8
    alex7841 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating a Pac-Man Game

    the problem is that i don't know how to go about putting the "maze.txt" file into a 2D array...once i have that working, it should compile. Right now im getting a null pointer exception for char c = mapArray[x][y]

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Creating a Pac-Man Game

    how to go about putting the "maze.txt" file into a 2D array.
    Take a piece of paper and draw out the contents of the maze.txt file and assign to each item in it the row,col that you want it to go into in the 2D array.
    When you have that, you should be able to read a line from the file, get each item from the line and store it where it goes in the array.
    You'll have the line number and the item number on the line that can be converted to the x,y for indexing into the array.

Similar Threads

  1. Creating a game with java.
    By GameCoderKata in forum New To Java
    Replies: 1
    Last Post: 12-19-2011, 01:17 AM
  2. Creating a flying game
    By george222 in forum Java Gaming
    Replies: 1
    Last Post: 02-11-2011, 10:21 AM
  3. Creating a Card Game
    By Kidd91 in forum New To Java
    Replies: 1
    Last Post: 12-24-2010, 02:25 PM
  4. Help creating a Masterminds game.
    By viper98 in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 04:13 AM
  5. need help creating my Tic Tac Toe game!
    By bobmasta5 in forum New To Java
    Replies: 6
    Last Post: 12-09-2008, 05:45 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
  •