Did you move the 'head' of the snake before the 'for' loop?


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[] snakeBody; String segment; int direction; private int speed; private int speedX; private int speedY; boolean eaten; /** * 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); direction = 0; eaten = false; speed = 2; speedX = speed; speedY = 0; snakeBody = new Snake[3]; // initialization of the Snake for(int i=0; i<snakeBody.length; i++) { snakeBody[i] = new Snake(); addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world } } public void checkKey() { if(Greenfoot.isKeyDown("right")) // checks if right key is pressed { if(direction==1 || direction==3) { direction = 0; speedX = speed; speedY = 0; } } else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed { if(direction==0 || direction==2) {direction = 1; speedX = 0; speedY = speed; } } else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed { if(direction==1 || direction==3) {direction = 2; speedX = -speed; speedY = 0; } } else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed { if(direction==0 || direction==2) { direction = 3; speedX = 0; speedY = -speed; } } } public void moveIt() { int previousLocationX = snakeBody[0].getX(); int previousLocationY = snakeBody[0].getY(); snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY); if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) { for(int i=1; i<snakeBody.length; i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); snakeBody[i].setLocation(previousLocationX, previousLocationY); previousLocationX = holdX; previousLocationY = holdY; } } } /** * This method will update the length of the Snake. */ private void growSnake(int x, int y) { Snake s = new Snake(); Snake oldSnake[] = new Snake[snakeBody.length]; for(int i=0; i<snakeBody.length; i++) { oldSnake[i] = snakeBody[i]; } snakeBody = new Snake[snakeBody.length+1]; for(int i=0; i<snakeBody.length-1; i++) { snakeBody[i] = oldSnake[i]; } snakeBody[snakeBody.length-1] = s; addObject(snakeBody[snakeBody.length-1], x, y); // adds new snake body actor } public void act() { checkKey(); moveIt(); } }
private void moveIt() { // save the location of the 'head' segment (segment in front) // these are the 'goto' values for the next segment int prevX = snakeBody[0].getX(); int prevY = snakeBody[0].getY(); // get the direction of movement for the 'head' along horizontal and vertical (0, 1, or -1) int dx = (1 - direction) * ((direction + 1)%2); int dy = (2 - direction) * (direction%2); // get tentative new coordinates for the 'head' int newX = prevX + speed * dx; int newY = prevY + speed * dy; // if 'head' cannot move, exit method if (newX < 0 || newX > getWidth() - 1) return; if (newY < 0 || newY > getHeight() - 1) return; // move head snakeBody[0].setLocation(newX, newY); // move body segments of snake for (int i = 1; i < snakeBody.length(); i++) { // hold segments location int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); // move segment snakeBody[i].setLocation(prevX, prevY); // set 'goto' values for next segment prevX = holdX; prevY = holdY; } }
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[] snakeBody; String segment; Food food; Counter score; private int direction; private int speed; private int speedX; private int speedY; private boolean eaten; /** * 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); direction = 0; eaten = false; speed = 20; speedX = speed; speedY = 0; snakeBody = new Snake[3]; // initialization of the Snake for(int i=0; i<snakeBody.length; i++) { snakeBody[i] = new Snake(); addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world } score = new Counter("Score : "); // initialization of the score addObject(score, 19*15, 8); // adding score actor to the world } public void checkKey() { if(Greenfoot.isKeyDown("right")) // checks if right key is pressed { if(direction==1 || direction==3) { direction = 0; speedX = speed; speedY = 0; } } else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed { if(direction==0 || direction==2) {direction = 1; speedX = 0; speedY = speed; } } else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed { if(direction==1 || direction==3) {direction = 2; speedX = -speed; speedY = 0; } } else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed { if(direction==0 || direction==2) { direction = 3; speedX = 0; speedY = -speed; } } } /** public void moveIt() { // save the location of the 'head' segment (segment in front) // these are the 'goto' values for the next segment int prevX = snakeBody[0].getX(); int prevY = snakeBody[0].getY(); // get the direction of movement for the 'head' along horizontal and vertical (0, 1, or -1) int dx = (1 - direction) * ((direction + 1)%2); int dy = (2 - direction) * (direction%2); // get tentative new coordinates for the 'head' int newX = prevX + speed * dx; int newY = prevY + speed * dy; // if 'head' cannot move, exit method if (newX < 0 || newX > getWidth() - 1) return; if (newY < 0 || newY > getHeight() - 1) return; // move head snakeBody[0].setLocation(newX, newY); // move body segments of snake for (int i = 1; i < snakeBody.length; i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); // move segment snakeBody[i].setLocation(prevX, prevY); // set 'goto' values for next segment prevX = holdX; prevY = holdY; } } */ public void moveIt() { // save the location of the 'head' segment (segment in front) // these are the 'goto' values for the next segment int previousLocationX = snakeBody[0].getX(); int previousLocationY = snakeBody[0].getY(); snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY); // move body segments of snake if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) { for(int i=1; i<snakeBody.length; i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); snakeBody[i].setLocation(previousLocationX, previousLocationY); // set 'goto' values for next segment previousLocationX = holdX; previousLocationY = holdY; } if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food { growSnake(previousLocationX, previousLocationY); updateFoodLocation(); score.add(5); } } else { Greenfoot.stop(); } } /** * This method will update the length of the Snake. */ private void growSnake(int x, int y) { Snake s = new Snake(); Snake oldSnake[] = new Snake[snakeBody.length]; for(int i=0; i<snakeBody.length; i++) { oldSnake[i] = snakeBody[i]; } snakeBody = new Snake[snakeBody.length+1]; for(int i=0; i<snakeBody.length-1; i++) { snakeBody[i] = oldSnake[i]; } snakeBody[snakeBody.length-1] = s; addObject(snakeBody[snakeBody.length-1], x, y); // adds new snake body actor } public void updateFoodLocation() { int x = 0; int y = 0; boolean overlap = true; eaten = false; while(overlap) { x = Greenfoot.getRandomNumber(getWidth()); y = Greenfoot.getRandomNumber(getHeight()); for( int i = 0; i < snakeBody.length; i++) { // Condition to check food will not touch the snake if(x!=snakeBody[i].getX() || y!= snakeBody[i].getY()) { overlap = false; break; } } } } public void act() { checkKey(); moveIt(); } }
public void moveIt() { // save the location of the 'head' segment (segment in front) // these are the 'goto' values for the next segment int previousLocationX = snakeBody[0].getX(); int previousLocationY = snakeBody[0].getY(); snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY); // move body segments of snake if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) { for(int i=1; i<snakeBody.length; i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); snakeBody[i].setLocation(previousLocationX, previousLocationY); // set 'goto' values for next segment previousLocationX = holdX; previousLocationY = holdY; } if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food { growSnake(previousLocationX, previousLocationY); updateFoodLocation(); score.add(5); } for(int i=1; i<snakeBody.length; i++) // Checks if the snake eats itself { if(snakeBody[0].getX()== snakeBody[i].getX() && snakeBody[0].getY()== snakeBody[i].getY()) { Greenfoot.stop(); } } } else { Greenfoot.stop(); } }
// replace all code starting at line 28 with if (getObjectsAt(snakeBody[0].getX(), snakeBody[0].getY(), null).size() == 1) return; } Greenfoot.stop(); }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Playground here. * * @author (your name) * @version (a version number or a date) */ public class Playground extends World { Snake[] snakeBody; String segment; Food food; Counter score; private int direction; private int speed; private int speedX; private int speedY; private boolean eaten; /** * 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); direction = 0; eaten = false; speed = 20; speedX = speed; speedY = 0; snakeBody = new Snake[3]; // initialization of the Snake for(int i=0; i<snakeBody.length; i++) { snakeBody[i] = new Snake(); addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world } score = new Counter("Score : "); // initialization of the score addObject(score, 19*15, 8); // adding score actor to the world food = new Food(); // initialization of the food addObject(food, 100, 100); // adding food actor to the world } public void checkKey() { if(Greenfoot.isKeyDown("right")) // checks if right key is pressed { if(direction==1 || direction==3) { direction = 0; speedX = speed; speedY = 0; } } else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed { if(direction==0 || direction==2) {direction = 1; speedX = 0; speedY = speed; } } else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed { if(direction==1 || direction==3) {direction = 2; speedX = -speed; speedY = 0; } } else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed { if(direction==0 || direction==2) { direction = 3; speedX = 0; speedY = -speed; } } } /** public void moveIt() { // save the location of the 'head' segment (segment in front) // these are the 'goto' values for the next segment int prevX = snakeBody[0].getX(); int prevY = snakeBody[0].getY(); // get the direction of movement for the 'head' along horizontal and vertical (0, 1, or -1) int dx = (1 - direction) * ((direction + 1)%2); int dy = (2 - direction) * (direction%2); // get tentative new coordinates for the 'head' int newX = prevX + speed * dx; int newY = prevY + speed * dy; // if 'head' cannot move, exit method if (newX < 0 || newX > getWidth() - 1) return; if (newY < 0 || newY > getHeight() - 1) return; // move head snakeBody[0].setLocation(newX, newY); // move body segments of snake for (int i = 1; i < snakeBody.length; i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); // move segment snakeBody[i].setLocation(prevX, prevY); // set 'goto' values for next segment prevX = holdX; prevY = holdY; } } */ public void moveIt() { // save the location of the 'head' segment (segment in front) // these are the 'goto' values for the next segment int previousLocationX = snakeBody[0].getX(); int previousLocationY = snakeBody[0].getY(); snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY); // move body segments of snake if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) { for(int i=1; i<snakeBody.length; i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); snakeBody[i].setLocation(previousLocationX, previousLocationY); // set 'goto' values for next segment previousLocationX = holdX; previousLocationY = holdY; } if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food { growSnake(previousLocationX, previousLocationY); updateFoodLocation(); score.add(5); } if(eaten) { updateFoodLocation();// updates the location of the food } if( getObjectsAt(snakeBody[0].getX(),snakeBody[0].getY(), null).size() == 1) return; { Greenfoot.stop(); } } else { Greenfoot.stop(); } } /** * This method will update the length of the Snake. */ private void growSnake(int x, int y) { Snake s = new Snake(); Snake oldSnake[] = new Snake[snakeBody.length]; for(int i=0; i<snakeBody.length; i++) { oldSnake[i] = snakeBody[i]; } snakeBody = new Snake[snakeBody.length+1]; for(int i=0; i<snakeBody.length-1; i++) { snakeBody[i] = oldSnake[i]; } snakeBody[snakeBody.length-1] = s; addObject(snakeBody[snakeBody.length-1], x, y); // adds new snake body actor } public void updateFoodLocation() { int x = 0; int y = 0; boolean overlap = true; eaten = false; while(overlap) { x = Greenfoot.getRandomNumber(getWidth()/15); y = Greenfoot.getRandomNumber(getHeight()/15); for( int i = 0; i < snakeBody.length; i++) { // Condition to check food will not touch the snake if(x!=snakeBody[i].getX() || y!= snakeBody[i].getY()) { overlap = false; break; } } } food.setLocation(x*15, y*15); } public void act() { checkKey(); moveIt(); } }