Exported from Notepad++
// Fun Snake 4 // Snake Constants var SNAKE_DIM = 10; var SNAKE_COLOR = Color.green; // Constants to represent the directions var EAST = 0; var SOUTH = 1; var WEST = 2; var NORTH = 3; //Food Constants var FOOD_DELAY = 6000; var FOOD_RADIUS = SNAKE_DIM/2; var FOOD_COLOR = Color.red; // Points Text Constants var POINTS_TEXT_X = 5; var POINTS_TEXT_Y_BUFFER = 5; var POINTS_FONT = "17pt Arial"; var snake; var pointsText; var points = 0; var currentDirection = EAST; function start() { // TODO: create the snake and add it to the canvas // TODO: create the pointsText object and add it to the canvas setTimer(draw,50); setTimer(addFood,FOOD_DELAY); keyDownMethod(keyDown); } function draw() { var dx = 0; var dy = 0; addTail(); if (currentDirection == EAST) { dx = SNAKE_DIM; } else if (currentDirection == NORTH) { dy = SNAKE_DIM; } //TODO: add the other three directions //TIP: Check the "center" of the position the snake is about to move to. var elem = getElementAt(snake.getX()+SNAKE_DIM/2+dx,snake.getY()+SNAKE_DIM/2+dy); //TODO: If elem contains a snake body, get GAME OVER // For an Example, check out DOCS->Animation->Misc //TODO: If elem containts a piece of food, remove the piece of food. snake.move(dx,dy); //TODO: Increment points variable updatePointsText(); } function keyDown(e) { if (e.keyCode == Keyboard.LEFT) { currentDirection = WEST; } // TODO: add the other three directions } function addTail() { //TODO: Add another rectangle at the current // position of the snake to act as the tail // the x and y coordinates of the snake can // be found using snake.getX() and snake.getY(). //TIP: Refer back to the TRAIL exercise for similar code. } function addFood() { //TIP: Choose food locations so they are Guaranteed to line // up perfectly with the snake's body. var foodColumn = Randomizer.nextInt(0,getWidth()/SNAKE_DIM); var foodX = foodColumn*SNAKE_DIM+FOOD_RADIUS //TODO: Create foodRow and foodY //TODO: Create a circular piece of food centered at (foodX,foodY) // using the FOOD_RADIUS and FOOD_COLOR constants } function updatePointsText() { //TODO: Update the points Text object to the current score; }