一个简单的游戏源代码

  一个简单的游戏源代码

作者:陈跃峰
出自: http://blog.csdn.net/mailbomb
 
       实现一个简单的翻牌游戏,每次最多翻开两个数字,如果数字相同则消失,否则两个数字不显示,可以继续游戏。
       游戏规则如下:
       1、游戏数据使用两个一维数组存储:
u       map存储逻辑数据
u       displayMap存储绘制的数据,-1代表已经翻开,0代表未翻开,大于0代表已经翻开。
2、绘制规则:
u       在屏幕中心绘制图形
u       每个单元格宽度和高度都是20,每个单元格之间间隔4
3、游戏结束判别规则:
u       单元格全部消失或者只有一个单元格剩余
实现代码如下:
package simplegame;
 
import java.util.Random;
import javax.microedition.lcdui.*;
 
/**
 * 翻图游戏
 * 游戏数据分别存储在两个数组,
 *       1、数组map存放实际的地图数据,每行显示的数据长度为关卡数字+1
 *       2、已经翻开的地图数据存储在displayMap数组中,-1代表已经翻开,0代表未翻开,大于0代表已经翻开
 * @author 陈跃峰
 * @version 1.0
 */
public class SimpleGameCanvas extends Canvas implements Runnable {
    /**地图数据,地图数据的长度是(level + 1) * (level + 1)*/
    private int[] map;
    /**显示的数据,0代表未显示,-1代表消失,大于0代表翻开的数据*/
    private int[] displayMap;
    /**关卡*/
    private int level = 1;
    /**已经翻开的数字数量*/
    int num = 0;
    /**选择框序号,编号规则为从左到右,从上到下*/
    int index = 0;
    /**第一个被翻开的方块*/
    int firstIndex = 0;
    /**第二个被翻开的方块*/
    int secondIndex = 0;
    /**屏幕宽度*/
    private int width;
    /**屏幕高度*/
    private int height;
    /**每个单元格宽度*/
    private final int TILE_WIDTH = 20;
    /**每个单元格高度*/
    private final int TILE_HEIGHT = 20;
    /**每个单元格之间的间隔*/
    private final int INNER_WIDTH = 4;
 
    /**
     * 默认构造方法
     */
    public SimpleGameCanvas() {
        //初始化数据
        width = this.getWidth();
        height = this.getHeight();
        init(level);
        //启动线程
        new Thread(this).start();
    }
 
    protected void paint(Graphics g) {
        //清屏
        g.setColor(0xffffff);
        g.fillRect(0, 0, width, height);
        g.setColor(0);
        //绘制数字
        drawNumber(g);
        //绘制关卡数字
        g.drawString("第" + level + "关", this.getWidth() / 2, 15,
                     Graphics.BASELINE | Graphics.HCENTER);
    }
 
     private void drawNumber(Graphics g) {
        //屏幕中心点坐标
        int xCenter = width / 2;
        int yCenter = height / 2;
        //左上坐标
        int x = xCenter -
                ((level + 1) * TILE_WIDTH + ((level + 1) - 1) * INNER_WIDTH) /
                2;
        int y = yCenter -
                ((level + 1) * TILE_HEIGHT + ((level + 1) - 1) * INNER_WIDTH) /
                2;
        //循环绘制
        for (int i = 0; i < displayMap.length; i++) {
            //绘制边框和数字
            g.setColor(0);
            if (displayMap[i] >= 0) {
                g.fillRect(x + (i % (level + 1)) * (TILE_WIDTH + INNER_WIDTH),
                           y + (i / (level + 1)) * (TILE_HEIGHT + INNER_WIDTH),
                           TILE_WIDTH,
                           TILE_HEIGHT);
                //绘制内部的数字
                if (displayMap[i] > 0) {
                    g.setColor(0xffffff);
                    g.drawString(String.valueOf(displayMap[i]),
                                 x +
                                 (i % (level + 1)) * (TILE_WIDTH + INNER_WIDTH) +
                                 7,
                                 y +
                                 (i / (level + 1)) * (TILE_HEIGHT + INNER_WIDTH) +
                                 2,
                                 Graphics.TOP | Graphics.LEFT
                            );
                }
            }
        }
        //绘制选择框
        g.setColor(0xff0000);
        g.drawRect(x + index % (level + 1) * (TILE_WIDTH + INNER_WIDTH) - 1,
                   y + (index / (level + 1)) * (TILE_HEIGHT + INNER_WIDTH) - 1,
                   TILE_WIDTH + 1,
                   TILE_HEIGHT + 1);
 
    }
 
    /**
     * 根据关卡初始化地图数据
     * @param level 等级
     */
    private void init(int level) {
        //初始化地图数据大小
        map = new int[(level + 1) * (level + 1)];
        //初始化地图数据
        for (int i = 0; i < map.length; i++) {
            map[i] = i % (map.length / 2) + 1;
        }
        //随机打乱地图数据
        Random ran = new Random();
        int index = 0;
        int temp;
        for (int i = 0; i < map.length; i++) {
            index = Math.abs(ran.nextInt() % map.length);
            //交换数据
            temp = map[i];
            map[i] = map[index];
            map[index] = temp;
        }
 
        //初始化显示地图数据大小
        displayMap = new int[(level + 1) * (level + 1)];
    }
 
    public void keyPressed(int keyCode) {
        int action = this.getGameAction(keyCode);
        switch (action) {
        case UP:
            if (index - level - 1 >= 0) {
                index = index - level - 1;
            }
            break;
        case DOWN:
            if (index + level + 1 <= (level + 1) * (level + 1) - 1) {
                index = index + level + 1;
            }
            break;
        case LEFT:
            if (index > 0) {
                index--;
            }
            break;
        case RIGHT:
            if (index < (level + 1) * (level + 1) - 1) {
                index++;
            }
            break;
        case FIRE: //确定
            if (num < 2 && displayMap[index] >= 0) { //翻开的数量小于2
                num++; //翻开的数量增加1
                //记录索引值
                if (num == 1) {
                    firstIndex = index;
                } else if (num == 2 && index != firstIndex) {
                    secondIndex = index;
                }
 
                //显示该数字
                displayMap[index] = map[index];
            }
        }
        repaint();
    }
 
    /**
     * 是否升级
     * 剩余1个或者是全部消失则升级
     * @return true代表升级
     */
    private boolean isLevelUp() {
        int n = 0; //存储没有翻开的方块数量
        for (int i = 0; i < displayMap.length; i++) {
            if (displayMap[i] != -1) {
                n++;
            }
        }
        if (n > 1) {
            return false;
        } else {
            return true;
        }
    }
 
    public void run() {
        try {
            while (true) {
                //暂停
                Thread.sleep(100);
                //逻辑处理
                //处理逻辑
                if (num == 2) {
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {}
 
                    if (map[firstIndex] == map[secondIndex]) { //内容相同
                        displayMap[firstIndex] = -1;
                        displayMap[secondIndex] = -1;
                    } else {
                        displayMap[firstIndex] = 0;
                        displayMap[secondIndex] = 0;
                    }
                    num = 0;
                }
                //升级
                if (isLevelUp()) {
                    level++; //等级+1
                    init(level); //初始化地图数据
                    num = 0; //翻开的数量归零
                    index = 0; //默认选择第一个方块
                }
                //重新绘制
                repaint();
            }
        } catch (Exception e) {}
    }
}

你可能感兴趣的:(J2ME学习)