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

2012/5/23

Help with movement code

DAustin15 DAustin15

2012/5/23

#
Hello, I am working on a new type of monopoly game that myself and friends have been working on for about 4 weeks now. We have the board, and the pieces but we are having trouble getting the pieces to turn when they get to the corner pieces of the board itself. We want it to be able to turn and compensate for the amount the person had rolled, we are starting to make progress but have not been fully successful.
public void yourTurn()
    {
        //turning();
        atCorner();
        movement();
        switchTurn();
        reset();

    }

    public void turning()
    {
        
        if (getX() > 599 && turningx == true || getX() < 40 && turningx == true)
        {
            turn(90);
            turningx = false;
        }
        if (getX() < 599 && turningx == false || getX() > 40 && turningx == false)
        {
            turningx = true;
        }
        if (getY() > 605 && turningy == true || getY() < 24 && turningy == true)
        {
            turn(90);
            turningy = false;
        }
        if (getY() < 605 && turningy == false || getY() > 24 && turningy == false)
        {
            turningy = true;
        }
    }

    public void movement()
    {
        if (Die1.rollValue1 + Die2.rollValue2 == 2)
        {
            move(123);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 3)
        {
            move(186);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 4)
        {
            move(249);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 5)
        {
            move(312);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 6)
        {
            move(375);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 7)
        {
            move(438);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 8)
        {
            move(440);           
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 9)
        {
            move(495);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 10)
        {
            move(550);           
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 11)
        {
            move(605);            
        }
        if (Die1.rollValue1 + Die2.rollValue2 == 12)
        {
            move(660);            
        }
    }
danpost danpost

2012/5/23

#
Tip: to make programming the movement code easier, make the distances between each stop location equal. I see differences from 2 (see rolls of 7 and 8) to 63 (see rolls of 6 and 7). I have a feeling the 2 is a mistake, but even so, you have 55s (between any 2 from 8 through 12). Then, you could put the sum of the die in a 'for' loop to move that distance that many times. The 'for' loop can call another method to iterate through the multiple moves (for one step) and check for limits and adjust direction, if neccessary. It would take 4 simple checks to verify direction. I see it as (assuming you made each displacement from stop to stop on the board equal to 60 pixels) a 'makeMove(int)' method (called by 'makeMove(Die1.rollValue1 + Die2.rollValue2);'), calling a 'moveStep' method as follows:
private void makeMove(int dieSum)
{
    for (int i = 0; i < dieSum; i++) moveStep();
}

private void moveStep()
{
    for (int i = 0; i < 60; i++)
    {
        checkTurn();
        move();
    }
}

private int direction = 0;

private void checkTurn();
{
    boolean chgDirection = false;
    if (getY() == <rightmost point> && direction == 0) chgDirection = true;
    if (getX() == <lowest point>  && direction == 1) chgDirection = true;
    if (getY() == <leftmost point> && direction == 2) chgDirection = true;
    if (getX() == <highest point> && direction == 3) chgDirection = true;
    if (chgDirection = true) direction = (direction + 1) % 4;
}

private void move()
{
    int dx = ((3 - direction) % 2) * (1 - direction);
    int dy = ((4 - direction) % 2) * (2 - direction);
    setLocation(getX() + dx, getY() + dy);
}
danpost danpost

2012/5/23

#
On second thought, you do not have to check for direction change each pixel, just each step. Therefore change lines 1 through 13 (removing 'moveStep') as follows
private void makeMove(int dieSum)
{
    for (int i = 0; i < dieSum; i++) 
    {
        checkTurn();
        move();
    }
}
and then, multiply in lines 29 and 30 both by 60; OR multiply both dx and dy in line 31 by 60.
DAustin15 DAustin15

2012/5/24

#
Thankyou for your help, we are trying to implement the code but the movement doesn't seem to be working right still.
DAustin15 DAustin15

2012/5/24

#
public class GamePieces extends Actor
{
    // boolean turningx = true;
    //boolean turningy = true;
    // boolean going = true;
    //boolean corner = false;
    private int dieSum = Die1.rollValue1 + Die2.rollValue2;
    public void yourTurn()
    {
        //turning();
        //atCorner();
        //movement();
        makeMove();
        switchTurn();
        reset();

    }

    public void makeMove()
    {
        for (int i = 0; i < Die1.rollValue1 + Die2.rollValue2; i++) 
        {
            checkTurn();
            move();
        }
    }
    private int direction = 0;
    public void checkTurn()
    {
    
        boolean chgDirection = false;
        if (getY() == 623 && direction == 0) chgDirection = true;
        if (getX() ==  607 && direction == 1) chgDirection = true;
        if (getY() == 41 && direction == 2) chgDirection = true;
        if (getX() == 34 && direction == 3) chgDirection = true;
        if (chgDirection = true) direction = (direction + 1) % 4;
    }

