Results 1 to 1 of 1
  1. #1
    whdbstjr90 is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default Java Pacman question

    I am using Eclipse to make a pacman game and I am having a trouble.

    I am trying to make ghosts and I don't know how to make that..

    I learned RandomGenerator gen = new RandomGenerator(); and Im sure I can

    make ghosts move randomly by using that but I don't know how to do that..

    can some one help me?

    Java Code:
    import acm.program.*;
    	import acm.util.*;
    	import java.applet.*;
    	import java.awt.*;
    	import java.awt.event.*;
    	import acm.graphics.*;
    	import java.util.*;
    	
    public class Main extends GraphicsProgram
    {	
    	private ArrayList<GRect> squarelist = new ArrayList<GRect>();
    	private ArrayList<GOval> dots = new ArrayList<GOval>();
    	private RandomGenerator gen = new RandomGenerator();
    	private Background Background;
    	private GRect Center;
    	private GRect LeftCenter;
    	private GRect LeftCenter2;
    	private GRect LeftCenter3;
    	private GRect RightCenter;
    	private GRect RightCenter2;
    	private GRect RightCenter3;
    	private GRect Boarder;
    	private GRect Boarder2;
    	private GRect LeftBoarder;
    	private GRect LeftBoarder2;
    	private GRect RightBoarder;
    	private GRect RightBoarder2;
    	private GRect North;
    	private GRect North2;
    	private GRect North3;
    	private GRect South;
    	private GRect South2;
    	private GRect South3;
    	private GRect South4;
    	private GRect Northwest;
    	private GRect Northwest2;
    	private GRect Northwest3;
    	private GRect Northeast;
    	private GRect Northeast2;
    	private GRect Northeast3;
    	private GRect Southwest;
    	private GRect Southwest2;
    	private GRect Southwest3;
    	private GRect Southwest4;
    	private GRect Southwest5;
    	private GRect Southwest6;
    	private GRect Southeast;
    	private GRect Southeast2;
    	private GRect Southeast3;
    	private GRect Southeast4;
    	private GRect Southeast5;
    	private GRect Southeast6;
    	private Pacman pacman;
    	private SwingTimer t;
    	private boolean smoothright;
    	private boolean smoothleft;
    	private boolean smoothup;
    	private boolean smoothdown;
    	private boolean right;
    	private boolean left;
    	private boolean up;
    	private boolean down;
    
    	public void run()
    	{
    		resize(800, 650); 
    	
    		Background = new Background();
    		Background.setLocation(0, 0);	
    		add(Background);
    		
    		Center = new GRect(210, 300, 110, 50);
    		Center.setColor(Color.BLUE);
    		Center.setFilled(false);
    		add(Center);
    		squarelist.add(Center);
    		
    		LeftCenter = new GRect(150, 160, 10, 150);
    		LeftCenter.setColor(Color.BLUE);
    		LeftCenter.setFilled(false);
    		add(LeftCenter);
    		squarelist.add(LeftCenter);
    		
    		LeftCenter2 = new GRect(160, 220, 40, 10);
    		LeftCenter2.setColor(Color.BLUE);
    		LeftCenter2.setFilled(false);
    		add(LeftCenter2);
    		squarelist.add(LeftCenter2);
    		
    		LeftCenter3 = new GRect(150, 380, 10, 70);
    		LeftCenter3.setColor(Color.BLUE);
    		LeftCenter3.setFilled(false);
    		add(LeftCenter3);
    		squarelist.add(LeftCenter3);
    		
    		RightCenter = new GRect(370, 160, 10, 150);
    		RightCenter.setColor(Color.BLUE);
    		RightCenter.setFilled(false);
    		add(RightCenter);
    		squarelist.add(RightCenter);
    		
    		RightCenter2 = new GRect(330, 220, 40, 10);
    		RightCenter2.setColor(Color.BLUE);
    		RightCenter2.setFilled(false);
    		add(RightCenter2);
    		squarelist.add(RightCenter2);
    		
    		RightCenter3 = new GRect(370, 380, 10, 70);
    		RightCenter3.setColor(Color.BLUE);
    		RightCenter3.setFilled(false);
    		add(RightCenter3);
    		squarelist.add(RightCenter3);
    		
    		Boarder = new GRect(20, 30, 500, 550);
    		Boarder.setColor(Color.BLUE);
    		Boarder.setFilled(false);
    		add(Boarder);
    		
    		Boarder2 = new GRect(30, 40, 480, 530);
    		Boarder2.setColor(Color.BLUE);
    		Boarder2.setFilled(false);
    		add(Boarder2);
    		
    		LeftBoarder = new GRect(30, 240, 80, 70);
    		LeftBoarder.setColor(Color.BLUE);
    		add(LeftBoarder);
    		squarelist.add(LeftBoarder);
    		
    		LeftBoarder2 = new GRect(30, 340, 80, 70);
    		LeftBoarder2.setColor(Color.BLUE);
    		LeftBoarder2.setFilled(false);
    		add(LeftBoarder2);
    		squarelist.add(LeftBoarder2);
    		
    		RightBoarder = new GRect(430, 240, 80, 70);
    		RightBoarder.setColor(Color.BLUE);
    		RightBoarder.setFilled(false);
    		add(RightBoarder);
    		squarelist.add(RightBoarder);
    		
    		RightBoarder2 = new GRect (430, 340, 80, 70);
    		RightBoarder2.setColor(Color.BLUE);
    		RightBoarder2.setFilled(false);
    		add(RightBoarder2);
    		squarelist.add(RightBoarder2);
    		
    		North = new GRect(260, 40, 10, 80);
    		North.setColor(Color.BLUE);
    		North.setFilled(false);
    		add(North);
    		squarelist.add(North);
    		
    		North2 = new GRect(210, 160, 110, 10);
    		North2.setColor(Color.BLUE);
    		North2.setFilled(false);
    		add(North2);
    		squarelist.add(North2);
    		
    		North3 = new GRect(260, 170, 10, 60);
    		North3.setColor(Color.BLUE);
    		North3.setFilled(false);
    		add(North3);
    		squarelist.add(North3);
    		
    		South = new GRect(260, 510, 10, 35);
    		South.setColor(Color.BLUE);
    		South.setFilled(false);
    		add(South);
    		squarelist.add(South);
    		
    		South2 = new GRect(180, 500, 165, 10);
    		South2.setColor(Color.BLUE);
    		South2.setFilled(false);
    		add(South2);
    		squarelist.add(South2);
    		
    		South3 = new GRect(180, 440, 170, 10);
    		South3.setColor(Color.BLUE);
    		South3.setFilled(false);
    		add(South3);
    		squarelist.add(South3);
    		
    		South4 = new GRect(260, 450, 10, 30);
    		South4.setColor(Color.BLUE);
    		South4.setFilled(false);
    		add(South4);
    		squarelist.add(South4);
    		
    		Northwest = new GRect(60, 80, 50, 40);
    		Northwest.setColor(Color.BLUE);
    		Northwest.setFilled(false);
    		add(Northwest);
    		squarelist.add(Northwest);
    		
    		Northwest2 = new GRect(150, 80, 70, 40);
    		Northwest2.setColor(Color.BLUE);
    		Northwest2.setFilled(false);
    		add(Northwest2);
    		squarelist.add(Northwest2);
    		
    		Northwest3 = new GRect(60, 160, 50, 10);
    		Northwest3.setColor(Color.BLUE);
    		Northwest3.setFilled(false);
    		add(Northwest3);
    		squarelist.add(Northwest3);
    		
    		Northeast = new GRect(420, 80, 50, 40);
    		Northeast.setColor(Color.BLUE);
    		Northeast.setFilled(false);
    		add(Northeast);
    		squarelist.add(Northeast);
    		
    		Northeast2 = new GRect(310, 80, 70, 40);
    		Northeast2.setColor(Color.BLUE);
    		Northeast2.setFilled(false);
    		add(Northeast2);
    		squarelist.add(Northeast2);
    		
    		Northeast3 = new GRect(420, 160, 50, 10);
    		Northeast3.setColor(Color.BLUE);
    		Northeast3.setFilled(false);
    		add(Northeast3);
    		squarelist.add(Northeast3);
    		
    		Southwest = new GRect(50, 535, 180, 10);
    		Southwest.setColor(Color.BLUE);
    		Southwest.setFilled(false);
    		add(Southwest);
    		squarelist.add(Southwest);
    		
    		Southwest2 = new GRect(150, 500, 10, 35);
    		Southwest2.setColor(Color.BLUE);
    		Southwest2.setFilled(false);
    		add(Southwest2);
    		squarelist.add(Southwest2);
    		
    		Southwest3 = new GRect(50, 470, 80, 10);
    		Southwest3.setColor(Color.BLUE);
    		Southwest3.setFilled(false);
    		add(Southwest3);
    		squarelist.add(Southwest3);
    		
    		Southwest4 = new GRect(120, 480, 10, 30);
    		Southwest4.setColor(Color.BLUE);
    		Southwest4.setFilled(false);
    		add(Southwest4);
    		squarelist.add(Southwest4);
    		
    		Southwest5 = new GRect(150, 470, 90, 10);
    		Southwest5.setColor(Color.BLUE);
    		Southwest5.setFilled(false);
    		add(Southwest5);
    		squarelist.add(Southwest5);
    		
    		Southwest6 = new GRect(30, 500, 65, 10);
    		Southwest6.setColor(Color.BLUE);
    		Southwest6.setFilled(false);
    		add(Southwest6);
    		squarelist.add(Southwest6);
    		
    		Southeast = new GRect(300, 535, 188, 10);
    		Southeast.setColor(Color.BLUE);
    		Southeast.setFilled(false);
    		add(Southeast);
    		squarelist.add(Southeast);
    		
    		Southeast2 = new GRect(370, 500, 10, 35);
    		Southeast2.setColor(Color.BLUE);
    		Southeast2.setFilled(false);
    		add(Southeast2);
    		squarelist.add(Southeast2);
    		
    		Southeast3 = new GRect(290, 470, 90, 10);
    		Southeast3.setColor(Color.BLUE);
    		Southeast3.setFilled(false);
    		add(Southeast3);
    		squarelist.add(Southeast3);
    		
    		Southeast4 = new GRect(400, 470, 88, 10);
    		Southeast4.setColor(Color.BLUE);
    		Southeast4.setFilled(false);
    		add(Southeast4);
    		squarelist.add(Southeast4);
    		
    		Southeast5 = new GRect(400, 480, 10, 30);
    		Southeast5.setColor(Color.BLUE);
    		Southeast5.setFilled(false);
    		add(Southeast5);
    		squarelist.add(Southeast5);
    		
    		Southeast6 = new GRect(435, 500, 75, 10);
    		Southeast6.setColor(Color.BLUE);
    		Southeast6.setFilled(false);
    		add(Southeast6);
    		squarelist.add(Southeast6);
    		
    		pacman = new Pacman();
    		add(pacman);
    		pacman.setLocation(257, 483);
    	
    		
    
    
    
    		t = new SwingTimer(10, this);
    		t.start();
    			
    		addMouseListeners();
    		addKeyListeners();
    		}
    		
    	
    		public void keyPressed(KeyEvent e)
    		{
    			if(e.getKeyCode() == KeyEvent.VK_RIGHT)
    			{	
    				smoothright = true;
    				smoothup = false;
    				smoothleft = false;
    				smoothdown = false;
    			}
    			
    			if(e.getKeyCode() == KeyEvent.VK_LEFT)
    			{
    				smoothleft = true;
    				smoothup = false;
    				smoothright = false;
    				smoothdown = false;
    			}
    			
    			if(e.getKeyCode() == KeyEvent.VK_UP)
    			{
    				smoothup = true;
    				smoothright = false;
    				smoothleft = false;
    				smoothdown = false;
    			}
    			
    			if(e.getKeyCode() == KeyEvent.VK_DOWN)
    			{
    				smoothdown = true;
    				smoothup = false;
    				smoothright = false;
    				smoothleft = false;
    				
    			}
    		}
    		
    	
    		
    		public void actionPerformed(ActionEvent e)
    		{	
    			if(smoothright == true)
    			{
    				right = true;
    				
    				if(right == true)
    				{
    					if(pacman.getX() + 40 >= 530)
    					{
    					}
    					else
    					{
    						pacman.move(2, 0);	
    					}
    				}
    				
    				int i = 0;
    				while(i < squarelist.size())
    				{
    					if(pacman.getBounds().intersects(squarelist.get(i).getBounds()))
    					{
    						right = false;	
    					}
    					i = i + 1;
    				}
    				
    				if(right == false)
    				{
    					pacman.move(-2, 0);
    				}
    				
    				right = true;
    			}
    			
    			if(smoothleft == true)
    			{
    				left = true;
    				
    				if(left == true)
    				{
    					if(pacman.getX() <= 33)
    						{
    						}
    						else
    						{
    							pacman.move(-2, 0);
    						}	
    				}
    				
    				int i = 0;
    				while(i < squarelist.size())
    				{
    					if(pacman.getBounds().intersects(squarelist.get(i).getBounds()))
    					{
    						left = false;
    					}
    					i = i + 1;
    				}
    				
    				if(left == false)
    				{
    					pacman.move(2, 0);
    				}
    				
    				
    				left = true;
    			}
    			
    			if(smoothup == true)
    			{
    				up = true;
    				
    				if(up == true)
    				{
    					if(pacman.getY() <= 43)
    					{
    					}
    					else
    					{
    						pacman.move(0, -2);
    					}	
    				}
    				
    				int i = 0;
    				while(i < squarelist.size())
    				{
    					if(pacman.getBounds().intersects(squarelist.get(i).getBounds()))
    					{
    						up = false;
    					}
    					i = i + 1;
    				}
    				if(up == false)
    				{
    					pacman.move(0, 2);
    				}
    				
    				up = true;
    			}
    			
    			if(smoothdown == true)
    			{
    				down = true;
    				
    				if(down == true)
    				{
    					if(pacman.getY() >= 548.1)
    					{
    					}
    					else
    					{
    						pacman.move(0, 2);
    					}	
    				}
    				
    				int i = 0;
    				while(i < squarelist.size())
    				{
    					if(pacman.getBounds().intersects(squarelist.get(i).getBounds()))
    					{
    						down = false;
    					}
    					i = i + 1;
    				}
    				
    				if(down == false)
    				{
    					pacman.move(0, -2);
    				}
    				
    				down = true;
    			}
    		}
    	}
    Last edited by JavaBean; 12-11-2007 at 11:47 PM. Reason: Code placed inside [code] tag.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. New Comer, Java C# question
    By lmei007 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 04:54 PM
  3. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 05:48 PM
  4. Java Review Question
    By toad in forum New To Java
    Replies: 1
    Last Post: 10-29-2007, 06:13 AM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 AM

Posting Permissions

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