使用Game API开发J2ME 2D游戏[5]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    private MicroTankSprite createTank() throws IOException
    {
        Image image = Image.createImage("/tank.png");
        return new MicroTankSprite(image, 32, 32);
    }

    private TiledLayer createBoard() throws IOException
    {
        Image image = Image.createImage("/board.png");
        TiledLayer tiledLayer = new TiledLayer(10, 10, image, 16, 16);

        int[] map = { 1, 1, 1, 1, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 0, 1,
                1, 1, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 11, 0, 0, 0,
                0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0 };

        for (int i = 0; i < map.length; i++)
        {
            int column = i % 10;
            int row = (i - column) / 10;
            tiledLayer.setCell(column, row, map[i]);
        }

        return tiledLayer;
    }

    public void start()
    {
        mTrucking = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run()
    {
        Graphics g = getGraphics();

        int timeStep = 80;

        while (mTrucking)
        {
            long start = System.currentTimeMillis();

            tick();
            input();
            render(g);

            long end = System.currentTimeMillis();
            int duration = (int) (end - start);

            if (duration < timeStep)
            {
                try
                {
                    Thread.sleep(timeStep - duration);
                } catch (InterruptedException ie)
                {
                    stop();
                }
            }
        }
    }

    private void tick()
    {
        if (mTank.collidesWith(mBoard, true))
            mTank.undo();
    }

本文关键:使用Game API开发J2ME 2D游戏
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top