javaME拼图游戏

 该拼图游戏的基本界面如下:

这是按OK键的帮助效果:

MIDlet代码:

Code:
  1. package com.javaME.PPuzzle;  
  2.   
  3. import javax.microedition.lcdui.Display;  
  4. import javax.microedition.lcdui.Ticker;  
  5. import javax.microedition.midlet.MIDlet;  
  6. import javax.microedition.midlet.MIDletStateChangeException;  
  7.   
  8. public class PPuzzleMidlet extends MIDlet {  
  9.     private Display dis;  
  10.       
  11.     public PPuzzleMidlet() {  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.   
  15.     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {  
  16.         // TODO Auto-generated method stub  
  17.   
  18.     }  
  19.   
  20.     protected void pauseApp() {  
  21.         // TODO Auto-generated method stub  
  22.   
  23.     }  
  24.   
  25.     protected void startApp() throws MIDletStateChangeException {  
  26.         // TODO Auto-generated method stub  
  27.           
  28.         dis = Display.getDisplay(this);  
  29.         PPuzzleCanvas canvas = new PPuzzleCanvas(this);  
  30.         canvas.setTicker(new Ticker("海滨拼图,越拼越勇!"));  
  31.         dis.setCurrent(canvas);  
  32.     }  
  33.   
  34. }  

自定义Canvas代码:

Code:
  1. package com.javaME.PPuzzle;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Random;  
  5.   
  6. import javax.microedition.lcdui.Canvas;  
  7. import javax.microedition.lcdui.Command;  
  8. import javax.microedition.lcdui.CommandListener;  
  9. import javax.microedition.lcdui.Displayable;  
  10. import javax.microedition.lcdui.Font;  
  11. import javax.microedition.lcdui.Graphics;  
  12. import javax.microedition.lcdui.Image;  
  13. import javax.microedition.lcdui.game.GameCanvas;  
  14. import javax.microedition.lcdui.game.Sprite;  
  15. import javax.microedition.midlet.MIDlet;  
  16.   
  17. public class PPuzzleCanvas extends Canvas implements CommandListener, Runnable {  
  18.     private Image img1 = null;  
  19.     private Image img2 = null;  
  20.     private Image img3 = null;  
  21.     private Image img4 = null;  
  22.     private Image img = null;  
  23.     private Image[] myImage = new Image[4];  
  24.     private Command cmdExit = new Command("退出", Command.BACK, 1);  
  25.     private Command cmdNext = new Command("下一关", Command.SCREEN, 2);  
  26.     private Command cmdBefore = new Command("上一关", Command.SCREEN, 1);  
  27.     private Command cmdRefresh = new Command("重置", Command.SCREEN, 3);  
  28.     private int edge;  
  29.     private MIDlet parent;  
  30.     private int num = 0;  
  31.     private int second = 0;  
  32.     private int minute = 0;  
  33.     private int FLAG = 0;  
  34.     private int map[][] = { { 00010203 }, { 10111213 },  
  35.             { 20212223 }, { 30313233 } };  
  36.   
  37.     public PPuzzleCanvas(MIDlet parent) {  
  38.         this.parent = parent;  
  39.         try {  
  40.             img1 = Image.createImage("/background1.png");  
  41.             img2 = Image.createImage("/background2.png");  
  42.             img3 = Image.createImage("/background3.png");  
  43.             img4 = Image.createImage("/background4.png");  
  44.         } catch (IOException e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.         myImage[0] = img1;  
  50.         myImage[1] = img2;  
  51.         myImage[2] = img3;  
  52.         myImage[3] = img4;  
  53.   
  54.         img = myImage[num];  
  55.         edge = img.getWidth() / 4;  
  56.         this.addCommand(cmdExit);  
  57.         this.addCommand(cmdRefresh);  
  58.         this.setCommandListener(this);  
  59.         initMap();  
  60.     }  
  61.   
  62.     private void initMap() {  
  63.         Random random = new Random();  
  64.         int temp, x1, y1, x2, y2;  
  65.   
  66.         for (int i = 0; i < 100; i++) {  
  67.             x1 = random.nextInt(4);  
  68.             y1 = random.nextInt(4);  
  69.             x2 = random.nextInt(4);  
  70.             y2 = random.nextInt(4);  
  71.             temp = map[x1][y1];  
  72.             map[x1][y1] = map[x2][y2];  
  73.             map[x2][y2] = temp;  
  74.         }  
  75.   
  76.         if (num == 3) {  
  77.             this.removeCommand(cmdNext);  
  78.             //this.removeCommand(cmdRefresh);  
  79.             // this.addCommand(cmdExit);  
  80.         } else {  
  81.             this.addCommand(cmdNext);  
  82.         }  
  83.           
  84.         if(num == 0) {  
  85.             this.removeCommand(cmdBefore);  
  86.         } else {  
  87.             this.addCommand(cmdBefore);  
  88.         }  
  89.         repaint();  
  90.     }  
  91.   
  92.     protected void paint(Graphics g) {  
  93.         // TODO Auto-generated method stub  
  94.           
  95.         if (FLAG == 0) {  
  96.             g.setColor(255255255);  
  97.             g.fillRect(00, getWidth(), getHeight());  
  98.             // 根据地图编号,选出小块图片画出来  
  99.             for (int i = 0; i < 4; i++) {  
  100.                 for (int j = 0; j < 4; j++) {  
  101.                     if (map[i][j] != 33) {  
  102.                         int x_src = map[i][j] / 10;  
  103.                         int y_src = map[i][j] % 10;  
  104.   
  105.                         int x_dest = i * edge;  
  106.                         int y_dest = j * edge;  
  107.   
  108.                         g.drawRegion(img, x_src * edge, y_src * edge, edge,  
  109.                                 edge, Sprite.TRANS_NONE, x_dest, y_dest,  
  110.                                 Graphics.LEFT | Graphics.TOP);  
  111.                     }  
  112.                 }  
  113.             }  
  114.         } else if (FLAG == 1) {  
  115.             g.drawImage(img, 00, Graphics.LEFT | Graphics.TOP);  
  116.         }  
  117.   
  118.         if (isSuccess()) {  
  119.             g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,  
  120.                     Font.SIZE_LARGE));  
  121.             g.setColor(25500);  
  122.             g.drawString("恭喜顺利通关!您共耗时:" + minute + "分" + second + "秒"this  
  123.                     .getWidth() / 2this.getHeight() - 60, Graphics.TOP  
  124.                     | Graphics.HCENTER);  
  125.               
  126.         }  
  127.     }  
  128.   
  129.     public void commandAction(Command cmd, Displayable d) {  
  130.         // TODO Auto-generated method stub  
  131.         if (cmd == cmdNext && num < 3) {  
  132.             num++;  
  133.             img = myImage[num];  
  134.             this.initMap();  
  135.             //this.removeCommand(cmdNext);  
  136.             // this.removeCommand(cmdExit);  
  137.         } else if (cmd == cmdExit) {  
  138.             parent.notifyDestroyed();  
  139.         } else if (cmd == cmdRefresh) {  
  140.             this.initMap();  
  141.         } else if (cmd == cmdBefore && num > 0) {  
  142.             num--;  
  143.             img = myImage[num];  
  144.             this.initMap();  
  145.         }  
  146.           
  147.     }  
  148.   
  149.     public boolean isSuccess() {  
  150.   
  151.         for (int x = 0; x < 4; x++) {  
  152.             for (int y = 0; y < 4; y++) {  
  153.                 int x_src = map[x][y] / 10;  
  154.                 int y_src = map[x][y] % 10;  
  155.                 if (x_src != x || y_src != y) {  
  156.                     return false;  
  157.                 }  
  158.             }  
  159.         }  
  160.   
  161.         return true;  
  162.     }  
  163. //控制上下左右,移动拼图  
  164.     protected void keyPressed(int keyCode) {  
  165.         int action = this.getGameAction(keyCode);  
  166.         int xOf33 = -1;  
  167.         int yOf33 = -1;  
  168.   
  169.         for (int x = 0; x < 4; x++) {  
  170.             for (int y = 0; y < 4; y++) {  
  171.                 if (map[x][y] == 33) {  
  172.                     xOf33 = x;  
  173.                     yOf33 = y;  
  174.                     break;  
  175.                 }  
  176.             }  
  177.         }  
  178.   
  179.         switch (action) {  
  180.         case GameCanvas.UP:  
  181.             if (yOf33 != 3) {  
  182.                 swap(xOf33, yOf33, xOf33, yOf33 + 1);  
  183.             }  
  184.             break;  
  185.         case GameCanvas.DOWN:  
  186.             if (yOf33 != 0) {  
  187.                 swap(xOf33, yOf33, xOf33, yOf33 - 1);  
  188.             }  
  189.             break;  
  190.         case GameCanvas.LEFT:  
  191.             if (xOf33 != 3) {  
  192.                 swap(xOf33, yOf33, xOf33 + 1, yOf33);  
  193.             }  
  194.             break;  
  195.         case GameCanvas.RIGHT:  
  196.             if (xOf33 != 0) {  
  197.                 swap(xOf33, yOf33, xOf33 - 1, yOf33);  
  198.             }  
  199.             break;  
  200.         }  
  201.   
  202.         this.repaint();  
  203.     }  
  204.   
  205.     private void swap(int xOf33, int yOf33, int targetX, int targetY) {  
  206.         int temp = map[targetX][targetY];  
  207.         map[targetX][targetY] = 33;// 用33代替了map[xOf33][yOf33]  
  208.         map[xOf33][yOf33] = temp;  
  209.     }  
  210. //按下OK键的时候,显示原图,帮助拼图  
  211.     protected void keyRepeated(int keyCode) {  
  212.         int action = this.getGameAction(keyCode);  
  213.         if (action == GameCanvas.FIRE) {  
  214.             FLAG = 1;  
  215.             this.repaint();  
  216.             // System.out.println("OK键按下");  
  217.         }  
  218.     }  
  219. //松开OK键的时候,恢复游戏  
  220.     protected void keyReleased(int keyCode) {  
  221.         int action = this.getGameAction(keyCode);  
  222.         if (action == GameCanvas.FIRE) {  
  223.             FLAG = 0;  
  224.             this.repaint();  
  225.             // System.out.println("OK键松开");  
  226.         }  
  227.     }  
  228. //计时器  
  229.     public void run() {  
  230.         // TODO Auto-generated method stub  
  231.         while (true) {  
  232.             second++;  
  233.             if (second == 10) {  
  234.                 second = 0;  
  235.                 minute++;  
  236.             }  
  237.         }  
  238.     }  
  239. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值