Results 1 to 3 of 3
  1. #1
    scheng12 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Help implementing Pacman

    Ok so I coded a text version initially and then we had to convert it to GUI.
    There are two classes that I'm having problem with, the World class, and the Driver class that is supposed to run everything. I'll include all of my classes in case someone wants to try to run it or something.

    World:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.Random;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class World extends JFrame implements ActionListener, KeyListener{
    	private Cell [][] world;
    	private int pRow, pCol;
    	private Ghost g1;
    	private Ghost g2;
    	private Ghost g3;
    	private Ghost g4;
    	private int size=10;
    	private int points = 0;
    	private int immunity = 0;
    	private boolean immune = false;
    	private JLabel [][] board;
    	JLabel scoreLabel;
    	private static char c;
    	
    	private int pellets = size * size ;
    //Constructor for World class.
    	public World() {
    		createWorld();
    		Display();
    	}
    	//Creates the Pacman World out of the Cell class. Forms a 10X10 2D array
    	private void createWorld(){
    		world = new Cell [size][size];
    		for (int i=0; i<size; i++) {
    			for (int j=0; j<size; j++){
    				world [i][j] = new Cell();
    
    			}
    		}
    		Random r = new Random();
    		//Randomly finds a spot for Pacmon and the Ghost and places them on the board
    		pRow = r.nextInt(size);
    		pCol = r.nextInt(size);
    		g1 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		g2 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		g3 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		g4 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		world[g1.getRow()][g1.getCol()].set('G');
    		world[g2.getRow()][g2.getCol()].set('G');
    		world[g3.getRow()][g3.getCol()].set('G');
    		world[g4.getRow()][g4.getCol()].set('G');
    		world [5][6].setIPellet();
    		world [2][3].setIPellet();
    		world [8][3].setIPellet();
    		world [3][9].setIPellet();
    		world[pRow][pCol].set('P');
    	}
    	//Prints the board so that the user can see it
    	public void printBoard() {
    		for (int i=0; i<size; i++) {
    			for (int j=0; j<size; j++){
    				System.out.print(world[i][j].get());
    			}
    			System.out.println();
    			
    		}
    		System.out.println("Points: " + points);
    	}
    	//Input method so that the user can tell pacman what to do. This was taken from one of the modules
    	private String input(){
    		System.out.print ("Enter your next move: ");
    		String input_line = "";
    	    try {
    	      InputStreamReader isr = new InputStreamReader (System.in);
    	      LineNumberReader lr = new LineNumberReader (isr);
    
    	      input_line = lr.readLine ();
    
    	    }
    	    catch (IOException e) {
    	      // If there was a problem...
    	      System.out.println (e);
    	   
    
    	}
    	    return input_line;
    
    
    	}
    	//Checks if all the pellets were "eaten" and declares a victory if so.
    	private boolean checkWin() {
    		if (points == pellets){
    			System.out.println("You Win!");
    			System.exit(0);
    		}
    		if( immune == false) {
    		if (g1.getRow()==pRow && g1.getCol() == pCol || g2.getRow() == pRow && g2.getCol() == pCol || g3.getRow() == pRow && g3.getCol() == pCol || g4.getRow()==pRow &&g4.getCol()==pCol) {
    			System.out.println("You Lose!");
    			System.exit(0);
    		}
    		}
    		return false;
    	}
    	//Checks if a move is valid and not going off of the board.
    	private boolean checkMove(int row, int col) {
    		if (row<0 || row > size-1 || col < 0 || col > size-1) {
    			return false;
    		}
    		return true;
    	}
    	//Randomly moves the ghost in a direction selected by the number generator. If the Ghost happens upon Pacman, the game is over.
    	
    	
    	//Up = W, Left = A, Right = D, Down = X, NorthW = Q, NorthE = E, SouthW= Z, SouthE = C
    	//Directions to actually move pacman. Each if statement incorporates the checkmove() method to see if it can continue
    	public void eatPellet() {
    		if (world[pRow-1][pCol].get() == 'X'){
    			points++;
    			
    		
    		}
    		if (world[pRow-1][pCol].get() == 'I') {
    			immune = true;
    			immunity = 10;
    			points++;
    		}
    	}
    	
    	private boolean movePacman(String m){
    		
    		
    		if (m.equals("W")|| m.equals("w")) {
    			if(checkMove(pRow-1, pCol)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow-1][pCol].set('P');
    			world[pRow][pCol].set('O');
    			pRow = pRow -1;
    			return true;
    		}
    		
    		if (m.equals("X")|| m.equals("x")) {
    			if(checkMove(pRow+1, pCol)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow+1][pCol].set('P');
    			world[pRow][pCol].set('O');
    			pRow = pRow +1;
    			return true;
    		}
    		if (m.equals("A")|| m.equals("a")) {
    			if(checkMove(pRow, pCol-1)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow][pCol-1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol -1;
    			return true;
    		}
    		
    		if (m.equals("D")|| m.equals("d")) {
    			if(checkMove(pRow, pCol+1)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow][pCol+1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol +1;
    			return true;
    		}
    		
    		if (m.equals("Q")|| m.equals("q")) {
    			if(checkMove(pRow-1, pCol-1)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow-1][pCol-1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol -1;
    			pRow = pRow - 1;
    			return true;
    		}
    		if (m.equals("e")|| m.equals("E")) {
    			if(checkMove(pRow-1, pCol+1)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow-1][pCol+1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol + 1;
    			pRow = pRow - 1;
    			return true;
    		}
    		if (m.equals("Z")|| m.equals("z")) {
    			if(checkMove(pRow+1, pCol-1)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow+1][pCol-1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol -1;
    			pRow = pRow + 1;
    			return true;
    		}
    		if (m.equals("C")|| m.equals("c")) {
    			if(checkMove(pRow+1, pCol+1)==false) {
    				System.out.println ("Move Invalid");
    				return false;
    				
    			}
    			eatPellet();
    			world[pRow+1][pCol+1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol +1;
    			pRow = pRow + 1;
    			return true;
    		}
    		
    		
    		else {
    			System.out.println ("Wrong Input");
    			return false;
    		}
    		
    		
    		
    	}
    	//The play method that begins the game. While the checkwin() method is false, the game continues to run.
    	public void play(String m) {
    	
    		boolean validMovePacman;
    		boolean validMoveGhost;
    		
    	
    		
    		while (true) {
    			printBoard();
    			updateDisplay();
    			validMovePacman = movePacman(m);	
    			
    			while (!validMovePacman) {
    				validMovePacman = movePacman(m);
    			}
    			if (immunity == 0 ) {
    				immune = false;
    			}
    			if (immune == true) {
    				immunity--;
    			}
    			checkWin();
    			world[g1.getRow()][g1.getCol()].set(world[g1.getRow()][g1.getCol()].getOldValue());
    			
    			validMoveGhost = g1.moveGhost();
    			while (!validMoveGhost) {
    				validMoveGhost = g1.moveGhost();
    			}
    			world[g1.getRow()][g1.getCol()].set('G');
    			world[g2.getRow()][g2.getCol()].set(world[g2.getRow()][g2.getCol()].getOldValue());
    			validMoveGhost = g2.moveGhost();
    			while (!validMoveGhost) {
    				validMoveGhost = g2.moveGhost();
    			}
    			world[g2.getRow()][g2.getCol()].set('G');
    			
    			world[g3.getRow()][g3.getCol()].set(world[g3.getRow()][g3.getCol()].getOldValue());
    			validMoveGhost = g3.moveGhost();
    			while (!validMoveGhost) {
    				validMoveGhost = g3.moveGhost();
    			}
    
    			world[g3.getRow()][g3.getCol()].set('G');
    
    			world[g4.getRow()][g4.getCol()].set(world[g4.getRow()][g4.getCol()].getOldValue());
    			validMoveGhost = g4.moveGhost();
    			while (!validMoveGhost) {
    				validMoveGhost = g4.moveGhost();
    			}
    
    			world[g4.getRow()][g4.getCol()].set('G');
    			checkWin();
    			
    		}
    		
    		
    	}
    	private void Display() {
    		this.setTitle("Pacman");
    		this.setResizable(false);
    		this.setSize(500, 500);
    		
    		ImageIcon pacmanIcon = createImageIcon("http://www.java-forums.org/images/pacman.gif","");
    		this.addKeyListener(this);
    		Container cPane = this.getContentPane();
    		cPane.setLayout (new BorderLayout());
    		
    		JPanel scorePanel = new JPanel();
    		scorePanel.setBackground(Color.black);
    		cPane.add(scorePanel, BorderLayout.NORTH);
    		scoreLabel = new JLabel("Score: "+ points);
    		scoreLabel.setForeground(Color.WHITE);
    		scorePanel.add(scoreLabel);
    		
    		JPanel startPanel = new JPanel();
    		startPanel.setBackground(Color.black);
    		cPane.add(startPanel, BorderLayout.SOUTH);
    		JButton startButton = new JButton("Start");
    		startButton.addActionListener(this);
    		startPanel.add(startButton);
    		JPanel boardPanel = new JPanel();
    		boardPanel.setBackground(Color.black);
    		boardPanel.setLayout(new GridLayout(size, size));
    		cPane.add(boardPanel, BorderLayout.CENTER);
    		board = new JLabel [size][size];
    		
    		for (int i = 0; i < size; i++) {
    			for (int j = 0; j < size; j++) {
    				
    				board[i][j] = new JLabel("X");
    				board[i][j].setForeground(Color.white);
    				boardPanel.add(board[i][j]);
    			}
    		}
    		
    		this.setVisible(true);
    	}
    	private void updateDisplay() {
    		for (int i=0; i<size; i++) {
    			for (int j=0; j<size; j++){
    				board[i][j].setText(""+world[i][j].get());
    				if (world[i][j].get() == 'P') {
    					board[i][j].setForeground(Color.yellow);
    				}
    				else if (i==g1.getRow() && j == g1.getCol()) {
    					board[i][j].setForeground(Color.blue);
    				}
    				else if (i==g2.getRow() && j == g2.getCol()) {
    					board[i][j].setForeground(Color.green);
    				}
    				else if (i==g3.getRow() && j == g3.getCol()) {
    					board[i][j].setForeground(Color.magenta);
    				}
    				else if (i==g4.getRow() && j == g4.getCol()) {
    					board[i][j].setForeground(Color.red);
    				}
    				else if (world[i][j].get() == 'X') {
    					board[i][j].setForeground(Color.orange);
    				}
    				else if (world[i][j].get() == 'I') {
    					board[i][j].setForeground(Color.white);
    				}
    				else if (world[i][j].get() == 'O') {
    					board[i][j].setForeground(Color.black);
    				}
    			}
    			
    			
    		}
    		scoreLabel.setText("Score: " + points);
    	}
    	private ImageIcon createImageIcon (String path, String description) {
    		java.net.URL imgURL = World.class.getResource(path);
    		if (imgURL != null) {
    			return new ImageIcon(imgURL, description);
    		} 
    		else {
    			System.err.println("Could not find the image file: " + path);
    			return null;
    		}
    	}
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	public void keyPressed(KeyEvent e) {
    		c = e.getKeyChar();
    		movePacman(""+c);
    		
    	}
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public static void main(String[] args) {
    		World w = new World();
    		w.play(""+c);
    
    	}
    }

    Cell:
    Java Code:
    //The cell class allows us to create a 10x10 board of X's. oldValue allows pacman to move among the pellets and eat them
    public class Cell {
    	private char value;
    	private char oldValue = 'X';
    	private boolean iPellet;
    	public Cell() {
    		value = 'X';
    		iPellet = false;
    	}
    	public char getOldValue() {
    		return oldValue;
    	}
    	public char get() {
    		return value;
    	}
    	public void setIPellet() {
    			value = 'I';
    			oldValue = 'I';
    		iPellet = true;
    		
    	}
    	public boolean getPellet() {
    		return iPellet;
    	}
    	public void set (char c) {
    		oldValue = value;
    		value = c;
    	}
    }
    Driver:
    Java Code:
    public class Driver {
    	
    	
    	public static void main(String[] args) {
    		World w = new World();
    		w.play();
    
    	}
    
    }

    Ghost:

    Java Code:
    import java.util.Random;
    
    public class Ghost {
    	
    	private int size;
    	private int row, col;
    	public Ghost (int r, int c, int s) {
    		row = r;
    		col = c;
    		size = s;
    		
    	}
    	public int getRow() {
    		return row;
    	}
    	public int getCol() {
    		return col;
    	}
    	public void setRow(int r) {
    		row = r;
    		
    	}
    	public void setCol(int c) {
    		col = c;
    	}
    	private boolean checkMove(int r, int c) {
    		if (r<0 || r > size-1 || c < 0 || c > size-1) {
    			return false;
    		}
    		return true;
    	}
    	public boolean moveGhost() {
    		int newMove;
    		Random r = new Random();
    		newMove = r.nextInt(7);
    	
    		if (newMove == 0) {
    			if(checkMove(row-1, col)==false) {
    			return false;
    			}
    		
    			
    			
    			row = row-1; 
    			return true;
    			
    		}
    		if (newMove == 1) {
    			if(checkMove(row+1, col)==false) {
    			return false;
    			}
    
    			row = row+1; 
    			return true;
    			
    		}
    		if (newMove == 2) {
    			if(checkMove(row, col-1)==false) {
    			return false;
    			}
    			col = col-1; 
    			return true;
    			
    		}
    		if (newMove == 3) {
    			if(checkMove(row, col+1)==false) {
    			return false;
    			}
    
    			col = col+1; 
    			return true;
    			
    		}
    		if (newMove == 4) {
    			if(checkMove(row-1, col-1)==false) {
    			return false;
    			}
    
    			row = row-1; 
    			col = col-1;
    			return true;
    			
    		}
    		if (newMove == 5) {
    			if(checkMove(row-1, col+1)==false) {
    			return false;
    			}
    
    			row = row-1; 
    			col = col+1;
    			return true;
    			
    		}
    		if (newMove == 6) {
    			if(checkMove(row+1, col-1)==false) {
    			return false;
    			}
    
    			row = row+1; 
    			col = col-1;
    			return true;
    			
    		}
    		if (newMove == 7) {
    			if(checkMove(row+1, col+1)==false) {
    			return false;
    			}
    			row = row+1; 
    			col = col+1;
    			return true;
    			
    		}
    		
    		return true;
    		
    		
    	}
    }

    It's a very basic implementation of it but I've been having trouble with the keylistener. When I made the text version, the play method did most of the checking when it calls movePacman, but now when I pass it the String from the keylistener, some weird stuff happens. I would really appreciate any help on this.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I had a look through it and changed up some stuff. I think some of your logic for moving the ghosts and pacman are a bit off(I might be wrong, however whenever I move pacman to the top of the board errors fly up). Anyhow, I made the World class implement Runnable. For some reason pressing start was taking the focus from the frame and never giving it back :confused: so I had the frame request focus. This solved that problem. I changed some things in the Ghost class, this may have caused the moving of the ghosts to go off a bit. I really didn't look into the logic to be honest. Anyhow, if you build on this I'm sure you can figure the rest out. You can now move Pacman and the ghosts move too.

    World:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.Random;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class World extends JFrame implements ActionListener, KeyListener, Runnable{
    	private Cell [][] world;
    	private int pRow, pCol;
    	private Ghost g1;
    	private Ghost g2;
    	private Ghost g3;
    	private Ghost g4;
    	private int size=10;
    	private int points = 0;
    	private int immunity = 0;
    	private boolean immune = false;
    	private JLabel [][] board;
    	private JLabel scoreLabel;
    	private JPanel scorePanel;
    	private JPanel startPanel;
    	private JPanel boardPanel;
    	private JButton startButton;
    	private Container cPane;
    	private static char c='m';
    	private boolean started=false;
    	
    	private int pellets = size * size ;
    //Constructor for World class.
    	public World() {
    		createWorld();
    		Display();
    		
    	}
    	//Creates the Pacman World out of the Cell class. Forms a 10X10 2D array
    	private void createWorld(){
    		world = new Cell [size][size];
    		for (int i=0; i<size; i++) {
    			for (int j=0; j<size; j++){
    				world [i][j] = new Cell();
    
    			}
    		}
    		Random r = new Random();
    		//Randomly finds a spot for Pacmon and the Ghost and places them on the board
    		pRow = r.nextInt(size);
    		pCol = r.nextInt(size);
    		System.out.println(pRow);
    		System.out.println(pCol);
    		g1 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		g2 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		g3 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		g4 = new Ghost(r.nextInt(size), r.nextInt(size), size);
    		world[g1.getRow()][g1.getCol()].set('G');
    		world[g2.getRow()][g2.getCol()].set('G');
    		world[g3.getRow()][g3.getCol()].set('G');
    		world[g4.getRow()][g4.getCol()].set('G');
    		world [5][6].setIPellet();
    		world [2][3].setIPellet();
    		world [8][3].setIPellet();
    		world [3][9].setIPellet();
    		world[pRow][pCol].set('P');
    		printBoard();
    	}
    	//Prints the board so that the user can see it
    	public void printBoard() {
    		for (int i=0; i<size; i++) {
    			for (int j=0; j<size; j++){
    				System.out.print(world[i][j].get());
    			}
    			System.out.println();
    			
    		}
    		System.out.println("Points: " + points);
    	}
    	//Input method so that the user can tell pacman what to do. This was taken from one of the modules
    	
    	//Checks if all the pellets were "eaten" and declares a victory if so.
    	private boolean checkWin() {
    		if (points == pellets){
    			System.out.println("You Win!");
    			JOptionPane.showMessageDialog(this, "you Won!!");
    			System.exit(0);
    		}
    		if( immune == false) {
    		if (g1.getRow()==pRow && g1.getCol() == pCol || g2.getRow() == pRow && g2.getCol() == pCol || g3.getRow() == pRow && g3.getCol() == pCol || g4.getRow()==pRow &&g4.getCol()==pCol) {
    			System.out.println("You Lose!");
    			JOptionPane.showMessageDialog(this,"You Lost!!");
    			System.exit(0);
    		}
    		}
    		return false;
    	}
    	//Checks if a move is valid and not going off of the board.
    	private boolean checkMove(int row, int col) {
    		boolean check=true;
    		if (row<0 || row > size-1 || col < 0 || col > size-1) {
    			check= false;
    		}
    		return check;
    	}
    	//Randomly moves the ghost in a direction selected by the number generator. If the Ghost happens upon Pacman, the game is over.
    	
    	
    	//Up = W, Left = A, Right = D, Down = X, NorthW = Q, NorthE = E, SouthW= Z, SouthE = C
    	//Directions to actually move pacman. Each if statement incorporates the checkmove() method to see if it can continue
    	public void eatPellet() {
    		if (world[pRow-1][pCol].get() == 'X'){
    			points++;
    			
    		
    		}
    		if (world[pRow-1][pCol].get() == 'I') {
    			immune = true;
    			immunity = 10;
    			points++;
    		}
    	}
    	
    	private boolean movePacman(char m){
    		
    		boolean valid=true;
    		if (m=='W'||m=='w') {
    			if(checkMove(pRow-1, pCol)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow-1][pCol].set('P');
    			world[pRow][pCol].set('O');
    			pRow = pRow -1;
    			updateDisplay();
    		}
    		}
    		else if (m=='X'||m=='x') {
    			if(checkMove(pRow+1, pCol)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow+1][pCol].set('P');
    			world[pRow][pCol].set('O');
    			pRow = pRow +1;
    			updateDisplay();
    			}
    		}
    		else if (m=='A'||m=='a') {
    			if(checkMove(pRow, pCol-1)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow][pCol-1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol -1;
    			updateDisplay();
    			}
    		}
    		
    		else if (m=='D'||m=='d') {
    			if(checkMove(pRow, pCol+1)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow][pCol+1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol +1;
    			updateDisplay();
    			}
    		}
    		
    		else if (m=='Q'||m=='q') {
    			if(checkMove(pRow-1, pCol-1)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow-1][pCol-1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol -1;
    			pRow = pRow - 1;
    			updateDisplay();
    			}
    		}
    		else if (m=='E'||m=='e') {
    			if(checkMove(pRow-1, pCol+1)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow-1][pCol+1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol + 1;
    			pRow = pRow - 1;
    			updateDisplay();
    			}
    		}
    		else if (m=='Z'||m=='z') {
    			if(checkMove(pRow+1, pCol-1)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow+1][pCol-1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol -1;
    			pRow = pRow + 1;
    			updateDisplay();
    			}
    		}
    		else if (m=='C'||m=='c') {
    			if(checkMove(pRow+1, pCol+1)==false) {
    				System.out.println ("Move Invalid");
    				valid= false;
    				
    			}else{
    			eatPellet();
    			world[pRow+1][pCol+1].set('P');
    			world[pRow][pCol].set('O');
    			pCol = pCol +1;
    			pRow = pRow + 1;
    			updateDisplay();
    			}
    		}
    		
    		
    		else {
    			//System.out.println ("Wrong Input");
    			valid=false;
    		}
    		return valid;
    		
    		
    	}
    	//The run method that begins the game. While the checkwin() method is false, the game continues to run.
    	public void run() {
    	
    		boolean validMovePacman;
    		boolean validMoveGhost;
    		
    	
    		
    		while (true) {
    			try{
    				Thread.sleep(1000);
    			}catch(Exception e){}
    			this.requestFocusInWindow();
    				
    			if (immunity == 0 ) {
    				immune = false;
    			}
    			if (immune == true) {
    				immunity--;
    			}
    			checkWin();
    			world[g1.getRow()][g1.getCol()].set(world[g1.getRow()][g1.getCol()].getOldValue());
    			validMoveGhost = g1.moveGhost();
    			if(validMoveGhost) {
    			world[g1.getRow()][g1.getCol()].set('G');
    			world[g2.getRow()][g2.getCol()].set(world[g2.getRow()][g2.getCol()].getOldValue());
    			}
    			validMoveGhost = g2.moveGhost();
    			if(validMoveGhost) {
    			world[g2.getRow()][g2.getCol()].set('G');
    			world[g3.getRow()][g3.getCol()].set(world[g3.getRow()][g3.getCol()].getOldValue());
    			}
    			validMoveGhost = g3.moveGhost();
    			if(validMoveGhost) {
    				world[g3.getRow()][g3.getCol()].set('G');
    				world[g4.getRow()][g4.getCol()].set(world[g4.getRow()][g4.getCol()].getOldValue());
    			}
    
    			
    			validMoveGhost = g4.moveGhost();
    			 if(validMoveGhost) {
    				 world[g4.getRow()][g4.getCol()].set('G');
    			}
    			 
    			 updateDisplay();
    			checkWin();
    			
    		}
    		
    		
    	}
    	private void Display() {
    		this.setTitle("Pacman");
    		this.setResizable(false);
    		this.setSize(500, 500);
    		this.addKeyListener(this);
    		ImageIcon pacmanIcon = createImageIcon("http://www.java-forums.org/images/pacman.gif","");
    		
    		cPane = this.getContentPane();
    		cPane.setLayout (new BorderLayout());
    		
    		scorePanel = new JPanel();
    		scorePanel.setBackground(Color.black);
    		cPane.add(scorePanel, BorderLayout.NORTH);
    		scoreLabel = new JLabel("Score: "+ points);
    		scoreLabel.setForeground(Color.WHITE);
    		scorePanel.add(scoreLabel);
    		
    		startPanel = new JPanel();
    		startPanel.setBackground(Color.black);
    		cPane.add(startPanel, BorderLayout.SOUTH);
    		startButton = new JButton("Start");
    		startButton.addActionListener(this);
    		startPanel.add(startButton);
    		boardPanel = new JPanel();
    		boardPanel.setBackground(Color.black);
    		boardPanel.setLayout(new GridLayout(size, size));
    		cPane.add(boardPanel, BorderLayout.CENTER);
    		board = new JLabel [size][size];
    		
    		for (int i = 0; i < size; i++) {
    			for (int j = 0; j < size; j++) {
    				
    				board[i][j] = new JLabel("X");
    				board[i][j].setForeground(Color.white);
    				boardPanel.add(board[i][j]);
    			}
    		}
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		this.setVisible(true);
    		this.requestFocusInWindow();
    	}
    	private void updateDisplay() {
    		for (int i=0; i<size; i++) {
    			for (int j=0; j<size; j++){
    				board[i][j].setText(""+world[i][j].get());
    				if (world[i][j].get() == 'P') {
    					board[i][j].setForeground(Color.yellow);
    					
    				}
    				else if (i==g1.getRow() && j == g1.getCol()) {
    					board[i][j].setForeground(Color.blue);
    					
    				}
    				else if (i==g2.getRow() && j == g2.getCol()) {
    					board[i][j].setForeground(Color.green);
    					
    				}
    				else if (i==g3.getRow() && j == g3.getCol()) {
    					board[i][j].setForeground(Color.magenta);
    					
    				}
    				else if (i==g4.getRow() && j == g4.getCol()) {
    					board[i][j].setForeground(Color.red);
    					
    				}
    				else if (world[i][j].get() == 'X') {
    					board[i][j].setForeground(Color.orange);
    					
    				}
    				else if (world[i][j].get() == 'I') {
    					board[i][j].setForeground(Color.white);
    					
    				}
    				else if (world[i][j].get() == 'O') {
    					board[i][j].setForeground(Color.black);
    					
    				}
    				
    				
    			}
    			
    			
    		}
    		
    		scoreLabel.setText("Score: " + points);
    	}
    	public void initiate()
    	{	
    		updateDisplay();
    		startButton.setEnabled(false);
    		started=true;
    
    		Thread run=new Thread(this);
    		run.start();
    	}
    	private ImageIcon createImageIcon (String path, String description) {
    		java.net.URL imgURL = World.class.getResource(path);
    		if (imgURL != null) {
    			return new ImageIcon(imgURL, description);
    		} 
    		else {
    			System.err.println("Could not find the image file: " + path);
    			return null;
    		}
    	}
    	public void keyPressed(KeyEvent e) {
    		c = e.getKeyChar();
    		System.out.println("C:"+c);
    		if(started==true){
    		movePacman(c);
    		}
    	}
    	public void actionPerformed(ActionEvent e) {
    		System.out.println(e.getActionCommand());
    		if(e.getActionCommand().equals("Start"))
    		{
    		initiate();
    		}
    	}
    	
    	public void keyReleased(KeyEvent e) {
    
    		
    	}
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public static void main(String[] args) {
    		World w = new World();
    
    	}
    }

    Ghost :

    Java Code:
    import java.util.Random;
    
    public class Ghost {
    	
    	private int size;
    	private int row, col;
    	public Ghost (int r, int c, int s) {
    		row = r;
    		col = c;
    		size = s;
    		
    	}
    	public int getRow() {
    		return row;
    	}
    	public int getCol() {
    		return col;
    	}
    	public void setRow(int r) {
    		row = r;
    		
    	}
    	public void setCol(int c) {
    		col = c;
    	}
    	private boolean checkMove(int r, int c) {
    		boolean check=true;
    		if (r<0 || r > size-1 || c < 0 || c > size-1) {
    			check= false;
    		}
    		return check;
    	}
    	public boolean moveGhost() {
    		int newMove;
    		boolean move=true;
    		Random r = new Random();
    		newMove = r.nextInt(7);
    	
    		if (newMove == 0) {
    			if(checkMove(row-1, col)==false) {
    			move=false;
    			}
    			else{
    			row = row-1;
    			}
    			
    		}
    		if (newMove == 1) {
    			if(checkMove(row+1, col)==false) {
    			move= false;
    			}
    			else{
    			row = row+1; 
    			}
    			
    		}
    		if (newMove == 2) {
    			if(checkMove(row, col-1)==false) {
    			move= false;
    			}else{
    			col = col-1; 
    			}
    			
    		}
    		if (newMove == 3) {
    			if(checkMove(row, col+1)==false) {
    			move= false;
    			}
    			else{
    			col = col+1; 
    			}
    			
    		}
    		if (newMove == 4) {
    			if(checkMove(row-1, col-1)==false) {
    			move= false;
    			}
    			else{
    			row = row-1; 
    			col = col-1;
    			}
    			
    		}
    		if (newMove == 5) {
    			if(checkMove(row-1, col+1)==false) {
    			move= false;
    			}
    			else{
    			row = row-1; 
    			col = col+1;
    			}
    			
    		}
    		if (newMove == 6) {
    			if(checkMove(row+1, col-1)==false) {
    			move= false;
    			}
    			else{
    			row = row+1; 
    			col = col-1;
    			}
    			
    		}
    		if (newMove == 7) {
    			if(checkMove(row+1, col+1)==false) {
    			move= false;
    			}else{
    			row = row+1; 
    			col = col+1;
    			}
    			
    		}
    		
    		return move;
    		
    		
    	}
    }
    Good luck with the rest of it. Hope this is some way helpful, im not exactly an expert :D

  3. #3
    scheng12 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Did you change the other methods? Or did you get rid of them? The start button worked but it doesn't seem like I can move pacman. It just gives me a bunch of errors. I'm not trying to have the ghosts move on their own, only when Pacman moves. Thanks for helping though.

Similar Threads

  1. need help with pacman game!!
    By Newbie_Javaer in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 02:44 PM
  2. Replies: 3
    Last Post: 07-18-2009, 01:26 PM
  3. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 01:35 PM
  4. Java Pacman question
    By whdbstjr90 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 11:28 PM
  5. Help implementing JDBC
    By mooey1232003 in forum New To Java
    Replies: 6
    Last Post: 07-11-2007, 10:15 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
  •