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).


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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" ; } } |
1 2 3 4 5 6 7 8 9 10 11 | 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 } |
1 2 3 4 | 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); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 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++; } } } |