fruit.java

来自「一款修改过的手机贪食蛇游戏」· Java 代码 · 共 67 行

JAVA
67
字号
/* * Fruit.java * * Created on June 19, 2007, 2:34 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package hello;import java.util.Random;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;/** * * @author jiabei */public class Fruit {        private Point point;    public Fruit(int x, int y) {        point = new Point(x, y);    }         /** Creates a new instance of Fruit */    private static Random random = new Random();        public static int getRandomInt(int min, int max) {        return Math.abs(random.nextInt()) % (max + 1 - min) + min;    }        public static Fruit getRandomFruit(Map map) {        boolean run = true;        Point tmpPoint = new Point(0, 0);        while (run) {            tmpPoint.setX(getRandomInt(0, map.getWidth()));            tmpPoint.setY(getRandomInt(0, map.getHeight()));            run = false;            for (int i = 0; i < map.getSnakeNum(); i ++) {                if (map.getCurSnake(i).isInside(tmpPoint) >= 0) {                    run = true; break;                }            }            for (int i = 0; i < map.getFruitNum(); i ++) {                if (map.getFruit(i).getPoint().isSame(tmpPoint)) {                    run =true; break;                }            }        }        return new Fruit(tmpPoint.getX(), tmpPoint.getY());    }        public void drawFruit(Graphics g, int xPixels, int yPixels) {                g.setColor(0, 100, 0);        g.drawRect(xPixels * point.getX(), yPixels * point.getY(), xPixels, yPixels);           }        public Point getPoint() {        return point;    }    }

⌨️ 快捷键说明

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