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

2018/6/26

My snake tail won't become a tail, it just sits at a fixed position, any ideas?

vaal vaal

2018/6/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SnakeHead here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeHead extends Animal
{  
    
    int speed = 20;
    int Diamondseaten = 0;
    int count = 0;
    
    
    
    /**
     * Act - do whatever the SnakeHead wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {    
        moveAround();
        if(isTouching(Diamond.class)) {
                removeTouching(Diamond.class);
                Diamondseaten++;
                
                SnakeWorld world = (SnakeWorld)getWorld();
                world.addDiamond();
        }
            
        if(isTouching(Wall.class)) {
                Greenfoot.setWorld(new SnakeWorld());  
                Greenfoot.stop();  
            }
        
        if(isTouching(SnakeTail.class)) {
                Greenfoot.setWorld(new SnakeWorld());
                Greenfoot.stop();
        }
    }
           
    private void moveAround()
    {
        count ++;
        if(count==speed){
            getWorld().addObject(new SnakeTail(Diamondseaten*speed), getX(), getY());
            move(1);
            count = 0;
        }
        
        move();
        if (Greenfoot.isKeyDown("left")) {
           turn(-25);
        }
        if (Greenfoot.isKeyDown("right")){
            turn(25);
        }
        if (Greenfoot.isKeyDown("up")){
            move();
        }
        if (Greenfoot.isKeyDown("down")){
            move(-2);
        }
    }
    
}
danpost danpost

2018/6/26

#
vaal wrote...
My snake tail won't become a tail, it just sits at a fixed position, any ideas?
You mention snake tails and you show SnakeHead class. That just is not right.
vaal vaal

2018/6/26

#
sorry my bad im a little weary, this is it
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SnakeTail here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeTail extends Animal
{   
    int lifeTime;
    int age = 0;
    
    public SnakeTail (int lifeTime) 
    {
        this.lifeTime = lifeTime;
    }
    
    /**
     * Act - do whatever the SnakeTail wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      if(age == lifeTime){
          getWorld().removeObject(this);
        }
        age++;
    } 
}
danpost danpost

2018/6/26

#
vaal wrote...
sorry my bad im a little weary, this is it << Code Omitted >>
I do not see any movement code for the tails.
You need to login to post a reply.