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

2015/10/23

I need help creating a tail for the snake any ideas? I want it to add onto the snake once it eats food.

T-rent T-rent

2015/10/23

#
public class Snake extends Animal { private int turnCounter = 0; /** * Act - do whatever the Snake wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); checkKeypress(); lookForFood(); snakeDetection(); } public void move() { move(-4); } public void checkKeypress() { if(Greenfoot.isKeyDown("left")) { setRotation(0); move(5); } if(Greenfoot.isKeyDown("up")) { setRotation(90); move(5); } if(Greenfoot.isKeyDown("right")) { setRotation(180); move(5); } if(Greenfoot.isKeyDown("down")) { setRotation(270); move(5); } } public void lookForFood() { if(canSee(Food.class)) { eat(Food.class); } } public void snakeDetection() { Actor a = getOneIntersectingObject (Snake.class); if(a != null){ World myWorld = getWorld(); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); getWorld().removeObject(a); } } private void growTail() { } }
danpost danpost

2015/10/23

#
Do you have a Tail class yet? If so, what is the code for that class? (show the entire class code so that it can be checked for accurateness) The parts of the snake (head and tail segments), at minimum, should have a reference to the tail segment, or head, that it follows it (the reference in the head should reference to end tail segment -- or itself, if not tail segments are present). With these references in place, the head (or Snake object) can control the movement and growth of the snake.
AndrewCZ AndrewCZ

2015/10/23

#
I gave it some thought and came up with quite a simple idea. I created a class called Head, which is the actor the player controls. This class holds an information about the ammount of food eaten (integer). The head moves at a constant speed and everytime it moves, it adds an object of class BodyPart, passing it the ammount of food eaten. The BodyPart class then uses that number as a counter, decreasing it by 1 every time the act() method is called and once it reaches zero, it removes itself from the world. I threw together an example and uploaded it including the source, check it out if you need to: http://www.greenfoot.org/scenarios/15254
danpost danpost

2015/10/23

#
That is quite an ingenious idea. Should work provided that the snake is in constant motion (or the field is only decreased when the head moves). I do not really need to check it out as I can envision it -- thanks.
danpost danpost

2015/10/23

#
I gave it a little more thought and realized that a slight offset in time will occur between the eating and the growing. The snake will have to move its current length before the growth is realized visually.
AndrewCZ AndrewCZ

2015/10/23

#
Yes, the snake moves every time the act() method is called. Btw, the "check it out" part was aimed at T-rent :P
You need to login to post a reply.