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

2014/5/24

My PacMan Game

SaxOps1 SaxOps1

2014/5/24

#
I'm working on a pacman game for school, and I need some help with movement: Here's my movement for up, but need some help. When I move up, 'up' becomes true. How would I make it false after using it? if(Greenfoot.isKeyDown("up") && down == false && left == false && right == false) { up = true; int x = getX(); int y = getY(); int ny = getY()-4; setLocation(x,ny); if(rotation != 270) { turn(270 - rotation); } }
danpost danpost

2014/5/24

#
What you should be saving in a field is the last instructed direction (whether that was the direction moved or not). The rotation of the pacman can be used to determine that last direction moved (or attempted to move, if against a wall). If the instructed direction is not the current direction, then if can go the instructed direction, go that way, else if can continue in current direction, go in that direction.
SaxOps1 SaxOps1

2014/5/24

#
Umm, don't completely understand that. I understand the first half but the last sentence you lost me
SaxOps1 SaxOps1

2014/5/24

#
Also is there a way to make it false after using it?
danpost danpost

2014/5/24

#
SaxOps1 wrote...
Also is there a way to make it false after using it?
I don't understand that at all.
SaxOps1 SaxOps1

2014/5/25

#
danpost wrote...
SaxOps1 wrote...
Also is there a way to make it false after using it?
I don't understand that at all.
Like, after setting 'up = true', would there be a way to make it false after use?
danpost danpost

2014/5/25

#
You could use 'up = false;' anywhere. However, you would most probably want to do it after the last place that the value is used. It is not being used anywhere in the code given; but, you possibly could have used it by replacing 'Greenfoot.isKeyDown("up")' with 'up == true'. Therefore, it is possible that you need to reset it to false within that 'if' block. But, that is where you are setting it to 'true' I think more context is necessary to answer that question. Show how all the direction are being dealt with (variables being set and used throughout the code of the class).
SaxOps1 SaxOps1

2014/5/25

#
What I'm trying to eliminate in my code is the ability to stop moving diagonally completely, and you can only move in one direction at any time, and prevent pressing both up and left (for example) to move diagonal moving. Here's my move method: public void move() { if(canSee(Wall.class)) { } else { rotation = getRotation(); if(Greenfoot.isKeyDown("up")) { if(down == false && left == false && right == false) { up = true; int x = getX(); int y = getY(); int ny = getY()-4; setLocation(x,ny); if(rotation != 270) { turn(rotationMove = 270 - rotation); } } up = false; } if(Greenfoot.isKeyDown("down")) { if(up == false && left == false && right == false) { down = true; int x = getX(); int y = getY(); int ny = getY()+4; setLocation(x,ny); if(rotation != 90) { turn(rotationMove = 90 - rotation); } } down = false; } if(Greenfoot.isKeyDown("left")) { if(up == false && down == false && right == false) { left = true; int x = getX(); int y = getY(); int nx = getX()-4; setLocation(nx, y); if(rotation != 180) { turn(rotationMove = 180 - rotation); } } left = false; } if(Greenfoot.isKeyDown("right")) { if(up == false && down == false && left == false) { right = true; int x = getX(); int y = getY(); int nx = getX()+4; setLocation(nx, y); if(rotation != 0) { turn(rotationMove = 0 - rotation); } } right = false; } } }
SaxOps1 SaxOps1

2014/5/25

#
Lol! Figured it out Now i'm using else ifs' for the last 3 and it works! Thanks for you help!
danpost danpost

2014/5/25

#
SaxOps1 wrote...
Lol! Figured it out Now i'm using else ifs' for the last 3 and it works! Thanks for you help!
Glad you got it working. I was about to suggest a different way; but, I did not want to suggest one (like the way you already have yours) that was not true to the way the real game movement is.
SaxOps1 SaxOps1

2014/5/26

#
Yeah, I have also restructured my code a little This is my code now: public void move() { if(canSee(Wall.class)) { moving = false; } else { rotation = getRotation(); if(moving == true) { if(movingUp == true) { if(step != 20) { step = step + 2; int x = getX(); int y = getY(); int ny = getY() - 2; setLocation(x, ny); if(rotation != 270) { turn(rotationMove = 270 - rotation); } } else if(step == 20) { moving = false; movingUp = false; step = 0; } } if(movingLeft == true) { if(step != 20) { step = step + 2; int x = getX(); int y = getY(); int nx = getX()-2; setLocation(nx, y); if(rotation != 180) { turn(rotationMove = 180 - rotation); } } else if(step == 20) { moving = false; movingLeft = false; step = 0; } } if(movingRight == true) { if(step != 20) { step = step + 2; int x = getX(); int y = getY(); int nx = getX()+2; setLocation(nx, y); if(rotation != 0) { turn(rotationMove = 0 - rotation); } } else if(step == 20) { moving = false; movingRight = false; step = 0; } } if(movingDown == true) { if(step != 20) { step = step + 2; int x = getX(); int y = getY(); int ny = getY()+2; setLocation(x,ny); if(rotation != 90) { turn(rotationMove = 90 - rotation); } } else if(step == 20) { moving = false; movingDown = false; step = 0; } } } else { if(Greenfoot.isKeyDown("up")) { movingUp = true; moving = true; } if(Greenfoot.isKeyDown("down")) { movingDown = true; moving = true; } if(Greenfoot.isKeyDown("left")) { movingLeft = true; moving = true; } if(Greenfoot.isKeyDown("right")) { movingRight = true; moving = true; } } } }
SaxOps1 SaxOps1

