Results 1 to 9 of 9
Thread: Creating a Pac-Man Game
- 02-29-2012, 02:34 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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
- 02-29-2012, 02:59 AM #2
Re: Creating a Pac-Man Game
Big question.where to go from here.
Where are you having problems?
Do you have any specific questions?
- 02-29-2012, 04:53 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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.
- 02-29-2012, 05:03 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Creating a Pac-Man Game
Go through the logic on switch block, weather it prevent adding images to the panel.
- 02-29-2012, 01:19 PM #5
Re: Creating a Pac-Man Game
How does the program chose different images in the for loop? What controls which image is drawn?It will only draw one image for the entire for() loop.
- 03-01-2012, 05:37 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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].
- 03-01-2012, 01:21 PM #7
Re: Creating a Pac-Man Game
Did that change fix your problem?
- 03-01-2012, 08:16 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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]
- 03-01-2012, 08:21 PM #9
Re: Creating a Pac-Man Game
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.how to go about putting the "maze.txt" file into a 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
-
Creating a game with java.
By GameCoderKata in forum New To JavaReplies: 1Last Post: 12-19-2011, 01:17 AM -
Creating a flying game
By george222 in forum Java GamingReplies: 1Last Post: 02-11-2011, 10:21 AM -
Creating a Card Game
By Kidd91 in forum New To JavaReplies: 1Last Post: 12-24-2010, 02:25 PM -
Help creating a Masterminds game.
By viper98 in forum New To JavaReplies: 4Last Post: 11-09-2009, 04:13 AM -
need help creating my Tic Tac Toe game!
By bobmasta5 in forum New To JavaReplies: 6Last Post: 12-09-2008, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks