package coreGame; import java.util.Vector; import javax.microedition.lcdui.Graphics; public class Snake { Vector snake; int direction; static final int LEFT = 0, UP = 1, DOWN =2, RIGHT = 3; public Snake() { snake = new Vector(); } public void paintSnake(Graphics g){ for(int i = 0; i < snake.size(); i++){ PartSnake part =(PartSnake)( snake.elementAt(i) ); g.setColor(33, 152, 33); g.fillRect(part.x, part.y, 10, 10); g.setColor(0, 0, 0); g.drawRect(part.x, part.y, 10, 10); } } public void moving(){ if(direction == UP)moveUp(); if(direction == DOWN)moveDown(); if(direction == LEFT)moveLeft(); if(direction == RIGHT)moveRight(); } public void create(int x, int y){ PartSnake head = new PartSnake(x, y); PartSnake part1 = new PartSnake(x, y + 10); PartSnake part2 = new PartSnake(x, y + 20); snake.addElement(head); snake.addElement(part1); snake.addElement(part2); } public void add(){ PartSnake localPart = (PartSnake)(snake.elementAt(snake.size() - 1)); int x = localPart.x; int y = localPart.y; if(direction == LEFT)snake.addElement( (PartSnake)(new PartSnake (x - 10, y) ) ); if(direction == UP)snake.addElement( (PartSnake)(new PartSnake(x, y + 10) ) ); if(direction == RIGHT)snake.addElement((PartSnake)(new PartSnake(x + 10, y) ) ); if(direction == DOWN)snake.addElement((PartSnake)(new PartSnake(x, y - 10) ) ); } public void moveLeft(){ PartSnake localPart = (PartSnake)(snake.elementAt(snake.size() - 1)); int x = localPart.x; int y = localPart.y; if(snake.size() == 0)create(10, 10); if(direction != RIGHT){ PartSnake head = new PartSnake(x - 10, y); snake.insertElementAt(head, 0); snake.removeElementAt(snake.size() - 1); } } public void moveUp(){ PartSnake localPart = (PartSnake)(snake.elementAt(snake.size() - 1)); int x = localPart.x; int y = localPart.y; if(snake.size() == 0)create(10, 10); if(direction != DOWN){ PartSnake head = new PartSnake(x, y - 10); snake.insertElementAt(head, 0); snake.removeElementAt(snake.size() - 1); } } public void moveRight(){ PartSnake localPart = (PartSnake)(snake.elementAt(snake.size() - 1)); int x = localPart.x; int y = localPart.y; if(snake.size() == 0)create(10, 10); if(direction != LEFT){ PartSnake head = new PartSnake(x + 10, y); snake.insertElementAt(head, 0); snake.removeElementAt(snake.size() - 1); } } public void moveDown(){ PartSnake localPart = (PartSnake)(snake.elementAt(snake.size() - 1)); int x = localPart.x; int y = localPart.y; if(snake.size() == 0)create(10, 10); if(direction != UP){ PartSnake head = new PartSnake( x, y + 10); snake.insertElementAt(head, 0); snake.removeElementAt(snake.size() - 1); } } }