J2ME连连看基础功能源代码(含详细注释)
时间:2011-02-04 csdn博客 陈跃峰
//界面类代码
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
* 连连看游戏界面
*/
public class LinkCanvas extends Canvas implements Runnable{
/**游戏逻辑类*/
GameEngine engine;
/**屏幕宽度*/
int width;
/**屏幕高度*/
int height;
public LinkCanvas(){
//创建对象
engine = new GameEngine();
//获得屏幕的高度和宽度
width = getWidth();
height = getHeight();
//启动线程
Thread t = new Thread(this);
t.start();
}
/**
* 绘制方法
*/
protected void paint(Graphics g) {
//清屏
clearScreen(g);
//绘制地图
engine.paintMap(g);
//绘制选择框
engine.paintSelectArea(g);
//绘制连线
engine.paintLinkLine(g);
}
/**
* 清屏方法
* @param g 画笔
*/
private void clearScreen(Graphics g){
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.setColor(0);
}
public void keyPressed(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case UP:
engine.moveUP();
break;
case DOWN:
engine.moveDown();
break;
case LEFT:
engine.moveLeft();
break;
case RIGHT:
engine.moveRight();
break;
case FIRE:
engine.fire();//选择块
break;
}
}
public void run() {
try{
while(true){
//延时
Thread.sleep(100);
//每次判断逻辑
engine.action();
repaint();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
//逻辑类源代码
import java.util.*;
import javax.microedition.lcdui.*;
/**
* 游戏数据和逻辑类
*/
public class GameEngine {
/**选中块的个数*/
private int selectTileNum = 0;
//第一个选择块的行号和列号
/**行号*/
private int firstRow;
/**列号*/
private int firstCol;
//第二个选择块的行号和列号
/**行号*/
private int secondRow;
/**列号*/
private int secondCol;
//当前选择框,默认在左上角
/**当前选择框的行号*/
private int cRow;
/**当前选择框的列号*/
private int cCol;
/**最大行数*/
private final int MAX_ROW = 10;
/**最大列数*/
private final int MAX_COL = 10;
/**地图数据,0代表空,数据1-10分别代表十种不同的结构*/
private int[][] map = new int[MAX_ROW][MAX_COL];
/**随机数对象*/
private Random ran = new Random();
//地图区域左上角的坐标
private final int LEFTX = 20;
private final int LEFTY = 50;
/**每个单元格的宽度*/
private final int TILE_WIDTH = 20;
/**每个单元格的高度*/
private final int TILE_HEIGHT = 20;
|