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

2012/11/10

Move the snake

4
5
6
7
danpost danpost

2012/11/18

#
Step-by-step comments included below (with a couple of minor modifications in the code):
// create a new segment
Snake s = new Snake(); 
// create temporary segment array of equal length
Snake[] oldSnake = new Snake[snakeBody.length];
// copy data from original array to temporary array
for(int i=0; i<snakeBody.length; i++) oldSnake[i] = snakeBody[i];
// re-create original array allowing one more element
snakeBody = new Snake[snakeBody.length+1];
// copy data from temporary array to re-created original array
for(int i=0; i<oldSnake.length; i++) snakeBody[i] = oldSnake[i];
// add the new segment to the array
snakeBody[snakeBody.length-1] = s;
// add the new segment into the world
addObject(snakeBody[s, previousLocationX, previousLocationY);
This can be slightly improved upon, with:
Snake[] oldSnake = new Snake[snakeBody.length];
System.arraycopy(snakeBody, 0, oldSnake, 0, snakeBody.length);
snakeBody = new Snake[oldSnake.length+1];
System.arraycopy(oldSnake, 0, snakeBody, 0, oldSnake.length);
snakeBody[snakeBody.length-1] = new Snake();
addObject(snakeBody[snakeBody.length-1], previousLocationX, previousLocationY);
Tezuka Tezuka

2012/11/18

#
this way I can have unlimited snakedBody right?
danpost danpost

2012/11/18

#
Tezuka wrote...
this way I can have unlimited snakedBody right?
Virtually. You have the same ability using a List object in place of the array.
Tezuka Tezuka

2012/11/19

#
Hi Danpost how can I change my world so that the snake doesnt appaer only half out.Iam not sure if u understand what I meant. When iam near the edge of the screen , my snake doesnt dies, half its body is outside.
danpost danpost

2012/11/20

#
I know exactly what you mean. You want to restrict your snake form coordinates less than 15 and greater than the width or height of the world minus 16 (instead of restricting it from less than 0 and greater than width or height minus one).
Tezuka Tezuka

2012/11/20

#
    public void checkWalls()
    {
        // check if hit the edge of screen
        if(snakeBody[0].getX()+speedX < 0 || snakeBody[0].getX() + speedX > getWidth())
        {
            die(); return;
        }

        if (snakeBody[0].getY() + speedY  < 0 || snakeBody[0].getY() + speedY > getHeight())
        {
            die();return;
        }
    }
Ya what should I do? this is my checkWalls method
danpost danpost

2012/11/20

#
Use
public void checkWalls()
    // check if at edge of screen
    if(snakeBody[0].getX() + speedX < 15 || snakeBody[0].getX() + speedX > getWidth() - 15 ||
       snakeBody[0].getY() + speedY < 15 || snakeBody[0].getY() + speedY > getHeight() - 15) die;
}
Tezuka Tezuka

2012/11/20

#
thx it worked great.
Tezuka Tezuka

2012/11/22

#
public void growSnake()
    {

        //create a new segment
        Snake s = new Snake();
        // create temporary segment array of equal length 
        Snake[] oldSnake = new Snake[snakeBody.length];
        // copy data from original array to temporary array
        for(int i=0; i<snakeBody.length; i++)
        
        {
            oldSnake[i] = snakeBody[i];
        }
        
        // re-create original array allowing one more element
        snakeBody = new Snake[snakeBody.length+1];

        // copy data from temporary array to re-created original array
        for(int i=0; i<oldSnake.length; i++)
        {
            snakeBody[i] = oldSnake[i];
        }
        // add the new segment to the array, in the last spot
        snakeBody[snakeBody.length-1] = s;
        // add the new segment into the world
        addObject( s, previousLocationX, previousLocationY);  
Hi danpost in this method the array will get increase by 1 every time I eat a snake, but is there a better way to multiplied my array by 2 when I almost reach the end of the array?
danpost danpost

2012/11/22

#
That might be better, in that you are not re-creating the array every time; but, you are not growing the snake every cycle and it would actually take more programming to accomplish (adding a variable to track the number of elements in the array and dealing with it; that, and you would still need the code to increase the size of the array). If you want to improve on the code, you might want to use an 'ArrayList' object for the array.
// add the following at the top of the world class
import java.util.List;
import java.util.ArrayList;
// declare your ArrayList instance
private List<Snake>snakeBody= new ArrayList();
// use 'snakeBody.size()' instead of 'snakeBody.length'
// add to the array with
Snake snake = new Snake();
snakeBody.add(snake);
// to get the specific snakeBody element i, use
snakeBody.get(i);
Using the 'ArrayList' you do not have to worry about the size of the array (it will take care of itself). And I think I covered everything above.
Tezuka Tezuka

2012/11/22

#
Yea thats good but iam not allowed to use arraylist.
danpost danpost

2012/11/22

#
Then it is probably best to stick with what you have or utilize the 6-liner I suggested at the top of this page.
Tezuka Tezuka

2012/11/22

#
ok thx anyway
You need to login to post a reply.
4
5
6
7