Results 1 to 4 of 4
  1. #1
    paris72 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Working on a Pacman game using threads but don't know how .

    I'm at the place where I want the enemy to move it does but it does cross the walls as well . here the code :
    Java Code:
    public void run () {
    		while (true) {
    			if (dir == 0) {
    				if (x == 0) {x = 19 ;}
    				else if (p.map[x - 1][y] != 1){
    					x--;
    				System.out.println(p.map[x-1][y]);
    				}
    				else{
    					dir = Math.abs(r.nextInt())%4;
    				}
    			}
    			if (dir == 1) {
    				if (x == 19) {x = 0 ;}
    				else if (p.map[x + 1][y] != 1){
    					x++;
    				}
    				else{
    					dir = Math.abs(r.nextInt())%4;
    				}
    			}
    			if (dir == 2) {
    				if (y == 0){ y = 19 ;}
    				else if (p.map[x][y - 1] != 1) {
    					y--;
    				}
    				else{
    					dir = Math.abs(r.nextInt())%4;
    				}
    			}
    			if (dir == 3) {
    				if (y == 19){ y = 0 ;}
    				else if (p.map[x][y + 1] != 1) {
    					y++;
    					//if (y == 19) y = 0 ;
    				}
    				else{
    					dir = Math.abs(r.nextInt())%4;
    				}
    			}
    			
    			try {
    				sleep(200) ;
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    what should I do ?

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Working on a Pacman game using threads but don't know how .

    It seems that your code is allowing for crossing of boundaries, e.g. when going in the negative direction if x=0, your program sets x=19. The same can be said of all other directions.

  3. #3
    paris72 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Working on a Pacman game using threads but don't know how .

    yes I know but I don't know how to fix it ?

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Working on a Pacman game using threads but don't know how .

    Change the code so that the coordinates don't change at the boundaries, i.e. if (x == 0) {x = 0}. Do the same for all the boundary conditions.

Similar Threads

  1. Pacman Game
    By Hollowsoul in forum Java 2D
    Replies: 13
    Last Post: 07-16-2011, 11:31 PM
  2. Making PacMan game, need help!
    By Mondos in forum Java Gaming
    Replies: 14
    Last Post: 06-03-2011, 03:24 PM
  3. Working with threads.
    By bpx95 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 06:24 AM
  4. Huge problem. PacMan game.
    By Bullfrog in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 02:39 PM
  5. need help with pacman game!!
    By Newbie_Javaer in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 02:44 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
  •