package newfinish;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class MainCanvas extends Canvas implements Runnable{
private Image offImage; //离屏画布
private Graphics offg; //离屏画布的画笔
private Image imgCircle; //选框图
private Image imgMine; //地雷图
private int posCircleX; //选框位置X
private int posCircleY; //选框位置Y
private int posMineX; //地雷位置X
private int posMineY; //地雷位置Y
private static int NUMBER_MINE = 25; //总雷数
private static int mineRowNum; //雷区行数
private static int mineColumnNum; //雷区列数
private int curRow; //当前行数
private int curColumn; //当前列数
private int[][] mineArea; //地雷数组
private int numRandom; //随机数
private Random random; //随机对象
// private boolean isMine; //是否地雷
private boolean isFail = false; //游戏是否失败
private boolean isMine = false; //是否有雷
private boolean isSurroundMine = false; //周围是否有雷
private int opened; //翻开个数
private int cleared; //清除个数
private boolean isWin = false; //是否赢得游戏
public int gameState; //游戏状态
public static final int PLAYING = 0; //游戏进行状态
private static final int LIMTIME = 200; //游戏总时间
private int leftTime = LIMTIME; //倒计时剩下的时间
private boolean timeOver = false; //时间是否用完
public static final int WIN = 1; //赢得游戏状态
private long beginTime; //游戏开始时间
private static final int intervalTime = 1000;//每隔1秒重绘一次
private boolean isPause = false; //是否暂停
private Font font1; //字体
private Font font2;
private Font font3;
private Font font4;
public MainCanvas(){
new Thread(this).start(); //启动线程
beginTime = System.currentTimeMillis(); //记录游戏开始的时间
offImage = Image.createImage(Pub.WIDTH, Pub.HEIGHT);
font1 = Font.getDefaultFont();
font2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
font3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
font4 = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
loadImage(); //加载图片
offg = offImage.getGraphics();
offg.setFont(font3);
mineRowNum = Pub.HEIGHT / imgCircle.getHeight() - 2;
mineColumnNum = Pub.WIDTH / imgCircle.getWidth();
initMine(); //初始化雷区,不能在paint()方法中初始化,否则每重画一次,雷的位置就会变一次
// System.out.println("mineRowNum = " + mineRowNum);
// System.out.println("mineColumnNum = " + mineColumnNum);
}
/*
* 主画布的绘制
*/
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, Pub.WIDTH, Pub.HEIGHT);
drawBack();
offg.setClip(0, Pub.HEIGHT - 40, Pub.WIDTH, 40);
// drawTime(); // 画时间
offg.setClip(0, 0, Pub.WIDTH, Pub.HEIGHT);
drawCircle();
if(isFail){
drawFail();
}
drawWin();
// drawTimeOver();
g.drawImage(offImage,0,0,0); //在主画布上绘制离屏画布
}
/*
* 绘制格子背景
*/
private void drawBack(){
/*
* 注意:在这里离屏画布也要重画
*/
offg.setColor(0xffffff);
offg.fillRect(0, 0, Pub.WIDTH, Pub.HEIGHT);
offg.setColor(0);
for(int row = 0;row < mineRowNum;row ++){
for(int column = 0;column < mineColumnNum;column ++){
offg.drawRect(imgCircle.getWidth() * column,imgCircle.getWidth() * row,imgCircle.getWidth(),imgCircle.getHeight());
if(mineArea[row][column] > 0 && mineArea[row][column] < 10){ //如果周围有雷,则显示雷数
offg.setFont(font3);
offg.drawString(""+ mineArea[row][column],column * imgCircle.getWidth() + 8,row * imgCircle.getHeight() + 2,0);
// System.out.println("绘制周围雷数在" + column * imgCircle.getWidth() + "," + row * imgCircle.getHeight());
}else if(mineArea[row][column] == 10){ //如果周围没有雷则涂绿
// System.out.println(column + " " + row + "处变绿");
offg.setColor(0x00ff00);
offg.fillRect(column * imgCircle.getWidth(), row * imgCircle.getHeight(), imgCircle.getWidth(), imgCircle.getHeight());
offg.setColor(0);
}else if(mineArea[row][column] == 19){ //如果清除掉,变红
// System.out.println(column + " " + row + "处变红");
offg.setColor(0x00ff);
offg.fillRect(column * imgCircle.getWidth(), row * imgCircle.getHeight(), imgCircle.getWidth(), imgCircle.getHeight());
offg.setColor(0);
}
}
}
drawCommand();
}
/*
* 绘制时间
*/
private long pauseTime; //暂停时刻
private boolean first = true; //是否记录时间(第一次才记录),非常关键!!!!!
private long space; //每次停止了多长时间
private long allSpace; //一共停止了多长时间
private void drawTime(){
long usedTime = System.currentTimeMillis() - beginTime;
int minute = 0;
int second = 0;
if(isPause && first){ //如果是某次暂停的第一次绘制,记录时间
pauseTime = System.currentTimeMillis();
first = false;
}
if (!(timeOver || isFail || isWin || isPause)) {
if(pauseTime > 0 && !first){ //如果是某次暂停再开始后第一次绘制,记录时间
space = System.currentTimeMillis() - pauseTime;
allSpace += space; //多次暂停的时间累计
}
leftTime = (int) (LIMTIME - (usedTime - allSpace) / 1000);
System.out.println("after usedTime = " + usedTime + " leftTime = " + leftTime + " space = " + space + " allSpace = " + allSpace);
first = true;
}
if (leftTime < 0) {
timeOver = true;
} else {
minute = leftTime / 60;
second = leftTime % 60;
System.out.println("MINUTE = " + minute + " SECOND = " + second);
}
offg.setColor(0);
offg.setFont(font3);
offg.drawString(minute + ":", (Pub.WIDTH >> 1) - 10, Pub.HEIGHT - font2.getHeight() - 13, 0);
offg.drawString(second + "", (Pub.WIDTH >> 1) + 2, Pub.HEIGHT - font2.getHeight() - 13, 0);
// System.out.println("绘制时间");
}
/*
*时间用完
*/
private void drawTimeOver(){
if(timeOver){
offg.setFont(font3);
offg.setColor(0xffffff);
offg.fillRect(0, 0, Pub.WIDTH, Pub.HEIGHT);
offg.setColor(0);
offg.drawString("你太慢了", (Pub.WIDTH >> 1) - 35, (Pub.HEIGHT - font3.getHeight() >> 1) + 20, 0);
}
drawCommand();
}
/*
* 绘制选框
*/
private void drawCircle(){
offg.drawImage(imgCircle,posCircleX,posCircleY,0);
}
/*
* 判断是否有雷
*/
private boolean judgeMine(int row,int column){
if(mineArea[row][column] == 20){
isFail = true;
}else if(mineArea[row][column] != 19){
int numSurround = judgeSurround(row,column); //如果不是雷,判断周围雷数
if(numSurround > 0){
// System.out.println("周围有雷: numSurround = " + numSurround);
mineArea[row][column] = numSurround;
isSurroundMine = true;
}else { //如果周围没有雷
mineArea[row][column] = 10; //翻开
// System.out.println("应该翻开 " + column + " " + row);
//左上
if(row > 0 && column > 0){
if(mineArea[row - 1][column - 1] != 10){
judgeMine(row - 1,column - 1); //如果周围没有雷,递归调用自己
// System.out.println("左上numSurround = " + numSurround);
// System.out.println("row = " + row + " column = " + column);
}
}
//上
if(row > 0){
if(mineArea[row - 1][column] != 10){
judgeMine(row - 1,column);
// System.out.println("上numSurround = " + numSurround);
// System.out.println("row = " + row + " column = " + column);
}
}
//右上
if(row > 0 && column < mineColumnNum - 1){
if(mineArea[row - 1][column + 1] != 10){
judgeMine(row - 1,column + 1);
// System.out.println("右上numSurround = " + numSurround);
// System.out.println("row = " + row + " column = " + column);
}
}
//左
if(column > 0){
if(mineAr
j2me手机游戏_扫雷 代码
3星 · 超过75%的资源 需积分: 9 55 浏览量
2009-02-06
09:37:00
上传
评论
收藏 5KB RAR 举报
xia198507
- 粉丝: 0
- 资源: 1
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