import java.awt.*; import javax.swing.*; import java.awt.event.*; // needed for event handling public class Snake { static final int SCREEN_SIZE_X=40; // In units of snake sections. static final int SCREEN_SIZE_Y=30; final int MAX_SNAKE_LENGTH = 1000; // While a snake is created with a very large number of snake sections, // determined by the constant variable MAX_SNAKE_LENGTH, the actual // apparent length of the snake in the game will be controlled by the // variable snakeLength. At each time step, the program will only draw // snakeLength snake sections for each snake. // // Each time the snake eats some "food" (a yellow box on the screen), // the snakeLength for that snake will // grow by the value of the food, which is printed in the box representing the // food. int snakeLength = 5; // Start snakes with length 5. SnakeSection [] snakeSecs = new SnakeSection[MAX_SNAKE_LENGTH]; // These variables represent the direction the snake is going. // Each time step, the snake moves in the direction represented by these // variables. The program does this by adding these values to the previous // head position of the snake. For example, the snake goes left initially, // since by adding -1 to the x value (dirX = -1) and adding 0 to the y value // (dirY=0), the head of the snake moves one square to the left. int dirX=-1; int dirY=0; Color color; // Holds the color of the snake. public Snake(SnakeSection startPos,int dx,int dy,Color color) { // Here, we are creating a large number of snake sections (1000 of them) so // that we don't have to worry about creating them later. for (int i=0; i0; i--) snakeSecs[i]=snakeSecs[i-1]; int newX=(snakeSecs[1].x + dirX + SCREEN_SIZE_X) % SCREEN_SIZE_X; int newY=(snakeSecs[1].y + dirY + SCREEN_SIZE_Y) % SCREEN_SIZE_Y; snakeSecs[0]=new SnakeSection(newX,newY); } // A snake is painted by drawing a square for each snake section. Each square is 20 by 20 pixels. public void paint(Graphics g) { g.setColor(color); for (int i=0; i