| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 710 人关注过本帖
标题:刚用 j2ME 写的扫雷游戏, 哥调c库太麻烦了...
只看楼主 加入收藏
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
结帖率:94.72%
收藏
已结贴  问题点数:20 回复次数:6 
刚用 j2ME 写的扫雷游戏, 哥调c库太麻烦了...
图片附件: 游客没有浏览图片的权限,请 登录注册

import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author blueguy
 */
public class GameLogic {

    MainCanvas mc;
    public final int WIDTH = 240;
    public final int HEIGHT = 360;
    //总区域的列数和行数
    public int cols = 13;
    public int rows = 16;
    public int rectWH = 16;
    public int nowCol;
    public int nowRow;
    public Image[] img;
    Random rd = new Random();
    public int num = 30;//雷的总数
    public short[][] mapData = new short[cols][rows];//地图数组  0: 原始  1: 地雷  2: 空地 3-10: 1-8数字  11:标记
    public boolean[][] isLei = new boolean[cols][rows];//地雷位置
    public boolean[][] isTest = new boolean[cols][rows];//已探测区域
    public boolean[][] isWrong=new boolean[cols][rows];
    public int markNum = 0;//标记数量
    public int leiCount;//标记正确的地雷数目
    public int srcX,  srcY;//雷区在屏幕的起始坐标

    public GameLogic(MainCanvas gc) {
        mc = gc;
        init();
    }

    public void init() {
        srcX = (WIDTH - cols * rectWH) >> 1;
        srcY = rectWH * 3 - 5;
        initImg();
        initLei(num);
    }
    //初始化图片数组

