一、 利用J2ME實現Windows自帶的掃雷游戲。實現過程主要用的知識點:
1、MIDlet實現入口,控制屏幕和命令的控制。
2、數組,偵聽接口,線程,簡單的算法。
二、 具體的實現過程:
入口類的實現:
import Javax.microedition.lcdui.*; import Javax.microedition.midlet.*; public class gameStart extends MIDlet implements CommandListener { public static gameStart midlet; public myCanvas mc; private Command exitCommand = new Command("Exit", Command.EXIT, 0); public gameStart() { gameStart.midlet = this; } public void startApp() { gameStart.midlet.startGame(); } public void pauseApp() { } public void destroyApp(boolean f) { } public void startGame() { mc = null; mc = new myCanvas(); Display.getDisplay(this).setCurrent(mc); mc.addCommand(exitCommand); mc.setCommandListener(this); } public void exitGame() { destroyApp(false); notifyDestroyed(); } public void commandAction(Command cmd, Displayable arg1) { if (cmd == exitCommand) { destroyApp(false); notifyDestroyed(); } } }
其中實現游戲的開始,結束和偵聽用戶命令。
還可以實現MIDlet三個狀態的轉化(暫停、結束、開啟)。
實現類:
import Javax.microedition.lcdui.*; import Java.util.*; public class myCanvas extends Canvas{ public Image offScreen; public Graphics offg; public int map[][]=new int[8][8]; public Random r1; public Image imCricle,imMine;//pic public int px,py; public int mineNumber=10;//¡Á¨¹¨¤&IExcl;Á¨ºy public boolean gameOver=false;//¨®??¡¤¨º?&IExcl;¤??¨¢¨º? public boolean gameWin=false;//¨®??¡¤¨º?¡¤?¨º&IExcl;訤? public myCanvas() { offScreen=Image.createImage(149,180); offg=offScreen.getGraphics(); try{ imCricle=Image.createImage("/cricle.png"); imMine=Image.createImage("/mine.png"); }catch(Exception e){} r1=new Random(); initMap();//3?¨º??¡¥¨¤&IExcl;Á?? } public void initMap() { for(int n=0;n<mineNumber;n++) {int tmp=Math.abs(r1.nextInt()%64); int row=tmp/8;//DD int col=tmp%8;//¨¢D if(map[row][col]!=20) { map[row][col]=20;//??¦Ì¨²rowDD¦Ì¨²col¨¢D¨¦¨¨3¨¦¨¤&IExcl;Á } else {n--;} } } public void drawMap() {offg.setColor(0xFFFFFF); offg.fillRect(0,0,176,208); offg.setColor(0); for(int i=0;i<8;i++) { for(int j=0;j<8;j++) {offg.drawRect(j*20,i*20,20,20); //¨¨?1?¡¤-?a?¨°??¨º??a¦Ì??¨¹?¡ì¦Ì?¨¤&IExcl;Á¨ºy if(map[i][j]!=20&&map[i][j]!=0&&map[i][j]!=10) { offg.drawString(" "+map[i][j],j*20,i*20,0); } else if(map[i][j]==10) { offg.drawString(" 0",j*20,i*20,0); } } } } public void drawCricle() { offg.drawImage(imCricle,px,py,0); } public void drawOver()//????¨®??&IExcl;¤?¨¢¨º? { offg.setColor(0xFFFFFF); offg.fillRect(0,0,176,208); offg.setColor(0); offg.drawString("Game Over",30,30,0); for(int i=0;i<8;i++) { for(int j=0;j<8;j++) {if(map[i][j]==20) {offg.drawImage(imMine,j*20,i*20,0);} else {offg.drawRect(j*20,i*20,20,20);} } } } public void drawWin()//????¨®??¡¤¨º&IExcl;訤? { offg.setColor(0xFFFFFF); offg.fillRect(0,0,176,208); offg.setColor(0); offg.drawString("you Win",30,30,0); } public void paint(Graphics g) {drawMap();//????¨¤&IExcl;Á?? drawCricle();//???????¨° if(gameOver){drawOver();} if(gameWin){drawWin();} offg.drawString("new game exit",0,170,0); g.drawImage(offScreen,0,0,0); } public void checkWin() { int count=0;//????&IExcl;¤-?a¦Ì?¨ºy¨¢? for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(map[i][j]>0) { count++; } } } if(count+this.mineNumber==64) { this.gameWin=true; } } //map[r2][c2]?¨¹?¡ì¦Ì?¨¤&IExcl;Á¨ºy public int checkNum(int r2,int c2) {int num=0; //&IExcl;Á¨®¨¦? if(r2-1>=0&&c2-1>=0)//?D??¨º?¡¤?¨®D&IExcl;Á¨®¨¦? { if(map[r2-1][c2-1]==20){num++;} } //¨¦? if(r2-1>=0) { if(map[r2-1][c2]==20)num++; } //¨®¨°¨¦? if(r2-1>=0&&c2+1<=7) { if(map[r2-1][c2+1]==20)num++; } //&IExcl;Á¨® if(c2-1>=0) { if(map[r2][c2-1]==20)num++; } //¨®¨° if(c2+1<=7) { if(map[r2][c2+1]==20)num++; } //&IExcl;Á¨®?? if(r2+1<=7&&c2-1>=0) { if(map[r2+1][c2-1]==20)num++; } //?? if(r2+1<=7) { if(map[r2+1][c2]==20)num++; } //¨®¨°?? if(r2+1<=7&&c2+1<=7) { if(map[r2+1][c2+1]==20)num++; } return num;//?a¦Ì??¨¹?¡ì¦Ì?¨¤&IExcl;Á¨ºy } public void check(int row,int col) { if(map[row][col]==20) {gameOver=true;} else {//?D?¡§?a¦Ì??¨¹?¡ì¦Ì?¨¤&IExcl;Á¨ºy int k=this.checkNum(row,col); if(k>0)//?a¦Ì??¨¹?&IExcl;쨮D¨¤¨¤ { map[row][col]=k;//??¨º??a¦Ì??¨¹?¡ì¦Ì?¨¤&IExcl;Á¨ºy } else//?a¦Ì??¨¹?¡ì??¨®D¨¤&IExcl;Á { map[row][col]=10;//&IExcl;¤-?a?a¨°?¦Ì? //?¨²?D???a¦Ì??¨¹?&IExcl;ì¦Ì??¨¦?? //&IExcl;Á¨®¨¦? if(row-1>=0&&col-1>=0) { if(map[row-1][col-1]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row-1,col-1); } } //¨¦? if(row-1>=0) { if(map[row-1][col]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row-1,col); } } //¨®¨°¨¦? if(row-1>=0&&col+1<=7) { if(map[row-1][col+1]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row-1,col+1); } } //&IExcl;Á¨® if(col-1>=0) { if(map[row][col-1]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row,col-1); } } //¨®¨° if(col+1<=7) { if(map[row][col+1]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row,col+1); } } //&IExcl;Á¨®?? if(row+1<=7&&col-1>=0) { if(map[row+1][col-1]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row+1,col-1); } } //?? if(row+1<=7) { if(map[row+1][col]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row+1,col); } } //¨®¨°?? if(row+1<=7&&col+1<=7) { if(map[row+1][col+1]<10){//?D??¨º?¡¤?¡À?&IExcl;¤-?a check(row+1,col+1); } } } } } public void keyPressed(int n) {if(n==-6) {gameStart.midlet.startGame();} if(n==-7) {gameStart.midlet.exitGame();} int k=this.getGameAction(n); switch(k) { case Canvas.UP: if(py>=20) py-=20; break; case Canvas.DOWN: if(py<=20*6){py+=20;} break; case Canvas.LEFT: if(px>=20) px-=20; break; case Canvas.RIGHT: if(px<=20*6)px+=20; break; case Canvas.FIRE: int seleRow=py/20;//¦Ì¡À?&IExcl;ã???¨°¦Ì??¨´?¨²DD int seleCol=px/20;//¦Ì¡À?&IExcl;ã???¨°¦Ì??¨´?¨²¨¢D check(seleRow,seleCol);//¨®??¡¤¡¤-?a¦Ì?¨¤&IExcl;Á¦Ì??¨¬2a //?¨¬2a1y1? checkWin(); } this.repaint(); this.serviceRepaints(); } }
在這個類中主要實現了屏幕的繪畫,實現其具體的算法,針對每種情況作出相應的處理。
三、編譯 運行 通過 之後打包 形成jar文件
我已經把jar放到了服務器上,手機可以直接訪問