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

• 05-09-2012, 01:01 AM
paris72
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 :
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 ?
• 05-09-2012, 02:49 AM
shall
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.
• 05-09-2012, 10:06 AM
paris72
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 ?
• 05-09-2012, 10:35 AM
shall
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.