    public void initImg() {
        try {
            int tx = 0;
            int ty = 0;
            Image img1 = Image.createImage("/TL.png");
            img = new Image[13];
            for (int i = 0; i < img.length-1; i++) {
                if (i >= 6) {
                    ty = rectWH;
                    tx = (i - 6) * rectWH;
                } else {
                    tx = i * rectWH;
                }
                Image image = Image.createImage(16, 16);
                Graphics g = image.getGraphics();
                g.setColor(0);
                g.fillRect(0, 0, 240, 320);
                g.drawImage(img1, -tx, -ty, 0);
                img[i] = image;
            }
            img[12]=Image.createImage("/2.png");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void drawMap(Graphics g) {
        int mapNum = 0;
//       System.out.println(mapData.length);
        for (int j = 0; j < mapData[0].length; j++) {
            for (int i = 0; i < mapData.length; i++) {

                mapNum = mapData[i][j];
              
//                System.out.println("**********************:"+mapNum);
                g.drawImage(img[mapNum], srcX + i * rectWH, srcY + j * rectWH, 0);
                  if(isWrong[i][j]&&mc.isEnd){
                    drawX(g,0xff0000,srcX + i * rectWH,srcY + j * rectWH,rectWH);
                }
            }
        }
        g.setColor(0);

//        System.out.println(nowCol + "," + nowRow);
        g.drawRect(srcX + nowCol * rectWH, srcY + nowRow * rectWH, rectWH, rectWH);
        
     
    }

    public void drawX(Graphics g, int color, int x, int y, int width) {
        g.setColor(color);
        g.drawLine(x, y, x + width, y + width);
        g.drawLine(x, y + width, x + width, y);
    }

    public void drawF(Graphics g,int color,int x,int y,int width,int height ){
        g.setColor(color);
         g.drawRect(x, y, width, height);
    }
//初始化地雷
    public void initLei(int num) {

        while (num > 0) {
            int max = cols * rows;
            int rand = Math.abs(rd.nextInt() % max);
            int col = rand % cols;
            int row = rand / cols;
            while (true) {
                if (isLei[col][row] == false) {
                    isLei[col][row] = true;
//                    mapData[col][row]=1;
                    num--;
                    break;
                }
                rand++;
                if (rand >= max) {
                    rand = 0;
                }
                col = rand % cols;
                row = rand / cols;

            }

        }
        for (int j = 0; j < isLei[0].length; j++) {
            for (int i = 0; i < isLei.length; i++) {

                System.out.print(isLei[i][j] + ",");
            }
            System.out.println();
        }
//          for(int j=0;j<isLei[0].length;j++){
//         for(int i=0;i<isLei.length;i++){
//
//        System.out.print(mapData[i][j]+",");
//        }
//        System.out.println();
//        }
    }

    //检测指定位置是否有雷
    public boolean isLei(int col, int row) {
        if (col < 0 || col >= cols) {
            return false;
        }
        if (row < 0 || row >= rows) {
            return false;
        }
        return isLei[col][row];
    }

//    public boolean isTest(int col, int row) {
//        if (col < 0 || col >= cols) {
//            return false;
//        }
//        if (row < 0 || row >= rows) {
//            return false;
//        }
//        return isTest[col][row];
//    }
    //打开所有雷
    public void findAll() {
        for (int col = 0; col < isLei.length; col++) {
            for (int row = 0; row < isLei[0].length; row++) {
                if (isLei[col][row] && !isTest[col][row]&&mapData[col][row]!=11) {
                    mapData[col][row] = 1;//显示地雷
                }
                if(mapData[col][row]==11&&isWrong[col][row]){
                  mapData[col][row] = 1;
                }
            }
        }
    }

    //标记雷
    public void markLei() {
        if (!isTest[nowCol][nowRow]) {
            if (mapData[nowCol][nowRow] != 11) {
                markNum++;
                mapData[nowCol][nowRow] = 11;
                isWrong[nowCol][nowRow]=true;
                if (isLei[nowCol][nowRow]) {
                  isWrong[nowCol][nowRow]=false;
                    leiCount++;
                }
            } else {
                mapData[nowCol][nowRow] = 0;
                markNum--;
                isWrong[nowCol][nowRow]=false;
                if (isLei[nowCol][nowRow]) {
                    leiCount--;
                }
            }

        }
    }
//判断是否是雷

    public boolean checkLei(int col, int row) {
        if (isLei[col][row]) {
            return true;
        }
        if (mapData[col][row] != 0) {
            return false;
        }
        int tpCount = 0;
        for (int c = -1; c <= 1; c++) {

            for (int r = -1; r <= 1; r++) {

                if (isLei(col + c, row + r)) {
                    tpCount++;
                }
            }
        }
        mapData[col][row] = (short) (2 + tpCount);
        isTest[col][row] = true;
//        System.out.println(" mapData[col][row]:"+ mapData[col][row]+"  col:"+ col+ "  row:"+row);
//        System.out.println("tpCount:"+tpCount);
        if (tpCount == 0) {
            for (int c = -1; c <= 1; c++) {
                if (col + c < 0 || col + c >= isLei.length) {
                    continue; //边缘情况
                }
                for (int r = -1; r <= 1; r++) {
                    if (row + r < 0 || row + r >= isLei[0].length) {
                        continue;//边缘情况
                    }
                    checkLei(col + c, row + r);
                }
            }
        }

        return false;
    }

    public void initGame() {
        mapData = new short[cols][rows];
        isLei = new boolean[cols][rows];
        isTest = new boolean[cols][rows];
        isWrong=new boolean[cols][rows];
        initLei(num);
        mc.isEnd = false;
        mc.isWin = false;
        nowCol = 0;
        nowRow = 0;
        markNum = 0;//标记数量
        leiCount = 0;
        mc.tempFlag=false;
        mc.timeCount=0;
    }
}
收到的鲜花
  • 观弈寒儒2011-02-26 09:39 送鲜花  2朵   附言:比以往好,可惜放错地方了,这里是C语言论坛 ...
搜索更多相关主题的帖子: change 游戏 扫雷 open 
2011-02-25 23:14
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:4 
不错哦 支持一下

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-02-25 23:35
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:4 
学习学习
2011-02-26 00:29
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:4 
我的偶像!

   唯实惟新 至诚致志
2011-02-26 07:40
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:4 
JAVA咖啡,可惜没有杯子就喝不成了…

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2011-02-26 07:51
观弈寒儒
Rank: 7Rank: 7Rank: 7
来 自:自 来
等 级:黑侠
帖 子:359
专家分:545
注 册:2011-1-9
收藏
得分:4 
回复 5楼 flyue
认同。

事件记录,值得关注! http://bbs.bccn.net/z_court.php?fid=5
2011-02-26 09:41
观弈寒儒
Rank: 7Rank: 7Rank: 7
来 自:自 来
等 级:黑侠
帖 子:359
专家分:545
注 册:2011-1-9
收藏
得分:0 
回复 5楼 flyue
郁闷,经常多发一次。

事件记录,值得关注! http://bbs.bccn.net/z_court.php?fid=5
2011-02-26 09:41
快速回复:刚用 j2ME 写的扫雷游戏, 哥调c库太麻烦了...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019789 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved