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

2012/5/20

Snake Game

Jesse_Orange Jesse_Orange

2012/5/20

#
I have all i need for a Snake game but I'm having difficulty creating the array so that the tail follows the head.
 private void addSnakeBody()
    
    {
         int currX = getX(); //get current x co-ordinate
         int currY = getY(); // get current y co-ordinate 
         
         getWorld().addObject(new SnakeBody(), currX-offsetX, currY-offsetY); //add SnakeBody at location of SnakeHead
         
         }
This part in SnakeHead adds a SnakeBody
 private void growSnake()
    {
         int offsetX=0;
         int offsetY=0;
         
         int currX = getX(); //get current x co-ordinate
         int currY = getY(); // get current y co-ordinate 
         
         List getSnakeHead=getWorld().getObjects(SnakeHead.class); //get the SnakeHead actor from the world.
         if(!getSnakeHead.isEmpty  ()) //if there is no SnakeHead
         {
            SnakeHead thisSnakeHead=(SnakeHead)getSnakeHead.get(0); //is there a SnakeHead in the current location?
            offsetX=thisSnakeHead.offsetX; //the x co-ordinate of SnakeHead
            offsetY=thisSnakeHead.offsetY; //the y co-ordinate of SnakeHead
        }
        setLocation(currX+offsetX, currY+offsetY); //
    }
This code adds a SnakeBod next to SnakeHead but because I don't have an array so the body bits pile up behind the head. HELP :(
danpost danpost

2012/5/20

#
With what I see here, you have all snakeBods following the head; where you need all snakeBods to follow the snakeBod in front of it (or the head, if the first snakeBod). To accomplish this, you will need an instance Actor variable in your SnakeBod class to be a reference to the actor it is following. In the first SnakeBod, it will be a reference to the SnakeHead; in the one behind it, it will be a reference to a SnakeBod. It would be simpler, if each had a reference to the SnakeBod behind it. That way (1) if the value is not 'null', you know it is of type 'SnakeBod', and (2) you can put all the movement code for both the SnakeHead and the SnakeBods in the world class. I wrote a simple demo for a snake. If you want to take a look, I can post it. Just let me know.
Jesse_Orange Jesse_Orange

2012/5/20

#
Could you post the demo please? Thanks :) - I mean yes that makes sense, I'm just unsure how I would actually accomplish this?
danpost danpost

2012/5/20

#
It is up! You can find it here.
Jesse_Orange Jesse_Orange

2012/5/20

#
I see much of your coding is in the world uitself, is there anyway to implement this within the actors themselves rather than the world?
danpost danpost

2012/5/20

#
I'll see what I can do. I think by making some static methods and variables within the Snake class, I can accomplish it.
danpost danpost

2012/5/20

#
It is up! Another snake demo 'Snake Demo 2', with only the initial creation of the snake head and one body segment in the world class. This one is here.
Jesse_Orange Jesse_Orange

2012/5/20

#
So this new version uses variables in the Snake rather than the world? Was it difficult to do?
danpost danpost

2012/5/20

#
Not really, most of it was just transferring the code from the world to the Snake class. The major adjustments were in the static fields (Class variables), where I needed to track the head, its direction, and the tail. Because the movement was now in the Snake class, I had to make sure that only when the head was acted on, that the Snake actually moves (and possibly grows). The only other thing was the clearing of the Snake class 'head' and 'direction' variables in the world constructor (because static fields retain their values even when the program is reset; only re-compiling will reset them otherwise).
danpost danpost

2012/5/20

#
Try comparing the two sets of code. You really will find only minor differences, other than what I mentioned above.
Jesse_Orange Jesse_Orange

2012/5/20

#
Stupid question - how do I compare the code?
danpost danpost

2012/5/20

#
Click the big green 'Open in Greenfoot' button to the right and a little above the scenario window on both scenarios to download them. Then, open both of them using Greenfoot.
Jesse_Orange Jesse_Orange

2012/5/21

#
There is no button.
danpost danpost

2012/5/26

#
Sorry for the slow response. Somehow, I do not think I am recieving all my 'new' notifications; though, they are all showing up in the 'See all' notifications page. Just noticed this today. :+( Evidently, after adding comments, I forgot to include source during update. It is up now! :+)
You need to login to post a reply.