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

2012/11/10

Move the snake

1
2
3
4
Tezuka Tezuka

2012/11/10

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

/**
 * Write a description of class Playground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Playground extends World
{
    Segment[] segments;
    private int maxSegmentNum = 10;
    /**
     * Constructor for objects of class Playground.
     * 
     */
    public Playground()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        segments = new Segment[maxSegmentNum];
        segments[0] = new Segment();
        segments[1] = new Segment();
        addObject(segments[0],100,100);
        segments[0].setLocation(segments[0].getX()+20, segments[0].getY());
    }
}
Hi iam trying to move my segment but cant make it help pls
danpost danpost

2012/11/10

#
Putting the movement code (line 26) in the constructor of the world will only execute it one time, and one time only. That line needs to be in an 'act' method.
Tezuka Tezuka

2012/11/10

#
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
{
    private String segment, direction;
    private int speed, speedX, speedY, length;
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Snake(String segment)
    {
        this.segment = segment;
        direction = "right";
        speedX = speed = 20;
        speedY = 0;
        length = 3;
    }

    public void act() 
    {
        if( segment == "head")
        {
            Playground playground = (Playground)getWorld();
            for( int i = length - 1; i > 0; i--)
            {
                playground.snake[i].setLocation(playground.snake[i-1].getX(), playground.snake[i-1].getY());
            }

            keyboardControl();
            move();
            eat();
        }
    }    

    public void keyboardControl()
    {
        if(Greenfoot.isKeyDown("left") && direction !="right")
        {
            speedX = -speed;
            speedY = 0;
            direction = "left";
        }

        if(Greenfoot.isKeyDown("right") && direction !="left")
        {
            speedX = speed;
            speedY = 0;
            direction = "right";
        }

        if(Greenfoot.isKeyDown("up") && direction !="down")
        {
            speedX = 0;
            speedY = -speed;
            direction = "up";
        }

        if(Greenfoot.isKeyDown("down") && direction !="up")
        {
            speedX = 0;
            speedY = speed;
            direction = "down";
        }
    }

    public void move()
    {
        // check if hit the edge of screen

        Playground playground  = (Playground)getWorld();
        if( getX()+speedX < 0 || getX() + speedX >= 400)
        {
            die();
        }

        if (getY() + speedY  < 0 || getY() + speedY>= 400)
        {
            die();
        }
        setLocation(getX() + speedX, getY() + speedY);
        
        // check if hit the body
        Actor body = getOneObjectAtOffset(speedX, speedY, Snake.class);
        if(body !=null)
        {
            die();
        }
        
    }

    public void die()
    {
        Greenfoot.stop();
    }

       public void eat()
    {
        Actor food = getOneObjectAtOffset(0 , 0, Food.class);
        if( food !=null)
        {
            Greenfoot.playSound("snakeeat.mp3");
            
            Playground playground = (Playground)getWorld();
            playground.removeObject(food);
            playground.snake[length] = new Snake("body");
            playground.addObject(playground.snake[length], playground.snake[length - 1].getX(), playground.snake[length - 1].getY());
            
            length++;
            
            playground.addFoodRandomly();
            
        }
    }
}
My snake dies when its near the segments
Tezuka Tezuka

2012/11/10

#
nvm i got it
Tezuka Tezuka

2012/11/12

#
Could you explain this line for me? if( segment == "head") { Playground playground = (Playground)getWorld(); for( int i = length - 1; i > 0; i--) { playground.snake.setLocation(playground.snake.getX(), playground.snake.getY()); }
danpost danpost

2012/11/12

#
The inner statement moves one snake segment to the location of the segment in front of it. The 'for' loop is used to advance the whole snake forward. The code is only executed when the segment the act is running for is the head segment (moving the whole snake at once; instead of each part moving seperately, which could result in strange behaviour).
Tezuka Tezuka

2012/11/12

#
ok thx got it
Tezuka Tezuka

2012/11/13

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

/**
 * Write a description of class Playground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Playground extends World
{
    Snake[] snake;
    private int maxSegementNum = 100;
    public Food food;
    /**
     * Constructor for objects of class Playground.
     * 
     */
    public Playground()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(400, 400, 1);

        Greenfoot.setSpeed(30);

        // add the snake
        snake = new Snake[maxSegementNum];
        snake[0] = new Snake("head");
        snake[1] = new Snake("body");
        snake[2] = new Snake("body");
        addObject( snake[2], 30, 30);
        addObject( snake[1], 50, 30);
        addObject( snake[0], 70,30);
if we say that I only got this code what iam suppose to do to make the snake move?
Tezuka Tezuka

2012/11/13

#
Do I need to have the move method in the world or the Snake class?
danpost danpost

2012/11/13

#
You can move the snake with the move code in the world class or with the move code in the snake class. I have two Snake Demo scenarios; one has the movement code for the snake in the world class; the other has it in the snake class. I do not believe, however, that I used an array for the snake parts.
Tezuka Tezuka

2012/11/13

#
I need to use array do u have in other demo?
Tezuka Tezuka

2012/11/13

#
        if( segment == "head")
        {
            Playground playground = (Playground)getWorld();
            for( int i = length - 1; i > 0; i--)
            {
                playground.snake[i].setLocation(playground.snake[i-1].getX(), playground.snake[i-1].getY());
            }
I didnt get it all of it. First if i = lengh-1 = 2 and then i put it in snake = snake?? why do I need i-1 cant I just have i ??
danpost danpost

2012/11/13

#
With an array, the movement code would be something like the following:
Snake last = null;
for (int i = 99; i >= 0; i--)
{
    if (last != null) last.setLocation(snake[i].getX(), snake[i].getY());
    last = snake[i];
}
// code to move head (check for keystrokes, check for world edge, etc.)
This code can be placed in the act method of the world class. To move the head, you could just call the move() method in your snake class for the head 'snake.move();'. If in the Snake class, then only execute the code when the act is running for the lead segment (head) of the snake. That is, start the act method with the following statement 'if (!"head".equals(segment)) return;'. Also, you would either have to retrieve the array from the world class, or just run the movement code for the body of the snake by calling a method in the world class and executing the head movement in the snake class. I, myself, believe it would be best to keep things as simple as possible. Put the movement code in the world class, at least for the body of the snake; and call 'snake.move();' to move the head of the snake from within the snake class. The Snake class will not have an 'act' method; just its constructor and a 'public void move()' method that the world calls upon to move the head of the snake. It can also have methods that the 'move' method calls; such as a 'checkKeys' method, for example.
danpost danpost

2012/11/13

#
To explain this statement: playground.snake.setLocation(playground.snake.getX(), playground.snake.getY()); playground.snake refers to the segment in front of playground.snake so, we are setting the location of a follower to that of the one in front of it if this is done for all segments, than the whole snake will have moved.
Tezuka Tezuka

2012/11/13

#
I saw in ur demo that u have 2 int to save the current location and 2 int to save the last location wasnt my code above a bit easier?
There are more replies on the next page.
1
2
3
4