    public void move()
    {
        int dx = ((3 - direction) % 2) * (1 - direction);
        int dy = ((4 - direction) % 2) * (2 - direction);
        setLocation(getX() + dx*60, getY() + dy*60);
    }

    
    /*public void turning()
    {

    if (getX() > 599 && turningx == true || getX() < 40 && turningx == true)
    {
    turn(90);
    turningx = false;
    }
    if (getX() < 599 && turningx == false || getX() > 40 && turningx == false)
    {
    turningx = true;
    }
    if (getY() > 605 && turningy == true || getY() < 24 && turningy == true)
    {
    turn(90);
    turningy = false;
    }
    if (getY() < 605 && turningy == false || getY() > 24 && turningy == false)
    {
    turningy = true;
    }
    }
     */
    /*public void movement()
    {
    if (Die1.rollValue1 + Die2.rollValue2 == 2)
    {
    move(123);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 3)
    {
    move(186);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 4)
    {
    move(249);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 5)
    {
    move(312);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 6)
    {
    move(375);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 7)
    {
    move(438);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 8)
    {
    move(440);           
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 9)
    {
    move(495);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 10)
    {
    move(550);           
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 11)
    {
    move(605);            
    }
    if (Die1.rollValue1 + Die2.rollValue2 == 12)
    {
    move(660);            
    }
    }
     */
    public void switchTurn()
    {
        if (Chrome.turn && Die1.rollValue1 + Die2.rollValue2 != 0)
        {
            reset();
            Chrome.turn = false;
            Safari.turn = true;
        }
        if (Safari.turn && Die1.rollValue1 + Die2.rollValue2 != 0)
        {
            reset();
            Safari.turn = false;
            Explorer.turn = true;
        }
        if (Explorer.turn && Die1.rollValue1 + Die2.rollValue2 != 0)
        {
            reset();
            Explorer.turn = false;
            Firefox.turn = true;
        }
        if (Firefox.turn && Die1.rollValue1 + Die2.rollValue2 != 0)
        {
            reset();
            Firefox.turn = false;
            Chrome.turn = true;
        } 
    }

    /*
    public void atCorner()
    {
    Actor go = getOneIntersectingObject(Go.class);
    if(go != null)
    {
    turn(90);
    }
    Actor jail = getOneIntersectingObject(Jail.class);
    if(jail != null)
    {
    turn(90);
    }
    Actor freeWifi = getOneIntersectingObject(FreeWifi.class);
    if(freeWifi != null)
    {
    turn(90);
    }
    Actor goToJail = getOneIntersectingObject(GoToJail.class);
    if(goToJail != null)
    {
    turn(90);
    }
    }
     */
    public void reset()
    {
        if (Die1.rollValue1 + Die2.rollValue2 != 0)
        {
            Die1.rollValue1 = 0;
            Die2.rollValue2 = 0;
        }        
    }
}
danpost danpost

2012/5/24

#
What is the size of your game board and what are the locations of the centers of the four corners?
DAustin15 DAustin15

2012/5/24

#
the world is 850 x 850 the game board is about 666 x 650 the locations of the 4 centers of the corners is 620,46 then 622,610 then 42,606 and last 44,44 we would like it to turn once it gets to the corners but it can land there, i appreciate your help with our project it is really helping us.
DAustin15 DAustin15

2012/5/24

#
i wasnt sure if i did the dieSum right, the movement doesnt seem to work right yet, but it is a improvement and it will greatly help once its done.
DAustin15 DAustin15

2012/5/24

#
actually it should be the same y coordinate for the first and last corner so 44 and it should be the same for the 2nd and 3rd so 623,608 and 44,608
danpost danpost

2012/5/24

#
DAustin15 wrote...
i wasnt sure if i did the dieSum right, the movement doesnt seem to work right yet, but it is a improvement and it will greatly help once its done.
You can remove line 7, as the value was hard-coded in on line 21. Are there 11 non-corners and 2 corners along each side of the board?
DAustin15 DAustin15

2012/5/24

#
this is a picture but there are 9 non corners on each side and 4 actual corners on the board http://tinypic.com/r/rh7viq/6
danpost danpost

2012/5/24

#
Well, after having a look at the board, I can tell, that this will not be as easy as I had believed at first. Also, with 4 pieces on the board at the same time, you certainly do not what them landing exactly on the same spot when on the same square. That adds another level of complexity. However, with the proper helper objects, and object image manipulation, I believe we can make it work. I am thinking about an array of Pad objects (invisible 'landing' pads for the pieces; 40 of them; one for each spot on the board that a piece can land on). They each can have a reference to the spot in front of it, so movement can be from spot to spot by using the locations of the pads. As far as the pieces not overlapping each other: take the image of each of the four pieces and draw each one of them onto a new image that is twice as high and twice as wide. Draw each one in a different quadrant of the new images. Then we can place the images at the same location as the pads, and they will not overlap.
You need to login to post a reply.