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

2015/1/20

dont want to run into self!HELP??

Moho Moho

2015/1/20

#
so i basically made the game tron having 2 players who run around with trails behind them now the game is basically complete but i want to know not to run into myself. i tried doing if can see redtrail (trail of the red player) but when the game starts the game ends because the trail got placed behind the red player right away. heres my code. for player
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Red here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Red extends Players
{
    /**
     * Act - do whatever the Red wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        Trail();
        Screen();

    } 

    private int xDirection, yDirection;

    public void move()
    {
        int dx = 0;
        int dy = 0;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (Greenfoot.isKeyDown("up")) dy--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (dx*dy == 0 && dx+dy != 0)
        {
            xDirection = dx;
            yDirection = dy;
        }
        setLocation(getX()+4*xDirection, getY()+4*yDirection);
    }

    public void Screen()
    {
        if(canSee(BlueTrail.class))
        {
            LoseScreen();
        }
        if(canSee(Blue.class))
        {
            NoWinner();

        }
        if(canSee(RedTrail.class))
        {

        }
        if(canSee(YellowTrail.class))
        {
            Greenfoot.stop();
        }
        if(canSee(Yellow.class))
        {
            Greenfoot.stop();
        }

    }

    public void LoseScreen()
    {
        Greenfoot.stop();

    }

    public void NoWinner()
    {
        String text = "Game Over!It is a Draw!";  
        Greenfoot.stop();

    }

    public void Trail()
    {
        int xOff = 5;
        int yOff = 5;
        getWorld().addObject(new RedTrail(), getX(), getY());
    }

}
danpost danpost

2015/1/20

#
For a start, I would switch the order of calls to 'Trail' and 'move' in the act method. That way, the trail will always be behind. Then, change the method for looking for trails. The method 'canSee' checks for any intersection. Maybe '!getObjectsAtOffset(xDirection*5, yDirection*5, RedTrail.class).isEmpty()' or something similar might be closer to what you need. I cannot say, for sure, if it will be sufficient as is, because the size of the trails is not given. Something that may also help is placing the trails slightly more behind instead of placing them at the current location.
Moho Moho

2015/1/20

#
how would i place them more behind??
danpost danpost

2015/1/20

#
Use something like:
getWorld().addObject(new RedTrail(), getX()-xDirection*5, getY()-yDirection*5);
Moho Moho

2015/1/20

#
i tried but it still didnt work. i programmed this extact game a year ago but i got a new computer so i cant go back i just dont recall how i did it.
You need to login to post a reply.