⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 支持蓝牙的手机游戏 支持蓝牙的手机游戏的分析与实现
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package org.challenge.chengshi.game;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;/** * * @author challenge */public class Game {    private static final int FREEMODEL=10;    private static final int MOVEMODEL=11;    private boolean isSelect=false;    private static final int GEST=2;    private static final int HOST=1;    //game UI    private GameUI gameUI;    //is your turn to paly    private boolean isTurn;    private int startX;    private int startY;    private int lineLength;    //current X    private int currentX;    //current Y    private int currentY;    //    private int dx;    private int dy;    //the chess    private int[][] chess={        {0,0,0,0},        {0,0,0,0},        {0,0,0,0},        {0,0,0,0}    };    /*{        {1,2,1,2},        {2,1,2,1},        {2,1,1,2},        {2,1,2,2}    };*/    boolean isGameOver=false;    boolean isWin=false;    int state=0x00;    private Image nativeImage;        private Image cusorImage;        private Image selectImage;    private Image gestImage;    private int selectX=0;    private int selectY=0;    private int step=0;    //构造函数            public Game(GameUI gameUI) throws Exception{        this.gameUI=gameUI;//        gameUI.s="2";//        try{        //装载图片用于绘制游戏画面        if(this.gameUI.isServer){            this.isTurn=false;            this.nativeImage=Image.createImage("/server.png");            this.selectImage=Image.createImage("/selectedServer.png");            this.cusorImage=Image.createImage("/cursorServer.png");            this.gestImage=Image.createImage("/client.png");        }else{            this.isTurn=true;            this.nativeImage=Image.createImage("/client.png");            this.selectImage=Image.createImage("/selectedClient.png");            this.cusorImage=Image.createImage("/cursorClient.png");            this.gestImage=Image.createImage("/server.png");        }  //      }catch(Exception e){       //     this.gameUI.mid.show();    //    }                this.isSelect=false;        init();    }    //初始化操作    protected void init(){        this.startX=this.nativeImage.getWidth()/2;        this.startY=this.gameUI.getHeight()-this.gameUI.getWidth()-this.nativeImage.getWidth()/2;        if(this.startY<0){            this.startY=this.nativeImage.getWidth()/2;        }        this.lineLength=this.gameUI.getWidth()-this.nativeImage.getWidth();        dx=(this.lineLength)/3;        dy=dx;        this.currentX=0;        this.currentY=0;        this.state=0x00;        this.step=0;    }    //由GameUI调用的对画面的重绘操作    public void paintGame(Graphics g){        this.clearScreen(g);        this.paintMessage(g);        this.drawScreen(g);        this.paintChess(g);        this.paintSelect(g);                this.paintCursor(g);    }    //清屏,把画面变成全白    private void clearScreen(Graphics g){        int color=g.getColor();        g.setColor(0xFFFFFF);        g.fillRect(0, 0, this.gameUI.getWidth(), this.gameUI.getHeight());        g.setColor(color); //       g.drawString("123", 21, 89, Graphics.BASELINE|Graphics.HCENTER);    }    //显示该谁走棋    private void paintMessage(Graphics g){        int color=g.getColor();        g.setColor(0xFF0000);        if((this.state&0x01)==0x01){            if(this.isWin){                g.drawString("恭喜,你赢了!", this.gameUI.getWidth()/2, this.startY/2, Graphics.TOP|Graphics.HCENTER);            }else{                g.drawString("你输了,下次努力!", this.gameUI.getWidth()/2, this.startY/2, Graphics.TOP|Graphics.HCENTER);            }        }else{            if(this.isTurn){                g.drawString("请走棋!", this.gameUI.getWidth()/2, this.startY/2, Graphics.TOP|Graphics.HCENTER);            }else{                g.drawString("对方走棋!", this.gameUI.getWidth()/2, this.startY/2, Graphics.TOP|Graphics.HCENTER);            }        }                g.setColor(color);    }    //绘制棋盘4×4    private void drawScreen(Graphics g){        int x,y;        int color=g.getColor();        g.setColor(0x000000);        for(int i=0;i<4;i++){            x=this.startX+i*this.dx;            y=this.startY+i*this.dy;            g.drawLine(x, this.startY, x, this.startY+this.lineLength);            g.drawLine(this.startX, y, this.startX+this.lineLength, y);        }        g.setColor(color);    }    //画选择的棋子,在进入第二阶段时才可画出    private void paintSelect(Graphics g){        if(this.isSelect){            g.drawImage(this.selectImage, this.startX+this.selectY*this.dx, this.startY+this.selectX*this.dy, Graphics.HCENTER|Graphics.VCENTER);        }    }    //画出当前的游标位置    private void paintCursor(Graphics g){        g.drawImage(this.cusorImage, this.startX+this.currentY*this.dx, this.startY+this.currentX*this.dy, Graphics.HCENTER|Graphics.VCENTER);    }    //画棋子    private void paintChess(Graphics g){        int color=g.getColor();        //g.setColor(0x000000);        for(int i=0;i<4;i++){            for(int j=0;j<4;j++){                if(this.chess[i][j]==HOST){                    g.drawImage(this.nativeImage, this.startX+j*this.dx, this.startY+i*this.dy, Graphics.HCENTER|Graphics.VCENTER);                }else if(this.chess[i][j]==GEST){                    g.drawImage(this.gestImage, this.startX+j*this.dx, this.startY+i*this.dy, Graphics.HCENTER|Graphics.VCENTER);                }            }        }        g.setColor(color);    }    //由GameUI调用上下左右确定的操作事件    public void keyPressed(int code){        switch(code){            case Canvas.LEFT:                this.left();                break;            case Canvas.RIGHT:                this.right();                break;            case Canvas.UP:                this.up();                break;            case Canvas.DOWN:                this.down();                break;            case Canvas.FIRE:                this.fired();                break;        }    }    //往左走    private void left(){        this.currentY=(this.currentY+3)%4;        this.gameUI.repaint();    }    //往右走    private void right(){        this.currentY=(this.currentY+1)%4;        this.gameUI.repaint();    }    //往上走    private void up(){        this.currentX=(this.currentX+3)%4;        this.gameUI.repaint();    }    //往下走    private void down(){        this.currentX=(this.currentX+1)%4;        this.gameUI.repaint();    }    //(this.state&0x01)==0x0游戏未结束    private void fired(){        if(((this.state&0x01)==0x00)&&isTurn&&this.isFireOK()){            this.isWin=Books.go(chess, currentX, currentY);            if(this.isWin){                this.isGameOver=true;                this.state|=0x01;// 游戏结束末位置1            }            this.gameUI.repaint();            this.sendMessage();            this.isTurn=false;       }    }    //判断现在是否可以进行下棋    private boolean isFireOK(){        boolean returnB=false;                if((this.state&0x02)==0x00){            if(this.chess[this.currentX][this.currentY]==0){                this.chess[this.currentX][this.currentY]=HOST;                return true;            }else if(this.isChess()==false){                this.state|=0x02;            }        }        if((this.state&0x02)==0x02){            if(this.isSelect==false){                if(this.chess[this.currentX][this.currentY]==HOST){                    this.isSelect=true;                    this.selectX=this.currentX;                    this.selectY=this.currentY;                }            }else{                if((this.state&0x0E)==0x02||(this.state&0x0E)==0x06){                    if(this.selectX==this.currentX&&this.selectY==this.currentY){//移除一个子                        this.chess[this.currentX][this.currentY]=0;                        this.isSelect=false;                        returnB=true;                        if((this.state&0x0E)==0x02)                        {                            this.state=0x06;                        }else{                            this.state=0x0E;                        }                                            }                }else if((this.state&0x0E)==0x0E){                    if((this.currentX==this.selectX&&Math.abs(this.selectY-this.currentY)==1)||(this.currentY==this.selectY&&Math.abs(this.currentX-this.selectX)==1)){                        this.chess[this.currentX][this.currentY]=HOST;                        this.chess[this.selectX][this.selectY]=0;                        returnB=true;                        this.isSelect=false;                    }else if(this.currentX==this.selectX&&this.currentY==this.selectY){                        this.isSelect=false;                    }                }            }        }        return returnB;    }    //是否有空可以下棋    private boolean isChess(){        for(int i=0;i<4;i++){            for(int j=0;j<4;j++){                if(this.chess[i][j]==0){                    return true;                }            }        }        return false;    }    //发送信息,棋盘信息和状态信息    public void sendMessage(){        StringBuffer message=new StringBuffer("");        int k;        for(int i=0;i<4;i++){            for(int j=0;j<4;j++){                if(chess[i][j]==GEST){                    k=HOST;                }else if(chess[i][j]==HOST){                    k=GEST;                }else{                    k=0;                }                message.append(k);            }        }        message.append(this.state);        this.step++;        if((this.state&0x01)==0x01){            this.gameUI.save(this.step);        }        this.gameUI.sendMessage(message.toString());    }    //接收信息    public void receieveMessage(String message){        for(int i=0;i<16;i++){                this.chess[i/4][i%4]=Integer.parseInt(message.substring(i,i+1));                    }        int s=Integer.parseInt(message.substring(16,message.length()));        this.isTurn=true;        if((s&0x01)==0x01){//判断是否游戏结束            this.isWin=false;            this.state=s;        }else{            if((s&0x0E)==0x06){//是否第二结段并且该去子                this.state|=0x06;                this.isSelect=false;            }else if((s&0x0E)==0x0E){                this.state|=0x0E;                this.isSelect=false;            }            this.state=s;        }        this.state=s;        this.gameUI.repaint();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -