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

2015/3/24

SnakeGame Growing Snake

ZOE ZOE

2015/3/24

#
Hi everyone! Having BIG time problem producing an Snake game in Greenfoot. The problem is that I want to do my snake grow. I have this in my world: public class SnakeWorld extends World { ... public void makeSnakeBody() { snakeBodyParts = getObjects(SnakeHead.class); sizeList = snakeBodyParts.size(); if(snakeBodyParts.isEmpty()){ snakeBodyParts.add(0, new SnakeHead()); addObject(snakeBodyParts.get(0),200,60); for(int i=1; i<6;i++){ snakeBodyParts.add(i, new SnakeBody()); addObject(snakeBodyParts.get(i),snakeBodyParts.get(0).getX() - i*CONST,snakeBodyParts.get(0).getY()); } snakeBodyParts.add(6, new SnakeTail()); addObject(snakeBodyParts.get(6),snakeBodyParts.get(0).getX() - 6*CONST,snakeBodyParts.get(0).getY()); } else { mouse = getObjects(Mouse.class); if(snakeBodyParts.get(0).getX()== mouse.get(0).getX() && snakeBodyParts.get(0).getY()== mouse.get(0).getY()) { snakeBodyParts.remove(sizeList-1); snakeBodyParts.add(sizeList-1, new SnakeBody()); addObject(snakeBodyParts.get(sizeList-1),snakeBodyParts.get(0).getX() - (sizeList-1)*CONST,snakeBodyParts.get(0).getY()); snakeBodyParts.add(sizeList, new SnakeTail()); addObject(snakeBodyParts.get(sizeList),snakeBodyParts.get(0).getX() - sizeList*CONST,snakeBodyParts.get(0).getY()); } } } But when the SnakeHead detects an object she eats but not growing. I think it's because of the way that defines the " eat" in SnakeHead class. I have this in my class: public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } I don't know! Someone can help me?? Thank you!!
danpost danpost

2015/3/24

#
The 'eat' method looks okay to me. What is the code around which you are calling the 'eat' method in the SnakeHead class?
ZOE ZOE

2015/3/24

#
it's this: public void lookForMouse() { if(canSee(Mouse.class)){ eat(Mouse.class); Greenfoot.playSound("Mastigar.wav"); updateFoodLocation(); SnakeWorld world = (SnakeWorld) getWorld(); ScoreCounter counter = world.getScore(); counter.addScore(5); }
danpost danpost

2015/3/24

#
There is no code in the method to add another SnakeBody to the snake.
ZOE ZOE

2015/3/25

#
I do that in the world! public void act() { }
ZOE ZOE

2015/3/25

#
I do that in the world! public void act() { makeSnakeBody(); }
danpost danpost

2015/3/25

#
'updateFoodLocation' will put the mouse elsewhere before the world realizes that it WAS at the snake head location and eaten.
ZOE ZOE

2015/3/25

#
I need to put makeSnakeBody in the method "lookForWorms" right?
danpost danpost

2015/3/25

#
ZOE wrote...
I need to put makeSnakeBody in the method "lookForWorms" right?
If the method adds one body part to the existing snake -- yes. I have not yet deciphered exactly what that method does (or is supposed to do)..
ZOE ZOE

2015/3/25

#
Well after you said that i need to put my growSnake method in the "lookForMouse" method in the world i have something like that: public void makeSnakeBody() { snakeBodyParts = getObjects(SnakeHead.class); sizeList = snakeBodyParts.size(); if(snakeBodyParts.isEmpty()){ snakeBodyParts.add(0, new SnakeHead()); addObject(snakeBodyParts.get(0),200,60); for(int i=1; i<6;i++){ snakeBodyParts.add(i, new SnakeBody()); addObject(snakeBodyParts.get(i),snakeBodyParts.get(i-1).getX() - CONST,snakeBodyParts.get(i-1).getY()); } snakeBodyParts.add(6, new SnakeTail()); addObject(snakeBodyParts.get(6),snakeBodyParts.get(5).getX() - CONST,snakeBodyParts.get(5).getY()); } } public void growSnake() { snakeBodyParts.remove(sizeList-1); snakeBodyParts.add(sizeList - 1 , new SnakeBody()); addObject(snakeBodyParts.get(sizeList-1),snakeBodyParts.get(sizeList-2).getX() - CONST,snakeBodyParts.get(sizeList-2).getY()); snakeBodyParts.add(sizeList , new SnakeTail()); addObject(snakeBodyParts.get(sizeList),snakeBodyParts.get(sizeList-1).getX() - CONST,snakeBodyParts.get(sizeList-2).getY()); } In "lookForMouse" world i put the growSnake method but something are happening wrong yet. I'm try to find what! Maybe is because i have the "makeSnakeBody" in the act() of the world!
danpost danpost

2015/3/25

#
Looking over your methods, I found only one issue: the last line of the 'growSnake' method gets the y coordinate from the wrong element in the list ( 'sizeList-2' should be 'sizeList-1' ); I am getting a strong indication that the 'SnakeBody' class is extending the 'SnakeHead' class. Am I correct?
ZOE ZOE

2015/3/26

#
Yes, you're right!
ZOE ZOE

2015/3/26

#
When she it's food, have something stupid happening, when she moves the body parts I added do not follow it and I remove the tail but it is not removed.
danpost danpost

2015/3/26

#
ZOE wrote...
Yes, you're right!
If you cannot say that something is a type of something else, then that something else should not extend that something. A head is not a type of body and a body is not a type of head; neither one should extend the other. SnakeWorld is a type of world -- no problem. SnakeHead is a type of Actor -- again, no problem. SnakeBody is also a type of Actor, but it is not a type of SnakeHead.
ZOE ZOE

2015/3/26

#
I need to thanks you a lot for helping but i found the problem and my snake now is growing!!! Thank you!
You need to login to post a reply.