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

2024/12/11

snake-alike game

soap soap

2024/12/11

#
im trying to make a code where the player can eat humans, and once it does, it gets a tail of those humans (just like in snake where if the player eats an apple, the snake gets a longer tail). i dont know how to determine the position of the tail so that it follows the player. urgent!! i need help please!!
public class PlayerZombie extends BaseClass 
{
    private int humanCounter = 1;

    public void eatHuman() 
    {
        if (canSee(Human.class)) 
        {
            removeObject(Human.class);
            Level level = (Level) getWorld();   
            
            level.addScore();
            humanCounter++;
            getWorld().addObject(new Tail(humanCounter),getX(),getY());
            
        }
    }
public class Tail extends Actor
{
    public int count = 0;
    public int life;
    
    public Tail(int l)
    {
        setImage("playerfront.png");
        life = 1;
    }
    public void act() 
    {
         
        if(count == life)
        {
            getWorld().removeObject(this);
        }
        count++;
    }
}
danpost danpost

2024/12/12

#
soap wrote...
im trying to make a code where the player can eat humans, and once it does, it gets a tail of those humans (just like in snake where if the player eats an apple, the snake gets a longer tail). i dont know how to determine the position of the tail so that it follows the player. << Code Omitted >>
This is what is usually done in snake-like games: If moving and NOT eating: the current "tail" is moved to where the head is, then the head is moved. If moving AND easting: a new "tail" is placed where the head is, then the head is moved. In neither case is it necessary to"position" a tail once it is placed.
You need to login to post a reply.