您好,欢迎光临本网站![请登录][注册会员]  
文件名称: J2ME 手机对对碰游戏
  所属分类: Java
  开发工具:
  文件大小: 480kb
  下载次数: 0
  上传时间: 2010-07-09
  提 供 者: ma0hu*****
 详细说明: J2ME对对碰 import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import java.util.Random; public class MainMIDP extends MIDlet { private Display display = Display.getDisplay(this); private GameScreen gs; public MainMIDP() { gs = new GameScreen(); display.setCurrent(gs); } protected void startApp() { } protected void pauseApp() { } public void destroyApp(boolean arg0) { display.setCurrent(null); notifyDestroyed(); } } final class GameScreen extends Canvas implements Runnable { int SCR EEN_W; int SCREEN_H; final int caseWidth = 22; final int BODY_W = 8; final int BODY_H = 8; final int blockCount = 7; Random random = new Random(); Image[] block = new Image[blockCount]; Image bg, cursor, bombing, imgNum01, imgScore, imgTen; final int clearFrameMax = 6; final int moveSpeed = 5; final int moveFrameMax = 4; final int csFrameMax = 5; final int cursorW = 26; final int clearW = 20; int moveFrame, csFrame, clearFrame, gameScore, iDisScore, beginDrawX, beginDrawY, currentX, currentY, selectedX, selectedY; int[][] body = new int[BODY_W][BODY_H]; int[][] tempMove = new int[BODY_W][BODY_H]; int[][] clear = new int[BODY_W][BODY_H]; boolean isSelected, isClear, isExchange, isReExchange; boolean isDown = true; public GameScreen() { setFullScreenMode(true); loadImg(); repaint(); SCREEN_W = getWidth(); SCREEN_H = getHeight(); beginDrawX = (SCREEN_W - BODY_W * caseWidth) >> 1; beginDrawY = (SCREEN_H - BODY_H * caseWidth) >> 1; new Thread(this).start(); } public void run() { long sleepRate = 50L; while (true) { long time = System.currentTimeMillis(); logic(); repaint(); serviceRepaints(); time = System.currentTimeMillis() - time; try { if (time < sleepRate) { Thread.sleep(sleepRate - time); } } catch (InterruptedException e) { } } } public void loadImg() { for (int i = 0; i < block.length; i++) { block[i] = createImage(i + ".png"); } bg = createImage("bg.png"); cursor = createImage("cursor.png"); bombing = createImage("bombing.png"); imgNum01 = createImage("num01.png"); imgScore = createImage("imgscore.png"); imgTen = createImage("ten.png"); } static Image createImage(String pngPath) { try { Image img = Image.createImage("/" + pngPath); return img; } catch (Exception e) { return null; } } public void logic() { csFrame = isSelected ? 2 : (++csFrame + csFrameMax) % csFrameMax; if (iDisScore < gameScore) { iDisScore += 10; } if (isClear) { clearFrame++; if (clearFrame > clearFrameMax) { clearFrame = 0; doClear(); isClear = false; isDown = doDown(); } } if (isDown || isExchange || isReExchange) { moveFrame++; } if (isDown) { if (moveFrame >= moveFrameMax) { moveFrame = 0; isDown = doDown(); if (!isDown) { isClear = checkClear(); } } } if (isExchange) { if (moveFrame >= moveFrameMax) { moveFrame = 0; isExchange = false; tempMove = new int[BODY_W][BODY_H]; isClear = checkClear(); if (!isClear) { isReExchange = true; doExchange(); } } } if (isReExchange) { if (moveFrame >= moveFrameMax) { tempMove = new int[BODY_W][BODY_H]; moveFrame = 0; isReExchange = false; } } } public void paint(Graphics g) { g.setColor(0xFFFFFF); g.setClip(0, 0, SCREEN_W, SCREEN_H); g.fillRect(0, 0, SCREEN_W, SCREEN_H); g.drawImage(bg, beginDrawX, beginDrawY, Graphics.TOP | Graphics.LEFT); for (int i = 0; i < tempMove.length; i++) { for (int j = 0; j < tempMove[i].length; j++) { switch (tempMove[i][j]) { case 1: paintBlock(g, body[i][j], (i - 1) * caseWidth + (moveFrame * moveSpeed), (j - 1) * caseWidth + (moveFrame * moveSpeed)); break; case 2: paintBlock(g, body[i][j], i * caseWidth, (j - 1) * caseWidth + (moveFrame * moveSpeed)); break; case 3: paintBlock(g, body[i][j], (i + 1) * caseWidth - (moveFrame * moveSpeed), (j - 1) * caseWidth + (moveFrame * moveSpeed)); break; case 4: paintBlock(g, body[i][j], (i - 1) * caseWidth + (moveFrame * moveSpeed), j * caseWidth); break; case 6: paintBlock(g, body[i][j], (i + 1) * caseWidth - (moveFrame * moveSpeed), j * caseWidth); break; case 7: paintBlock(g, body[i][j], (i - 1) * caseWidth + (moveFrame * moveSpeed), (j + 1) * caseWidth - (moveFrame * moveSpeed)); break; case 8: paintBlock(g, body[i][j], i * caseWidth, (j + 1) * caseWidth - (moveFrame * moveSpeed)); break; case 9: paintBlock(g, body[i][j], (i + 1) * caseWidth - (moveFrame * moveSpeed), (j + 1) * caseWidth - (moveFrame * moveSpeed)); break; default: if (clear[i][j] != 0) { g.setClip(beginDrawX, beginDrawY, caseWidth * BODY_W, caseWidth * BODY_H); g.drawImage(imgTen, beginDrawX + i * caseWidth, beginDrawY + j * caseWidth - clearFrame * 5, Graphics.TOP | Graphics.LEFT); g.setClip(beginDrawX + i * caseWidth, beginDrawY + j * caseWidth, clearW, clearW); g.drawImage(bombing, beginDrawX + i * caseWidth - ((clearFrame / 2) * clearW), beginDrawY + j * caseWidth, Graphics.TOP | Graphics.LEFT); } else { paintBlock(g, body[i][j], i * caseWidth, j * caseWidth); } break; } } } g.setClip(0, 0, SCREEN_W, SCREEN_H); g.drawImage(imgScore, 0, 0, Graphics.TOP | Graphics.LEFT); paintNumber(g, iDisScore, imgScore.getWidth(), 0, 9, 13); g.setClip(beginDrawX + currentX * caseWidth - ((cursorW - caseWidth) >> 1), beginDrawY + currentY * caseWidth - ((cursorW - caseWidth) >> 1), cursorW, cursorW); g.drawImage(cursor, beginDrawX + currentX * caseWidth - ((cursorW - caseWidth) >> 1) - (csFrame * cursorW), beginDrawY + currentY * caseWidth - ((cursorW - caseWidth) >> 1), Graphics.TOP | Graphics.LEFT); } private boolean doDown() { boolean isFull = false; for (int i = 0; i < body.length; i++) { for (int j = body[i].length - 1; j >= 0; j--) { tempMove[i][j] = 0; if (body[i][j] == 0) { isFull = true; for (int k = j; k >= 0; k--) { tempMove[i][k] = 2; if (k == 0) { body[i][k] = Math .abs(random.nextInt() % blockCount) + 1; } else { body[i][k] = body[i][k - 1]; } } break; } } } return isFull; } private void doExchange() { if (currentX - selectedX == -1) { if (currentY - selectedY == -1) { tempMove[currentX][currentY] = 9; tempMove[selectedX][selectedY] = 1; } else if (currentY - selectedY == 0) { tempMove[currentX][currentY] = 6; tempMove[selectedX][selectedY] = 4; } else if (currentY - selectedY == 1) { tempMove[currentX][currentY] = 3; tempMove[selectedX][selectedY] = 7; } } else if (currentX - selectedX == 0) { if (currentY - selectedY == -1) { tempMove[currentX][currentY] = 8; tempMove[selectedX][selectedY] = 2; } else if (currentY - selectedY == 1) { tempMove[currentX][currentY] = 2; tempMove[selectedX][selectedY] = 8; } } else if (currentX - selectedX == 1) { if (currentY - selectedY == -1) { tempMove[currentX][currentY] = 7; tempMove[selectedX][selectedY] = 3; } else if (currentY - selectedY == 0) { tempMove[currentX][currentY] = 4; tempMove[selectedX][selectedY] = 6; } else if (currentY - selectedY == 1) { tempMove[currentX][currentY] = 1; tempMove[selectedX][selectedY] = 9; } } int temp = body[selectedX][selectedY]; body[selectedX][selectedY] = body[currentX][currentY]; body[currentX][currentY] = temp; } private void doClear() { for (int i = 0; i < body.length; i++) { for (int j = 0; j < body[i].length; j++) { if (clear[i][j] == 1) { clear[i][j] = body[i][j] = 0; gameScore += 10; } } } } private boolean checkClear() { boolean clearBlock = false; for (int i = 0; i < body.length; i++) { for (int j = 0; j < body[i].length; j++) { if (j > 0 && j < body[i].length - 1 && body[i][j] == body[i][j - 1] && body[i][j] == body[i][j + 1]) { clear[i][j] = clear[i][j - 1] = clear[i][j + 1] = 1; clearBlock = true; } if (i > 0 && i < body.length - 1 && body[i][j] == body[i - 1][j] && body[i][j] == body[i + 1][j]) { clear[i][j] = clear[i - 1][j] = clear[i + 1][j] = 1; clearBlock = true; } } } return clearBlock; } private void paintBlock(Graphics g, int index, int x, int y) { index--; if (index >= 0 && index < block.length) { g.setClip(beginDrawX, beginDrawY, caseWidth * BODY_W, caseWidth * BODY_H); g.drawImage(block[index], beginDrawX + x, beginDrawY + y, Graphics.TOP | Graphics.LEFT); } } protected void keyPressed(int keyCode) { if (getGameAction(keyCode) == FIRE) { if (isDown || isExchange || isReExchange) { return; } isSelected = !isSelected; selectedX = currentX; selectedY = currentY; } else { if (isExchange || isReExchange) { return; } if (getGameAction(keyCode) == UP) { moveUp(); } else if (getGameAction(keyCode) == DOWN) { moveDown(); } else if (getGameAction(keyCode) == LEFT) { moveLeft(); } else if (getGameAction(keyCode) == RIGHT) { moveRight(); } else if (keyCode == KEY_NUM1) { moveLeftUp(); } else if (keyCode == KEY_NUM3) { moveRightUp(); } else if (keyCode == KEY_NUM7) { moveLeftDown(); } else if (keyCode == KEY_NUM9) { moveRightDown(); } } } public void setExchange() { if (isSelected) { isExchange = true; isSelected = false; doExchange(); } } private void moveRightDown() { if (currentX == BODY_W - 1 || currentY == BODY_H - 1) { isSelected = false; } currentX = ++currentX % BODY_W; currentY = ++currentY % BODY_H; setExchange(); } private void moveLeftDown() { if (currentX == 0 || currentY == BODY_H - 1) { isSelected = false; } currentX = (--currentX + BODY_W) % BODY_W; currentY = ++currentY % BODY_H; setExchange(); } private void moveRightUp() { if (currentY == 0 || currentX == BODY_W - 1) { isSelected = false; } currentX = ++currentX % BODY_W; currentY = (--currentY + BODY_H) % BODY_H; setExchange(); } private void moveLeftUp() { if (currentX == 0 || currentY == 0) { isSelected = false; } currentX = (--currentX + BODY_W) % BODY_W; currentY = (--currentY + BODY_H) % BODY_H; setExchange(); } private void moveUp() { if (currentY == 0) { isSelected = false; } currentY = (--currentY + BODY_H) % BODY_H; setExchange(); } private void moveDown() { if (currentY == BODY_H - 1) { isSelected = false; } currentY = ++currentY % BODY_H; setExchange(); } private void moveLeft() { if (currentX == 0) { isSelected = false; } currentX = (--currentX + BODY_W) % BODY_W; setExchange(); } private void moveRight() { if (currentX == BODY_W - 1) { isSelected = false; } currentX = ++currentX % BODY_W; setExchange(); } private void paintNumber(Graphics g, int num, int x, int y, int w, int h) { String tmpStr = "" + Math.abs(num); for (int i = 0; i < tmpStr.length(); i++) { g.setClip(x + i * w, y, w, h); g.drawImage(imgNum01, x + (i - Integer.parseInt(tmpStr.charAt(i) + "")) * w, y, Graphics.TOP | Graphics.LEFT); } g.setClip(0, 0, SCREEN_W, SCREEN_H); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: J2ME
 输入关键字,在本站1000多万海量源码库中尽情搜索: