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

2020/11/29

Snake Game Add Tail Question

cjz19931031 cjz19931031

2020/11/29

#
I am making a Snake game. Snake increases its tail when eats food. However, the snake eats food, its tail doesn't increase.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    private int speed = 2;
    private int dx = 1;
    private int dy = 0;
    private int foodCounter = 0;
    public Snake()
    {  //Set the head of snake.
        GreenfootImage img = new GreenfootImage(10,10); //Create a new image with 20x20 as the size.
        img.setColor(Color.WHITE); //Set colour
        img.fill(); //Fill the block.
        setImage(img); //Save our changes. 
    }

    public void act()
    {
        //
        turnAround();
        move(speed);
        if(isTouching(food.class))
        {
            removeTouching(food.class);
            SnakeWorld sw = (SnakeWorld)getWorld();
            sw.addFood();
            getWorld().addObject(new tail(), getX()-1, getY()-1);
        }

    }
    public void turnAround()
    {
        if(Greenfoot.isKeyDown("up") && dx == 0)
        {
            dx = -1;
            dy = 0;
            setRotation(270);
        }
        if(Greenfoot.isKeyDown("down") && dx == 0)
        {
            dx = -1;
            dy = 0;
            setRotation(90);
        }
        if(Greenfoot.isKeyDown("left") && dy == 0)
        {
            dx = 0;
            dy = 1;
            setRotation(180);
        }
        if(Greenfoot.isKeyDown("right") && dy == 0)
        {
            dx = 0;
            dy = 1;
            setRotation(360);
        }
    }

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

/**
 * Write a description of class tail here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class tail extends Actor
{
    /**
     * Act - do whatever the tail wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int count = 0;
    public tail()
    {
        GreenfootImage img = new GreenfootImage(10,10); //Create a new image with 20x20 as the size.
        img.setColor(Color.WHITE); //Set colour
        img.fill(); //Fill the block.
        setImage(img);
    }
    public void act() 
    {
        
    }

}
danpost danpost

2020/11/29

#
I do not see where a tail moves or is removed. That is, they would seem to be permanent fixtures in your world. Also, I would question the placement location of a new tail object.
cjz19931031 cjz19931031

2020/11/29

#
At the beginning of the game, there is only one block in the world. When the snake eats food, then its tail should be increased. In line 37, I place that code. But when the snake eats fook, its tail will show up on the screen where the food was there and won't follow the head.
danpost danpost

2020/11/29

#
cjz19931031 wrote...
its tail ... won't follow the head.
Like I said, you have no code written to have the tail follow the head (moving tail) or appear to follow the head (by adding and removing tail actors). There is no act code written for tail objects and nowhere else is there any code that does anything to the tail objects in the world. Actors do not magically perform any actions they are not programmed to do.
cjz19931031 cjz19931031

2020/12/1

#
I updated my code. Now tails will follow its head, but when the snake eats food, its tail will increase after 2 second. It means that there is some delay. Are there any errors in my code?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    private int speed = 2;
    private int dx = 1;
    private int dy = 0;
    private int foodCounter = 0;
    
    public Snake()
    {  //Set the head of snake.
        GreenfootImage img = new GreenfootImage(10,10); //Create a new image with 20x20 as the size.
        img.setColor(Color.WHITE); //Set colour
        img.fill(); //Fill the block.
        setImage(img); //Save our changes. 
    }

    public void act()
    {
        getWorld().addObject(new Tail(30 * foodCounter),getX(),getY());
        turnAround();
        move(speed);
        if(isTouching(food.class))
        {
            removeTouching(food.class);
            SnakeWorld sw = (SnakeWorld)getWorld();
            sw.addFood();
            foodCounter++;
            
            
        }
        

    }
    public void turnAround()
    {
        if(Greenfoot.isKeyDown("up") && dx == 0)
        {
            dx = -1;
            dy = 0;
            setRotation(270);
        }
        if(Greenfoot.isKeyDown("down") && dx == 0)
        {
            dx = -1;
            dy = 0;
            setRotation(90);
        }
        if(Greenfoot.isKeyDown("left") && dy == 0)
        {
            dx = 0;
            dy = 1;
            setRotation(180);
        }
        if(Greenfoot.isKeyDown("right") && dy == 0)
        {
            dx = 0;
            dy = 1;
            setRotation(360);
        }
    }

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

/**
 * Write a description of class tail here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Tail extends Actor
{
    /**
     * Act - do whatever the tail wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int count = 0;
    public int life;
    public Tail(int l)
    {
        GreenfootImage img = new GreenfootImage(10,10); //Create a new image with 20x20 as the size.
        img.setColor(Color.WHITE); //Set colour
        img.fill(); //Fill the block.
        setImage(img);
        life = l;
    }
    public void act() 
    {
        
        if(count == life)
        {
            getWorld().removeObject(this);
        }
        count++;
    }
}
cjz19931031 cjz19931031

2020/12/1

#
And when the snake eats more food. The delay becomes longer and longer.
danpost danpost

2020/12/1

#
cjz19931031 wrote...
And when the snake eats more food. The delay becomes longer and longer.
Try removing "30 * " from first line in act method of Snake class.
cjz19931031 cjz19931031

2020/12/1

#
And one more question, I want to show scores at the end of the game. But it seems like doesn't work
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    private int speed = 2;
    private int dx = 1;
    private int dy = 0;
    private int foodCounter = 0;
    private int score = 0;
    private Score sc = new Score();
    public Snake()
    {  //Set the head of snake.
        GreenfootImage img = new GreenfootImage(10,10); //Create a new image with 20x20 as the size.
        img.setColor(Color.WHITE); //Set colour
        img.fill(); //Fill the block.
        setImage(img); //Save our changes. 
    }

    public void act()
    {
        if(checkEdge())
        {
            Greenfoot.setWorld(new LoseWorld());
        }
        getWorld().addObject(new Tail(foodCounter),getX(),getY());
        turnAround();
        move(speed);
        if(isTouching(food.class))
        {
            removeTouching(food.class);
            SnakeWorld sw = (SnakeWorld)getWorld();
            sw.addFood();
            foodCounter += 3;
            score = score + 5;
            sc.addScore(score);
            
        }
        

    }
    public boolean checkEdge()
    {
        return getX() == 0 || getY() == 0 || getX() == getWorld().getWidth() - 1 ||getY() == getWorld().getHeight() -1;
    }
    public void turnAround()
    {
        if(Greenfoot.isKeyDown("up") && dx == 0)//if the snake moving toward upward, then it can't turn to down.
        {
            dx = -1;
            dy = 0;
            setRotation(270);
        }
        if(Greenfoot.isKeyDown("down") && dx == 0)//if the snake moving toward downward, then it can't turn to upward.
        {
            dx = -1;
            dy = 0;
            setRotation(90);
        }
        if(Greenfoot.isKeyDown("left") && dy == 0)//if the snake moving toward left, then it can't turn to right.
        {
            dx = 0;
            dy = 1;
            setRotation(180);
        }
        if(Greenfoot.isKeyDown("right") && dy == 0)////if the snake moving toward right, then it can't turn to left.
        {
            dx = 0;
            dy = 1;
            setRotation(360);
        }
    }
    public int getScore()
    {
        return score;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private String prefix;

    private int value;
    public Score()
    {
        background = getImage();  // get image from class
        this.prefix = "Score: ";
        updateImage();

    }
    public void addScore(int s)
    {
        value = value + s;
        updateImage();
    }

    private void updateImage()
    {

        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix , 22, Color.BLACK, transparent); 
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
            (image.getHeight()-text.getHeight())/2);
        setImage(image); 
    }

}
danpost danpost

2020/12/1

#
cjz19931031 wrote...
I want to show scores at the end of the game. But it seems like doesn't work
I do not see any attempt to show score at game over.
cjz19931031 cjz19931031

2020/12/1

#
The score will always show 0;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    private int speed = 2;
    private int dx = 1;
    private int dy = 0;
    private int foodCounter = 0;
    public int score = 0;
    
    public Snake()
    {  //Set the head of snake.
        GreenfootImage img = new GreenfootImage(10,10); //Create a new image with 20x20 as the size.
        img.setColor(Color.WHITE); //Set colour
        img.fill(); //Fill the block.
        setImage(img); //Save our changes. 
    }

    public void act()
    {
        if(checkEdge())
        {
            Greenfoot.setWorld(new LoseWorld());
        }
        getWorld().addObject(new Tail(foodCounter),getX(),getY());
        turnAround();
        move(speed);
        if(isTouching(food.class))
        {
            removeTouching(food.class);
            SnakeWorld sw = (SnakeWorld)getWorld();
            sw.addFood();
            foodCounter += 3;
            score = score + 5;
            
            
        }
        

    }
    public boolean checkEdge()
    {
        return getX() == 0 || getY() == 0 || getX() == getWorld().getWidth() - 1 ||getY() == getWorld().getHeight() -1;
    }
    public void turnAround()
    {
        if(Greenfoot.isKeyDown("up") && dx == 0)//if the snake moving toward upward, then it can't turn to down.
        {
            dx = -1;
            dy = 0;
            setRotation(270);
        }
        if(Greenfoot.isKeyDown("down") && dx == 0)//if the snake moving toward downward, then it can't turn to upward.
        {
            dx = -1;
            dy = 0;
            setRotation(90);
        }
        if(Greenfoot.isKeyDown("left") && dy == 0)//if the snake moving toward left, then it can't turn to right.
        {
            dx = 0;
            dy = 1;
            setRotation(180);
        }
        if(Greenfoot.isKeyDown("right") && dy == 0)////if the snake moving toward right, then it can't turn to left.
        {
            dx = 0;
            dy = 1;
            setRotation(360);
        }
    }
    public int getScore()
    {
        return score;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private String prefix;

    private int value;
    public Score(int i)
    {
        background = getImage();  // get image from class
        this.prefix = "Score: ";
        
        value = i;
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent); 
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
            (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
    /*public void addScore(int s)
    {
        value = value + s;
        updateImage();
    }

    private void updateImage()
    {

         
    }*/

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

/**
 * Write a description of class LoseWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoseWorld extends World
{

    /**
     * Constructor for objects of class LoseWorld.
     * 
     */
    private Snake sk = new Snake();
    private int score;
    public LoseWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        score = sk.getScore();
        Score s = new Score(score);
        addObject(s, (getWidth() - 1) / 2, (getHeight() -1) / 2);
    }
}
danpost danpost

2020/12/2

#
When you create a new LoseWorld object, you create a new Snake object. Of course its score is 0. The Snake object that creates the LoseWorld object contains the score. Just pass it to the LoseWorld constructor.
You need to login to post a reply.