2014/6/2

#
Ok. So, i'm working on connecting wall textures, and the corners are acting weird. They are supposed to rotate but it physically looks like they have moved, but they haven't. Code:
public void connectedTextures() 
    {
        if(canSeeUp(Wall.class))
        {
            setImage(wall2);
            rotation = 4;
            rotation();
            if(canSeeRight(Wall.class))
            {
                setImage(wall3);
                rotation = 4;
                rotation();
                if(canSeeLeft(Wall.class))
                {
                    setImage(wall4);
                    rotation = 3;
                    rotation();
                    if(canSeeDown(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeDown(Wall.class))
                {
                    setImage(wall4);
                    rotation = 4;
                    rotation();
                    if(canSeeLeft(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeLeft(Wall.class))
            {
                setImage(wall3);
                rotation = 3;
                rotation();
                if(canSeeRight(Wall.class))
                {
                    setImage(wall4);
                    rotation = 3;
                    rotation();
                    if(canSeeDown(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeDown(Wall.class))
                {
                    setImage(wall4);
                    rotation = 2;
                    rotation();
                    if(canSeeRight(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeDown(Wall.class))
            {
                setImage(wall5);
                rotation = 2;
                rotation();
                if(canSeeLeft(Wall.class))
                {
                    setImage(wall4);
                    rotation = 2;
                    rotation();
                    if(canSeeRight(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeRight(Wall.class))
                {
                    setImage(wall4);
                    rotation = 4;
                    rotation();
                    if(canSeeLeft(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
        }
        else if(canSeeLeft(Wall.class))
        {
            setImage(wall2);
            rotation = 3;
            rotation();
            if(canSeeUp(Wall.class))
            {
                setImage(wall3);
                rotation = 2;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 2;
                    rotation();
                    if(canSeeRight(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeRight(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeRight(Wall.class))
            {
                setImage(wall5);
                rotation = 1;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 3;
                    rotation();
                    if(canSeeDown(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeDown(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeDown(Wall.class))
            {
                setImage(wall3);
                rotation = 2;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 2;
                    rotation();
                    if(canSeeRight(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeRight(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
        }
        else if(canSeeDown(Wall.class))
        {
            setImage(wall2);
            rotation = 2;
            rotation();
            if(canSeeLeft(Wall.class))
            {
                setImage(wall3);
                rotation = 2;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 2;
                    rotation();
                    if(canSeeRight(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeRight(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeUp(Wall.class))
            {
                setImage(wall5);
                rotation = 1;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 3;
                    rotation();
                    if(canSeeDown(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeDown(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeRight(Wall.class))
            {
                setImage(wall3);
                rotation = 1;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 4;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeLeft(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
        }
        else if(canSeeRight(Wall.class))
        {
            setImage(wall2);
            rotation = 1;
            rotation();
            if(canSeeUp(Wall.class))
            {
                setImage(wall3);
                rotation = 3;
                rotation();
                if(canSeeRight(Wall.class))
                {
                    setImage(wall4);
                    rotation = 3;
                    rotation();
                    if(canSeeDown(Wall.class))
                    {
                       setImage(wall6); 
                    }
                }
                else if(canSeeDown(Wall.class))
                {
                    setImage(wall4);
                    rotation = 2;
                    rotation();
                    if(canSeeRight(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeLeft(Wall.class))
            {
                setImage(wall5);
                rotation = 1;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 3;
                    rotation();
                    if(canSeeDown(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeDown(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
            else if(canSeeDown(Wall.class))
            {
                setImage(wall3);
                rotation = 1;
                rotation();
                if(canSeeUp(Wall.class))
                {
                    setImage(wall4);
                    rotation = 4;
                    rotation();
                    if(canSeeLeft(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
                else if(canSeeLeft(Wall.class))
                {
                    setImage(wall4);
                    rotation = 1;
                    rotation();
                    if(canSeeUp(Wall.class))
                    {
                        setImage(wall6);
                    }
                }
            }
        }
        else
        {
            setImage(wall1);
            rotation = 1;
            rotation();
        }
You need to login to post a reply.