⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.java

📁 一款修改过的手机贪食蛇游戏
💻 JAVA
字号:
/* * Snake.java * * Created on June 19, 2007, 1:53 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package hello;import javax.microedition.lcdui.*;/** * * @author jiabei */public class Snake {        public static final int CONTROL_KEYBOARD = 0;    public static final int CONTROL_AI = 1;    public static final int STATE_NORMAL = 0;    public static final int STATE_SNAKE_DEAD = 1;    public static final int STATE_EAT_FRUIT = 2;        public static final int[] directionOffsetX = {0, 1, 0, -1};    public static final int[] directionOffsetY = {-1, 0, 1, 0};    public static final int DIRECTION_UP = 0;    public static final int DIRECTION_LEFT = 3;    public static final int DIRECTION_DOWN = 2;    public static final int DIRECTION_RIGHT = 1;    public static final int DIRECTION_NOCHANGE = -1;        public static final int MAX_LENGTH = 100;        private boolean isAlive;    private int direction;    private int length;    private Point[] body;     private int control;    private int eatedFruitNum;        private boolean growing = false;        public static int getOppositeDirection(int direction) {        if (direction < 2) {            return direction + 2;        }        return direction - 2;    }        /** Creates a new instance of Snake */    public Snake(int control, int direction, int length, int x, int y) {        isAlive = true;        this.direction = direction;        this.length = length;        this.control = control;        eatedFruitNum = 0;        body = new Point[MAX_LENGTH];        for (int i = 0; i < length; i ++) {            body[i] = new Point(x, y);            x += directionOffsetX[getOppositeDirection(direction)];            y += directionOffsetY[getOppositeDirection(direction)];        }    }        public void setDirection(int direction) {        if (direction != DIRECTION_NOCHANGE && (Math.abs(this.direction - direction) != 2)) {            this.direction = direction;            //System.out.println("Snake" + this.direction);        }    }        public int getControl() {        return control;    }        public boolean isAlive() {        return isAlive;    }        public static int keepInRange(int v, int min, int max) {        if (v < min) {            return v + (max - min);        } else if (v > max) {            return v - (max - min);        }        return v;    }        public void move(int height, int width) {        if (growing) {            if (body[length] == null) {                    body[length] = new Point(0, 0);            }            length ++;             growing = false;        }        for (int i = length - 1; i > 0; i --) {            body[i].setX(body[i - 1].getX());            body[i].setY(body[i - 1].getY());        }        body[0].setX(keepInRange(body[0].getX() + directionOffsetX[direction], 0, width - 1));        body[0].setY(keepInRange(body[0].getY() + directionOffsetY[direction], 0, height - 1));                //System.out.println("Snake" + direction);    }        public int isInside(Point point) {                // Should be in the opposite way, please check update().        for (int i = length - 1; i >= 0 && isAlive; i --)            if (body[i].isSame(point))                return i;        return -1;    }        public Point getHead() {        return body[0];    }        public int getDirection() {        return direction;    }        public void setDead(Map map) {        if (isAlive) {            map.decressSnake();        }        isAlive = false;    }        public void setGrowing() {        if (length < MAX_LENGTH) {            growing = true;        } else {            growing = false;        }    }        public int getEatedFruitNum() {        return eatedFruitNum;    }        public void incressEatedFruitNum() {        eatedFruitNum ++;    }        public static int update(Map map, int curSnake, boolean canCollide) {        int state = STATE_NORMAL;        map.getCurSnake(curSnake).move(map.getHeight(), map.getWidth());        for (int i = 0; i < map.getFruitNum(); i ++) {            if (map.getFruit(i).getPoint().isSame(map.getCurSnake(curSnake).getHead())) {                state = STATE_EAT_FRUIT;                map.getCurSnake(curSnake).incressEatedFruitNum();                map.getCurSnake(curSnake).setGrowing();                map.deleteFruit(i);                System.out.println(map.getFruitNum());            }        }        for (int i = 0; i < map.getSnakeNum(); i ++) {            int k;            if (!canCollide && map.getCurSnake(i).isAlive() &&                     ((k =  map.getCurSnake(i).isInside(map.getCurSnake(curSnake).getHead())) >= 0) ) {                if (i == curSnake && k != 0 || i != curSnake) {                    //System.out.println("" + i + "," + k);                    map.getCurSnake(curSnake).setDead(map);                                                        }                if (k == 0 && i != curSnake) {                    if (Math.abs(map.getCurSnake(i).getDirection() - map.getCurSnake(curSnake).getDirection()) == 2) {                        map.getCurSnake(i).setDead(map);                    }                }            }        }        return state;    }        public void drawSnake(Graphics g, int xPixels, int yPixels, int stringX, int stringY) {        g.setColor(255, 0, 0);        if (!isAlive && length > 0) {            length --;            g.setColor(100, 0, 0);        }        for (int i = 0; i < length; i ++) {            g.drawRect(xPixels * body[i].getX(), yPixels * body[i].getY(), xPixels, yPixels);        }        g.drawString("" + eatedFruitNum, stringX, stringY, g.TOP | g.LEFT);    }            public int getLength() {        return length;    }        public Point getBody(int i) {        if (i >= 0 && i < length)            return body[i];        return null;    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -