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

2018/6/15

How to move my object up on a board according to the die roll?

pablo0826 pablo0826

2018/6/15

#
Hi, I am very new to Greenfoot, and programming in general. I am working on a game in Greenfoot. You have probably noticed that I posted another question as well, well now I have another one :) In my game, the user clicks a button to begin, They then select the game pieces that they wish to play with which then move to the start location. After this, they roll the die, which will move the game piece 100 pixels to the right if it is a 1, 200 if it's a 2 and so on. The problem that I have now encountered is, if you have probably seen a snakes and ladders board,here's an image of what I've designed: Once the game piece reaches...let's say 8, and then they roll a a 6. What can I do to the code, that I have attached below to make the game piece do that? Can someone explain in simplest code possible, I am very new, and sorry for the extremely 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();
    //private Actor playerUp = p1;
    
    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 static void main(String[] args)
    {
        int[][] gridPosition = 
    }*/
    public GameBoard()
    {    
       super(1350, 1050, 1);
     
       addObject(new Start_Button(), 215, 200);
        
        addObject(new PlayerOne(), 85, 350);
        //addObject(new Hippo(), 200, 350);
        addObject(p1, 200, 350);
        
        addObject(new PlayerTwo(), 85, 430);
        //addObject(new Wombat(), 200, 430);
        addObject(p2, 200, 430);
        
        addObject(new PlayerThree(), 85, 510);
        //addObject(new Pelican(), 200, 510);
        addObject(p3, 200, 510);
        
        addObject(new PlayerFour(), 85, 590);
        //addObject(new Fish(), 200, 590);
        addObject(p4, 200, 590);
        
        addObject(new PlayerFive(), 85, 670);
        //addObject(new Spider(), 200, 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[][] 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};*/
        
            int rollValue = dice.roll();
            JOptionPane.showMessageDialog(new JInternalFrame(), "You have rolled a: " + rollValue,"Dice Roll", JOptionPane.INFORMATION_MESSAGE);
            if (Greenfoot.isKeyDown("space"))
            {
                Greenfoot.stop();
            }
            if (Greenfoot.isKeyDown("p"))
            {
                Greenfoot.start();
            }
            if (rollValue == 1)
            {
                //p1.setLocation(gridPositionX[9][0], gridPositionY[9]);
                p1.move(100);
                //p1.move(p1.getX() + 100);
                if ((p1.getX() + 100) > 1050)
                {
                    p1.setLocation(p1.getX(), p1.getY()-100);
                }
            }
            if (rollValue == 2)
            {
                //p1.setLocation(gridPositionX[9][1], gridPositionY[9]);
                p1.move(200);
                //p1.move(p1.getX() + 200);
            }
            if (rollValue == 3)
            {
                //p1.setLocation(gridPositionX[9][2], gridPositionY[9]);
                p1.move(300);
                //p1.move(p1.getX() + 300);
            }
            if (rollValue == 4)
            {
                //p1.setLocation(gridPositionX[9][3], gridPositionY[9]);
                p1.move(400);
                //p1.move(p1.getX() + 400);
            }
            if (rollValue == 5)
            {
                //p1.setLocation(gridPositionX[9][4], gridPositionY[9]);
                p1.move(500);
                //p1.move(p1.getX() + 500);
            }
            if (rollValue == 6)
            {
                //p1.setLocation(gridPositionX[9][5], gridPositionY[9]);
                p1.move(600);
                //p1.move(p1.getX() + 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[8][3] && p1.getY() == gridPositionY[8])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[9][5], gridPositionY[9]);
            }
            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[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[9][3] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[2][8] && p1.getY() == gridPositionY[2])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[0][9], gridPositionY[0]);
            }
            if (p1.getX() == gridPositionX[9][3] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[2][0] && p1.getY() == gridPositionY[2])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[0][1], gridPositionY[0]);
            }
            if (p1.getX() == gridPositionX[9][3] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[1][6] && p1.getY() == gridPositionY[1])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[6][4], 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[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][5], gridPositionY[2]);
            }
            if (p1.getX() == gridPositionX[0][2] && p1.getY() == gridPositionY[0])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[2][1], gridPositionY[2]);
            }
            //JOptionPane.showMessageDialog(new JInternalFrame(), "Player 2, please roll the die", "Next Turn", JOptionPane.INFORMATION_MESSAGE);

            /*int rollValue2 = dice.roll();
            JOptionPane.showMessageDialog(new JInternalFrame(), "Player 2, please roll the die", "Next Turn", JOptionPane.INFORMATION_MESSAGE);
            
            if (rollValue2 == 1)
            {
                p2.setLocation(gridPositionX[9][0], gridPositionY[9]);
                //p1.move(100);
                
            }
            if (rollValue2 == 2)
            {
                p2.setLocation(gridPositionX[9][1], gridPositionY[9]);
                //p1.move(200);
     
            }
            if (rollValue2 == 3)
            {
                p1.setLocation(gridPositionX[9][2], gridPositionY[9]);
                //p1.move(300);
            }
            if (rollValue2 == 4)
            {
                p1.setLocation(gridPositionX[9][3], gridPositionY[9]);
                //p1.move(400);
            }
            if (rollValue2 == 5)
            {
                p1.setLocation(gridPositionX[9][4], gridPositionY[9]);
                //p1.move(500);
            }
            if (rollValue2 == 6)
            {
                p1.setLocation(gridPositionX[9][5], gridPositionY[9]);
                //p1.move(600);
            }
            if (p2.getX() == gridPositionX[9][0] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[6][2], 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[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[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[9][3] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[2][8] && p1.getY() == gridPositionY[2])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[0][9], gridPositionY[0]);
            }
            if (p1.getX() == gridPositionX[9][3] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[2][0] && p1.getY() == gridPositionY[2])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[0][1], gridPositionY[0]);
            }
            if (p1.getX() == gridPositionX[9][3] && p1.getY() == gridPositionY[9])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[8][6], gridPositionY[8]);
            }
            if (p1.getX() == gridPositionX[1][6] && p1.getY() == gridPositionY[1])
            {
                    Greenfoot.delay(50);
                    p1.setLocation(gridPositionX[6][4], 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[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][5], 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

#
Did you create two accounts and ask the same question twice? Why? Question answered in the other discussion: https://www.greenfoot.org/topics/61062/
You need to login to post a reply.