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

2019/6/13

NEED HELP!

bolzano bolzano

2019/6/13

#
I want to make it to be controlled by the computer and chace in order to kill the other opponent(blue) which is controlled by the player with the arrows.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

public class PlayerPink  extends Actor
{
    private GreenfootImage pink = new GreenfootImage("PinkPlayer.jpg");
    int lastMovement = Greenfoot.getRandomNumber(4);
    int newMovement = lastMovement;
    public void act()
    {
        moveTron();
        die();
        leaveTrail(getX(), getY());
    }

    /** This method allows the player to interact with the actor.
     * It detects when and which button is pressed
     * The actor will move in the direction accordingly.
     * This is done by connecting the button keys with a command
     * "last pressed", which allows it to repeate infinitely.
     */
    public void moveTron()
    {
        int z = 3;
        int decideToTurn =Greenfoot.getRandomNumber(100);
        if (decideToTurn<5)
        {
            newMovement = Greenfoot.getRandomNumber(4);

            if ((java.lang.StrictMath.abs(newMovement-lastMovement)!=1) || (newMovement*lastMovement==2)) 
            {
                lastMovement=newMovement;
            }
        }
        switch (lastMovement) {
            case 0: setLocation(getX() - z, getY());     
            break;
            case 1: setLocation(getX() + z, getY());                   
            break;
            case 2: setLocation(getX(), getY() - z);
            break;
            case 3: setLocation(getX(), getY() + z);
            break;
            default:     break;
        } 

    }

    /** This method checks whether or not pink has collided.
     *  If it is true, then the scenario will stop.
     */

    public void die()
    {
        TronWorld a=(TronWorld) getWorld();
        if (getOneObjectAtOffset(0,0,null)!=null)
        {
            a.stopTheGame("BLUE");
            getWorld().addObject(new GameOverText("PINK"), 400, 300);     

        }
    }

    /** This method requests for the world to add a static actor that is a replica of this one wherever it goes. */
    public void leaveTrail(int x, int y)
    {

        getWorld().addObject(new TrailPink(), x, y); 

    }
}
Super_Hippo Super_Hippo

2019/6/13

#
Get the player's coordinates and introduce some kind of logic how the chasing should be done. The easiest way to program is to move it directly in the direction in which the player is. And PLEASE, give discussions a meaningful name. Two "(I) NEED HELP!" discussions is too much in such a short time. I should just refuse to open anything in the future which has a name like that...
bolzano bolzano

2019/6/13

#
sorry. i am new to that and i dont know what to do. Could you please help me in how to make it follow the direction of the player
Super_Hippo Super_Hippo

2019/6/13

#
It could look like this:
Actor enemy = ((MyWorld) getWorld()).getObjects(PlayerBlue.class).get(0);
if (enemy != null)
{
    int[] directions = new int[] {-1, -1};
    int num = -1;
    if (getX() != enemy.getX())
    {
        directions[0] = getX() < enemy.getX() ? 1 : 0;
        num++;
    }
    
    if (getY() != enemy.getY())
    {
        directions[0] = getY() < enemy.getY() ? 3 : 2;
        num+=2;
    }
    
    switch (num)
    {
        case 0: case 1: lastMovement = directions[num]; break;
        case 2: lastMovement = directions[Greenfoot.getRandomNumber(2)]; break;
    }
}
This is not optimized at all, but you can try that for now.
bolzano bolzano

2019/6/14

#
Thank you very much. I will put that code at the move tron block?
Super_Hippo Super_Hippo

2019/6/14

#
Put it in the act method or in a method which is called from the act method. The moveTron method could do it, yes.
bolzano bolzano

2019/6/18

#
Actor enemy = ((MyWorld) getWorld()).getObjects(PlayerBlue.class).get(0);
if (enemy != null)
{
    int[] directions = new int[] {-1, -1};
    int num = -1;
    if (getX() != enemy.getX())
    {
        directions[0] = getX() < enemy.getX() ? 1 : 0;
        num++;
    }
     
    if (getY() != enemy.getY())
    {
        directions[0] = getY() < enemy.getY() ? 3 : 2;
        num+=2;
    }
     
    switch (num)
    {
        case 0: case 1: lastMovement = directions[num]; break;
        case 2: lastMovement = directions[Greenfoot.getRandomNumber(2)]; break;
    }
}
i placed the code there but the game doesnt run. The terminal windows shows that java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
danpost danpost

2019/6/18

#
Must be at line 1 where no PlayerBlue object is in the world. null is not a PlayerBlue object in the world and will not be put in the List object returned by getObjects. You need to check to see if the list is empty or not before trying to get a PlayerBlue object from it.
bolzano bolzano

2019/6/20

#
Thank you. I ll try that
You need to login to post a reply.