Yes, moving the snake starting from the rear and working forward is much simpler than moving the snake starting from the front and working back (which is what was done there).


public void keyboardControll() { if(Greenfoot.isKeyDown("left") && direction !="right") { speedX = -speed; speedY = 0; direction = "left"; } if(Greenfoot.isKeyDown("right") && direction !="left") { speedX = speed; speedY = 0; direction = "right"; } if(Greenfoot.isKeyDown("up") && direction !="down") { speedX = 0; speedY = -speed; direction = "up"; } if(Greenfoot.isKeyDown("down") && direction !="up") { speedX = 0; speedY = speed; direction = "down"; } }
private void keyboardControll() { int dx = 0; // to be 0, 1, or -1 for the x direction int dy = 0; // to be 0, 1, or -1 for the y direction if (Greenfoot.isKeyDown("right")) dx++; if (Greenfoot.isKeyDown("down")) dy++; if (Greenfoot.isKeyDown("left")) dx--; // notice how opposite keys cancel each other if (Greenfoot.isKeyDown("up")) dy--; if (dx * dy != 0 || dx + dy == 0) return; // ensures one is zero and the other is not zero direction = (2 - dy) * (dy * dy) + (1 - dx) * (dx * dx); // set direction }
int x = (1-direction) * ((direction+1)%2); int y = (2-direction) * (direction%2); // validate that the parameters in the following statement are within world boundaries setLocation(getX() + speed * x, getY() + speed * y);
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Snake here. * * @author (your name) * @version (a version number or a date) */ public class Snake extends Actor { private Counter counter; private String segment; private int direction; private int speed; private int speedX; private int speedY; private int length; /** * Act - do whatever the Snake wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Snake(Counter pointsCounter) { pointsCounter = counter; } public Snake(String segment) { this.segment = segment; direction = 0; speedX = speed = 20; speedY = 0; length = 3; } public void act() { if( segment == "head") { Playground playground = (Playground)getWorld(); for( int i = length -1; i > 0; i--) { playground.snake[i].setLocation(playground.snake[i-1].getX(),playground.snake[i-1].getY()); } keyboardControl(); move(); eat(); } } public void keyboardControl() { if(Greenfoot.isKeyDown("d")) // checks if right key is pressed { if(direction==1 || direction==3) { direction = 0; } } else if(Greenfoot.isKeyDown("s")) // checks if down key is pressed { if(direction==0 || direction==2) { direction = 1; } } else if(Greenfoot.isKeyDown("a")) // checks if left key is pressed { if(direction==1 || direction==3) { direction = 2; } } else if(Greenfoot.isKeyDown("w")) // checks if up key is pressed { if(direction==0 || direction==2) { direction = 3; } } } public void move() { // check if hit the edge of screen Playground playground = (Playground)getWorld(); if( getX()+speedX < 0 || getX() + speedX >= 400) { die(); } if (getY() + speedY < 0 || getY() + speedY>= 400) { die(); } setLocation(getX() + speedX, getY() + speedY); // check if hit the body Actor body = getOneObjectAtOffset(speedX, speedY, Snake.class); if(body !=null) { die(); } } public void die() { Greenfoot.stop(); } public void eat() { Actor food = getOneObjectAtOffset(0 , 0, Food.class); if( food !=null) { Playground playground = (Playground)getWorld(); playground.removeObject(food); playground.snake[length] = new Snake("body"); playground.addObject(playground.snake[length], playground.snake[length - 1].getX(), playground.snake[length - 1].getY()); length++; } } }