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

📄 forground.java

📁 手机上一个坦克游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package demo;

import javax.microedition.lcdui.*;

/**
 * <p>Title: </p>
 * <p>Description: j</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Forground
/*implements Runnable/**/{

    public BitArray mapArray; //这个背景象素消息
    //public BattleField battleCanvas; //主类
    public Image mapImage = null;

    ///////////////////////////////////////////////////////////////////////////
    public static short topLine[]; //记录从资源文件读来的信息通过算法得到的值(记录地表信息)
    ///////////////////////////////////////////////////////////////////////////

    private static Forground instance;
    private int m_nWidth;
    private int m_nHeight;
    private int m_nColor;
    public int m_nWindPower = 0;

    public Forground(short[] mapInfo, int mapType,
                     int map_width, int map_height,int color, int squares, boolean isNew) {
        /*新地图的格式是
         宽度
         高度
         地图数据类型
         方块数
         方块A的起始坐标(x,y)
         方块A的width,height
         方块B......
         */
        ////System.out.println("in Forground");
        m_nWidth = map_width;
        m_nHeight = map_height;
        //System.out.println("mm_nWdith = " + m_nWidth);
        //System.out.println("m_nHeight = " + m_nHeight);
        int x = m_nWidth, y = m_nHeight, w = 0, h = 0, n = 0;
        if(isNew){
            //System.out.println("mapInfo.length = " + mapInfo.length);
            while(n < mapInfo.length){
                if(mapInfo[n] < x){
                    x = mapInfo[n];
                }
                if(mapInfo[n+1] < y){
                    y = mapInfo[n + 1];
                }
                if(mapInfo[n] + mapInfo[n+2] > w){
                    w = mapInfo[n] + mapInfo[n + 2];
                }
                if(mapInfo[n+1] + mapInfo[n+3] > h){
                    h = mapInfo[n + 1] + mapInfo[n + 3];
                }
                n = n + 4;
                //System.out.println("mapInfo[" + n + "] = " +  mapInfo[n]);
            }
            w = w - x;
            h = h - y;
            //System.out.println("x = " + x + "," + "y = " + y + "," + "w = " + w +
                               //"," + "h = " + h);
            mapArray = new BitArray(w , h ,x,y);
        }
        else{
            mapArray = new BitArray(m_nWidth, m_nHeight,0,0);
        }
        m_nColor = color;

        Image bg = null;
        Image top = null;
        byte abyte[];

        mapType = mapType % 4;
        abyte = Main.b("ground" + mapType+ ".png");
        bg = Image.createImage(abyte, 0, abyte.length);
        if (mapType < 3) {
            abyte = Main.b("top" + Integer.toString(mapType) + ".png");
            top = Image.createImage(abyte, 0, abyte.length);
        }
        //System.out.println("before create mapArray! width = " + map_width + " height = " + map_height);
        mapImage = Image.createImage(w,h);
        topLine = new short[map_width];
        //for(int i = 0 ; i
        //System.out.println("after create mapArray!");
        Graphics bGraphic = mapImage.getGraphics();
        bGraphic.setColor(m_nColor); //天空颜色
        bGraphic.fillRect(0, 0, w, h);
        bGraphic.setStrokeStyle(0);
        //System.out.println("color = " + color + " isNew = " + isNew + " squares = " + squares + " mapInfo.length = " + mapInfo.length);
        createMap(mapInfo, bGraphic, bg, top, squares, isNew);

    }

    public void createMap(short[] mapInfo, Graphics bGraphic, Image bg,
                          Image top, int squares, boolean isNew) {
        int cx = bGraphic.getClipX();
        int cy = bGraphic.getClipY();
        int cw = bGraphic.getClipWidth();
        int ch = bGraphic.getClipHeight();
        if (isNew) { //新的方块地图
            int x = 0;
            int y = 0;
            int w = 0;
            int h = 0;
            int x_times = 0;
            int y_times = 0;
            int t_times = 0;
            //System.out.println("isNew");
            //
            for (int i = 0; i < squares; i++) {
                x = mapInfo[i * 4];
                y = mapInfo[i * 4 + 1];
                w = mapInfo[i * 4 + 2];
                h = mapInfo[i * 4 + 3];
                /*System.out.println("x = " + x );
                System.out.println("y = " + y );
                System.out.println("w = " + w );
                System.out.println("h = " + h );
                System.out.println("x - mapArray.m_nX =  " + (x - mapArray.m_nX));
                System.out.println("y - mapArray.m_nXY = " + (y - mapArray.m_nY));/**/
                bGraphic.setClip(x - mapArray.m_nX , y - mapArray.m_nY, w, h);
                x_times = w / 32 + 1;
                y_times = h / 32 + 1;
                t_times = w / 16 + 1;
                //System.out.println("before paint bg");
                for (int k = 0; k < x_times; k++) {
                    for (int l = 0; l < y_times; l++) {
                        bGraphic.drawImage(bg, x + k * 32 - mapArray.m_nX, y + l * 32 - mapArray.m_nY , 20);
                    }
                }
                //System.out.println("before paint top");
                if (top != null) {
                    for (int k = 0; k < t_times; k++) {
                        bGraphic.drawImage(top, x + k * 16 - mapArray.m_nX   , y - mapArray.m_nY , 20);
                    }
                }
                //System.out.println("before set array");
                for (int k = 0; k < w ; k++) {
                    for (int l = 0; l < h ; l++) {
                         //System.out.println((x +k )+ "," + (y + l));
                         mapArray.set(x + k, y + l);
                    }
                }
                /*System.out.println("************************");
                System.out.println("231 139 point's value = " + mapArray.get(231,139));
                System.out.println("230 139 point's value = " + mapArray.get(230,139));
                System.out.println("229 139 point's value = " + mapArray.get(229,139));
                System.out.println("228 139 point's value = " + mapArray.get(228,139));
                System.out.println("227 139 point's value = " + mapArray.get(227,139));
                System.out.println("226 139 point's value = " + mapArray.get(226,139));
                System.out.println("225 139 point's value = " + mapArray.get(225,139));
                System.out.println("224 139 point's value = " + mapArray.get(224,139));/**/
            }
        }
        else {
            int index = 0;
            int num = 0;
            int j = 0;
            int x = 0;
            int times = 0;
            byte byte1 = 5;
            int key = 2;
            while (index < mapInfo.length) {
                num = mapInfo[index];
                for (j = 0; j < num / 2; j++) {
                    bGraphic.setClip(x, mapInfo[index + 2 * j + 1], 1,
                                     mapInfo[index + 2 * j + 2] -
                                     mapInfo[index +
                                     2 * j + 1]); // x, y, width, height
                    for (int n = mapInfo[index + 2 * j + 1];
                         n <= mapInfo[index + 2 * j + 2]; n++) {
                        //mapArray[x][n - 1] = 1;
                        mapArray.set(x , n - 1);
                    }
                    times = (mapInfo[index + 2 * j + 2] - mapInfo[index +
                             2 * j +
                             1]) / 32 + 1;
                    for (byte n = 0; n < times; n++) {
                        bGraphic.drawImage(bg, x - x % bg.getWidth(),
                                           mapInfo[index + 2 * j + 1] +
                                           n * bg.getHeight(),
                                           20); //img, x, y, anchor
                    }
                    if (top != null) {
                        bGraphic.setClip(x, mapInfo[index + 2 * j + 1], key,
                                         top.getHeight()); // x, y, width, height
                        bGraphic.drawImage(top, x - x % 16,
                                           mapInfo[index + 2 * j + 1], 20); // 16 is the width of the pic
                    }
                }
                x++;
                index = index + num + 1;
            }
        }
        //System.out.println("before set clip");
        bGraphic.setClip(cx, cy, cw, ch);
        //System.out.println("topLine.length  = " + topLine.length);
        for (int i = 0; i < topLine.length; i++) {
            topLine[i] = (short) Math.min( m_nHeight + 1,
                getDownNotNull(i, 0));
            //System.out.println("topLine["+i+"]"+topLine[i]);
        }
        mapInfo = null;

    }


    public void paint(Graphics g1) {
        if (mapImage != null)
            g1.drawImage(mapImage, mapArray.m_nX, mapArray.m_nY, 20);
    }

    //@parm k1 : 导弹爆炸效果类
    //这个函数对爆炸以后对地形的破坏的处理
    public void exacuteCrash(int x, int y, int radius) { //爆炸中心坐标
        //System.out.println("crash in x = " + x + "; y = " + y + "; radius = " +
        //radius);
        try {
            Graphics bGraphic = mapImage.getGraphics();
            //bGraphic.setColor(255, 255, 255);
            //爆炸产生的效果是一个类似圆形区域
            for (int i1 = x - radius; i1 < x + radius; i1++) {
                for (int j1 = y - radius; j1 < y + radius; j1++) {
                    //距离的平方不要大于radius平方,和i1不能超过屏幕的宽度j和i1要大于0和j1小于高度g以及那个像素有东西
                    if ( (i1 - x) * (i1 - x) +
                        (j1 - y) * (j1 - y) < radius * radius &&
                        i1 < mapArray.getWidth() && j1 >= 0 &&
                        i1 >= 0 &&
                        j1 <
                        mapArray.getHeight()
                        /*&& mapArray[i1][j1] == 1/**/) {
                        //mapArray[i1][j1] = 0;
                        //System.out.println("set crash point : x = " + i1 + " y = " + j1);
                        mapArray.reSet(i1, j1 );
                        //System.out.println("get : mapArray.get(i1, j1 ) = " + mapArray.get(i1, j1 ));
                        bGraphic.setColor(m_nColor);
                        bGraphic.drawLine(i1 - mapArray.m_nX, j1 - mapArray.m_nY, i1 - mapArray.m_nX, j1 - mapArray.m_nY );
                    }
                }
                /*if(i1 == x + radius -1){
                    //System.out.println("finish mod map");
                                 }*/
            }
        }
        catch (ArrayIndexOutOfBoundsException arrayindexoutofboundsexception) {
            arrayindexoutofboundsexception.printStackTrace();
        }
        for (int i = x - radius; i < x + radius; i++) {
            if (i >= 0 && i < Main.displayable.MAP_WIDTH) {
                topLine[i] = (short) Math.min(m_nHeight + 1,
                                              getDownNotNull(i, 0));
            }
        }
    }

    public int getUpNull(int x, int initY) { // 求出从送入的参数为坐标的点向上的第一个空点的坐标!(包含送入的点)
        for (int i = initY - 1; i >= mapArray.m_nY; i--) {
            //System.out.println((initY -1 ) + "*******" + mapArray.m_nY);
            //System.out.println("mapArray[" + (x - 1) + "][" + i + "] = " +
            //mapArray[x - 1][i]);
            if(i < mapArray.getHeight()){
                if (x >= mapArray.m_nX && x < mapArray.getWidth()) {
                    if (mapArray.get(x, i) == 0) { //mapArray[x - 1][i] == 0) {
                        //System.out.println("upNull: x : " + x + " y = " + i + " " + mapArray.get(x, i));
                        return i;
                    }
                }
                /*else {
                    return 1000;
                }*/
            }
        }

⌨️ 快捷键说明

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