This site requires JavaScript, please enable it in your browser!
Greenfoot back
beastonion
beastonion wrote ...

2017/5/1

Help with pacman ghost AI

beastonion beastonion

2017/5/1

#
So, I'm trying to make a pacman game and while I was making the ghost AI, I ran into a wall. When I used "getOneObjectAtOffset" when it hits a wall and decide which way to go, it stops(like it should), but it's not seeing any walls apparently(it should go left). I had it to print out which wall it sees, but it doesn't see any, thus it's not doing any of the conditions I set and stops at the wall it hit... Even my teacher took a long look at it, and she couldn't figure out why it's not seeing any walls. Please help! Thanks :) Here's the code:
Wall w = (Wall)getOneIntersectingObject(Wall.class);
    Wall walll =(Wall)getOneObjectAtOffset(getX()-2,getY(), Wall.class);
    Wall wallr =(Wall)getOneObjectAtOffset(getX()+2,getY(), Wall.class);
    Wall wallu =(Wall)getOneObjectAtOffset(getX(),getY()-2, Wall.class);
    Wall walld =(Wall)getOneObjectAtOffset(getX(),getY()+2, Wall.class);
    List<Pac> player= getObjectsInRange(100,Pac.class);



    if(w!=null)//see when it hits a wall
    {
        lockl=false;
        lockr=false;
        locku=false;
        lockd=false;
        stop=true;
    }
    if(stop==true)
    {
        //see if its seeing walls at all
        if(walll==null)
        {
            System.out.println("no wall left");
        }
        if(wallr==null)
        {
            System.out.println("no wall right");
        }
        if(walld==null)
        {
            System.out.println("no wall down");
        }
        if(wallu==null)
        {
            System.out.println("no wall up");
        }

        //3 ways to go
        if(walll==null && wallr==null && wallu!=null && walld==null)//left,right,down
        {
            System.out.println("cantgoup");
            lockl=true;
        }
        if(walll==null && wallr==null && wallu==null && walld!=null)//left,right,up
        {
            lockr=true;
        }

        //2ways to go
        if(walll==null && wallr!=null && wallu==null && walld!=null)//left,up
        {
            lockr=true;
        }
        if(walll==null && wallr!=null && wallu!=null && walld==null)//left,down
        {
            lockl=true;
        }
        if(walll!=null && wallr==null && wallu==null && walld!=null)//right,up
        {
            locku=true;
        }
        if(walll!=null && wallr==null && wallu!=null && walld==null)//right,down
        {
            lockd=true;
        }

        //one way to go
        if(walll==null && wallr!=null && wallu!=null && walld!=null)//left
        {
            lockr=true;
        }
        if(walll!=null && wallr==null && wallu!=null && walld!=null)//right
        {
            lockl=true;
        }
        if(walll!=null && wallr!=null && wallu==null && walld!=null)//down
        {
            lockd=true;
        }
        if(walll!=null && wallr!=null && wallu!=null && walld==null)//up
        {
            lockr=true;
        }
if(lockl==true)
        {
            xl=getX()-1;
        }
        if(lockr==true)
        {
            xl=getX()+1;
        }
        if(locku==true)
        {
            yl=getY()-1;
        }
        if(lockd==true)
        {
            yl=getY()+1;
        }}
danpost danpost

2017/5/1

#
The Pacman is supposed to run into walls .. not you. The 'getOneObjectAtOffset' method uses offset values, not absolute coordinate values in its parameters. Remove all 'getX()' and 'getY()' from between the parenthesis of the method calls. That is, lines 2 through 5 should be:
Wall walll =(Wall)getOneObjectAtOffset(-2, 0, Wall.class);
Wall wallr =(Wall)getOneObjectAtOffset(2, 0, Wall.class);
Wall wallu =(Wall)getOneObjectAtOffset(0, -2, Wall.class);
Wall walld =(Wall)getOneObjectAtOffset(0, 2, Wall.class);
You may need to use a larger value than '2' as the offset (maybe use something closer to 'getImage().getWidth()' and 'getImage().getHeight()' when appropriate).
beastonion beastonion

2017/5/1

#
danpost wrote...
The Pacman is supposed to run into walls .. not you. The 'getOneObjectAtOffset' method uses offset values, not absolute coordinate values in its parameters. Remove all 'getX()' and 'getY()' from between the parenthesis of the method calls. That is, lines 2 through 5 should be:
Wall walll =(Wall)getOneObjectAtOffset(-2, 0, Wall.class);
Wall wallr =(Wall)getOneObjectAtOffset(2, 0, Wall.class);
Wall wallu =(Wall)getOneObjectAtOffset(0, -2, Wall.class);
Wall walld =(Wall)getOneObjectAtOffset(0, 2, Wall.class);
You may need to use a larger value than '2' as the offset (maybe use something closer to 'getImage().getWidth()' and 'getImage().getHeight()' when appropriate).
Well, I changed the lines, and now it's not moving at all. Also, it tends to get stuck on walls when placed in a path.... I don't have to use "getoneobjectatoffset", it's preferred to use some kind of "List" method for this purpose, I used it just to test to see if my logic works or not.
You need to login to post a reply.