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

2018/6/15

How to Move Object Up According to Die Roll

studen0326 studen0326

2018/6/15

#
Hello, I am very new to Greenfoot, and programming in general. I am working on a project in which I am making a game with the same concept as snakes and ladders. This is what my board looks like : https://imgur.com/YAptEIS Here's the run-down of how the program works. The user clicks a button, which will begin the game. Each player will click on their pre-selected game pieces, which will then move to the start position. From there, each player rolls a die, and their game piece will move 100 pixels to the right if they roll a 1, 200 if they roll a 2, and so on up to 6 and 600. Now the issue that I am having is that, let's say the user's game piece is on square 8...and they roll a 6, what do I do to my code, so that the game piece moves right, up, and then left to square 14. Below is my code, sorry for the long and repetitive code...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
public class GameBoard extends World
{
    private DiceRoll dice = new DiceRoll();
    Hippo p1 = new Hippo();
    Wombat p2 = new Wombat();
    Pelican p3 = new Pelican();
    Fish p4 = new Fish();
    Spider p5 = new Spider();
    
    int[][] gridPositionX = {{350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250},
                             {350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250}};
                             
    int[] gridPositionY = {80, 180, 280, 380, 480, 580, 680, 780, 880, 980};
    public GameBoard()
    {    
       super(1350, 1050, 1);
      
       addObject(new Start_Button(), 215, 200);
         
        addObject(new PlayerOne(), 85, 350);
        addObject(p1, 200, 350);
         
        addObject(new PlayerTwo(), 85, 430);
        addObject(p2, 200, 430);
         
        addObject(new PlayerThree(), 85, 510);
        addObject(p3, 200, 510);
         
        addObject(new PlayerFour(), 85, 590);
        addObject(p4, 200, 590);
         
        addObject(new PlayerFive(), 85, 670);
        addObject(p5, 200, 670);
         
        addObject(new Welcome(), 150, 65);
         
        addObject(new ClickHere(), 94, 200);
        
        //addObject(new DiceRoll(), 125, 840);
        addObject(dice, 125, 840);
        addObject(new RollDie(), 130, 770);
         
        addObject(new Start(), 228, 930);
         
    }
    public void act()
    {
        if(Greenfoot.mouseClicked(dice))
        {
            int rollValue = dice.roll();
            JOptionPane.showMessageDialog(new JInternalFrame(), "You have rolled a: " + rollValue,"Dice Roll", JOptionPane.INFORMATION_MESSAGE);
            if (rollValue == 1)
            {
                //p1.setLocation(350, 980);
                p1.move(100);
            }
            if (rollValue == 2)
            {
                //p1.setLocation(450, 980);
                p1.move(200);
            }
            if (rollValue == 3)
            {
                //p1.setLocation(550, 980);
                p1.move(300);
            }
            if (rollValue == 4)
            {
                //p1.setLocation(650, 980);
                p1.move(400);
            }
            if (rollValue == 5)
            {
                //p1.setLocation(750, 980);
                p1.move(500);
            }
            if (rollValue == 6)
            {
                //p1.setLocation(850, 980);
                p1.move(600);
            }
            if (p1.getX() == gridPositionX[9][0] && p1.getY() == gridPositionY[9])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[6][2], gridPositionY[6]);
            }
            if (p1.getX() == gridPositionX[9][3] && p1.getY() == gridPositionY[9])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[9][8] && p1.getY() == gridPositionY[9])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[6][9], gridPositionY[6]);
            }
            if (p1.getX() == gridPositionX[8][3] && p1.getY() == gridPositionY[8])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[9][5], gridPositionY[9]);
            }
            if (p1.getX() == gridPositionX[7][0] && p1.getY() == gridPositionY[7])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[5][1], gridPositionY[5]);
            }
            if (p1.getX() == gridPositionX[7][7] && p1.getY() == gridPositionY[7])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[1][3], gridPositionY[1]);
            }
            if (p1.getX() == gridPositionX[4][9] && p1.getY() == gridPositionY[4])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[3][6], gridPositionY[3]);
            }
            if (p1.getX() == gridPositionX[4][6] && p1.getY() == gridPositionY[4])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[6][6], gridPositionY[6]);
            }
            if (p1.getX() == gridPositionX[3][1] && p1.getY() == gridPositionY[3])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[8][1], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[3][3] && p1.getY() == gridPositionY[3])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[4][0], gridPositionY[4]);
            }
            if (p1.getX() == gridPositionX[2][8] && p1.getY() == gridPositionY[2])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[0][9], gridPositionY[0]);
            }
            if (p1.getX() == gridPositionX[2][0] && p1.getY() == gridPositionY[2])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[0][1], gridPositionY[1]);
            }
            if (p1.getX() == gridPositionX[1][6] && p1.getY() == gridPositionY[1])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[6][4], gridPositionY[6]);
            }
            if (p1.getX() == gridPositionX[0][7] && p1.getY() == gridPositionY[0])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[2][7], gridPositionY[2]);
            }
            if (p1.getX() == gridPositionX[0][5] && p1.getY() == gridPositionY[0])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[2][2], gridPositionY[2]);
            }
            if (p1.getX() == gridPositionX[0][2] && p1.getY() == gridPositionY[0])
            {
                Greenfoot.delay(50);
                p1.setLocation(gridPositionX[2][1], gridPositionY[2]);
            }
        }
    }
}
Super_Hippo Super_Hippo

2018/6/15

#
Try this and report back what it does. I didn't test it:
int x = getX(), y=getY(); //current position
x += diceRoll * (y-80)%200==0 ? -1 : 1; //move left or right depending on the diceRoll and the current row
if (x > 1350) //moving out on the right side
{
    y -= 100; //move up one step
    x = 2600-x; //move one less than the number of steps which where moved outside of the grid to the left
}
else if (x < 350) //moving out on the left side
{
    y -= 100; //move up one step
    x = 600-x; //move one less than the number of steps which were moved outside of the grid to the right
}
setLocation(x, y);
It is probably easier to save a variable for each object and move in a grid array.
danpost danpost

2018/6/15

#
I suggest adding an int counter to track which grid-square the player is on. You already have the squares numbered, but java starts counting at zero, so the field will actually have a value one less than the labelled number on the square it is on. With that, initial dice roll movement might look like this, with gridNum being the field and a return value indicating game won on move:
public boolean rollMovement(int roll)
{
    for (int i=0; i<roll; i++)
    {
        int dir = (gridNum/10)%2 == 1 ? -1 : 1; //even/odd row determines direction of movement
        if (gridNum%10 == 9) setLocation(getX(), getY()-100); // end of row moves up 
        else setLocation(getX()+100*dir, getY()); // else moves in direction of movement
        gridNum++; // on next square
        Greenfoot.delay(20); // pause for about 1/3 of a second
        if (gridNum == 99) return true; // game over returns true value
    }
    return false; // not game over returns false value
}
Hippos way will probably work, but it is dependent on more than just where the player was initially placed. It uses exact grid coordinates (350 and 1350) where if you decide to change them, these values would need changed also.
You need to login to post a reply.