国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

一個(gè)象棋手機(jī)游戲的源代碼

2019-11-18 15:03:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近看到了一些五子棋,掃雷的代碼講解的文章,我也就寫(xiě)了個(gè)手機(jī)的象棋游戲,寫(xiě)的不是太全面,但還是能實(shí)現(xiàn)基本功能,共享出來(lái)供大家交流交流。

先介紹一下我的大體思路吧,我采用canvas讓手機(jī)自己畫(huà)出棋盤(pán)和棋子,而不是采用調(diào)用圖片,雖然麻煩,但能鍛煉自己的編程能力還能讓算法簡(jiǎn)單,同時(shí)還能節(jié)省空間。具體的細(xì)節(jié)在代碼中在說(shuō)吧。首先要有一個(gè)主程序Game,這里比較簡(jiǎn)單,大家一看就能明白,我就不多說(shuō)了.

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class Game extends MIDlet {
    GameCanvas game;//定義游戲界面的Canvas類(lèi)GameCanvas的對(duì)象gobang
    public Game() {
        super();
        game=new GameCanvas(this);//生成GameCanvas類(lèi)的對(duì)象game
    }
    PRotected void startApp(){
        Display.getDisplay(this).setCurrent(game);
          //在屏幕上繪出游戲見(jiàn)面game
    }
    protected void pauseApp(){
    }
    protected void destroyApp(boolean arg0){
    }
}

然后就是程序的主題部分了--GameCanvas,這里實(shí)現(xiàn)了從畫(huà)棋盤(pán)棋子一直到判定和輸出.
我的主題思想是把棋盤(pán)初始化為一個(gè)2維數(shù)組,在有棋子的地方初始化為非0數(shù),其他的都初始化為0;
大家可在代碼中看到,在圖象輸出和棋子移動(dòng)也都是基于這個(gè)數(shù)組進(jìn)行的。

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class GameCanvas extends Canvas implements CommandListener
{
    protected Game game;
    protected int empty;//屏幕右側(cè)留的空間
    protected int x;//棋盤(pán)輸出的坐標(biāo)
    protected int cellWidth;//每個(gè)棋格的邊長(zhǎng)
    protected int mapWidth,canvasW;//棋盤(pán)的寬度和畫(huà)布的寬度
    protected int a,b,c,d;//這是畫(huà)炮下面的那幾個(gè)折線,沒(méi)什么用
    protected int chessR;//棋子的半徑
    protected int selectedX,selectedY;//選擇框在棋盤(pán)格局上的x,y位置
    protected static int i,j;
    protected int m,n,p;//記住開(kāi)始的selectedX,selectedY和point[selectedX][selectedY]
    protected String q;//記住Word[selectedX][selectedY]
    protected int guard,guard1,guard2,g,g1;//標(biāo)記FIRE被按了多少次,g是用來(lái)判定走直線時(shí)前后的棋子,中間是否有其他棋子的累加器
    protected static int g2,isRedWin,isWhiteWin;//g2表示該誰(shuí)走了,后面那倆顧名思義了        
    protected Command exitCmd;
   
    protected int point[][]={{1,2,3,4,5,6,7,8,9},//初始化INT數(shù)組
        {0,0,0,0,0,0,0,0,0},
              {0,10,0,0,0,0,0,11,0},
        {12,0,13,0,14,0,15,0,16},
        {0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0},
        {28,0,29,0,30,0,31,0,32},
        {0,26,0,0,0,0,0,27,0},
        {0,0,0,0,0,0,0,0,0},
        {17,18,19,20,21,22,23,24,25}};
        
    protected String[][] word;
    public GameCanvas(){};
    public GameCanvas(Game game)//構(gòu)造函數(shù)
    {
       this.game=game;
      
       empty=getWidth()/6;
       x=empty*1/3;
 canvasW=getWidth()-empty;
       mapWidth=canvasW-canvasW%8;
       cellWidth=mapWidth/8;
       a=cellWidth*2/5;
       b=cellWidth/8;
       c=cellWidth-a;
       d=cellWidth-b;
       chessR=cellWidth*2/5;
       selectedX=0;
       selectedY=0;
       guard=0;
       guard1=selectedX;guard2=selectedY;
       m=guard1;n=guard2;
       word=new String[10][9];
       g2=1;
       for(i=0;i<10;i++)//初始化字符數(shù)組
       {
           for(j=0;j<9;j++)
    {
  if(i==0)
  {
  if(j==0){word[i][j]="車(chē)";}
  if(j==1){word[i][j]="馬";}
  if(j==2){word[i][j]="相";}
  if(j==3){word[i][j]="士";}
  if(j==4){word[i][j]="帥";}
  if(j==8){word[i][j]="車(chē)";}
  if(j==7){word[i][j]="馬";}
  if(j==6){word[i][j]="相";}
  if(j==5){word[i][j]="士";}
  }
  if(i==1){word[i][j]="空";}
  if(i==2){
   if((j!=1)&(j!=7)){word[i][j]="空";}
   if(j==1){word[i][j]="炮";}
   if(j==7){word[i][j]="炮";}
   }
  if(i==3){
   if(j%2==0){word[i][j]="卒";}
   if(j%2==1){word[i][j]="空";}
   }
  if(i==4){word[i][j]="空";}
  if(i==5){word[i][j]="空";}
  if(i==6){
   if(j%2==0){word[i][j]="卒";}
   if(j%2==1){word[i][j]="空";}
   }
  if(i==7){
   if((j!=1)&(j!=7)){word[i][j]="空";}
   if(j==1){word[i][j]="炮";}
   if(j==7){word[i][j]="炮";}
   }
  if(i==8){word[i][j]="空";}
  if(i==9)
  {
  if(j==0){word[i][j]="車(chē)";}
  if(j==1){word[i][j]="馬";}
  if(j==2){word[i][j]="相";}
  if(j==3){word[i][j]="士";}
  if(j==4){word[i][j]="帥";}
  if(j==8){word[i][j]="車(chē)";}
  if(j==7){word[i][j]="馬";}
  if(j==6){word[i][j]="相";}
  if(j==5){word[i][j]="士";}
  }
 
    }         
       }
 exitCmd = new Command("退出", Command.EXIT, 0);
       
        addCommand(exitCmd);
        setCommandListener(this);       
    }
    protected void paintMapa(Graphics g)//畫(huà)河的上半部分的棋盤(pán)
    {
        for(int q=0;q<4;q++)
          {
           for(int w=0;w<8;w++)
              {
                g.setColor(128,128,128);
  g.drawRect(x+w*cellWidth,x+q*cellWidth,cellWidth,cellWidth);
              }
          }
         g.setColor(128,128,128);
  g.drawLine(x+3*cellWidth,x,x+5*cellWidth,x+2*cellWidth);
         g.drawLine(x+5*cellWidth,x,x+3*cellWidth,x+2*cellWidth);
   
         //畫(huà)左上方的炮
         g.drawLine(x+d,x+cellWidth+c,x+d,x+cellWidth+d);//左上豎
         g.drawLine(x+c,x+cellWidth+d,x+d,x+cellWidth+d);//左上橫
      
         g.drawLine(x+d+2*b,x+cellWidth+c,x+d+2*b,x+cellWidth+d);//右上豎
         g.drawLine(x+cellWidth+b,x+cellWidth+d,x+cellWidth+a,x+cellWidth+d);//右上橫
       
         g.drawLine(x+d,x+2*cellWidth+b,x+d,x+2*cellWidth+a);//左下豎
         g.drawLine(x+c,x+cellWidth+d+2*b,x+d,x+cellWidth+d+2*b);//左下橫
       
         g.drawLine(x+d+2*b,x+2*cellWidth+b,x+d+2*b,x+2*cellWidth+a);//右下豎
         g.drawLine(x+cellWidth+b,x+cellWidth+d+2*b,x+cellWidth+a,x+cellWidth+d+2*b);//右下橫
 
         //畫(huà)右上方的炮



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌兰察布市| 辽阳市| 漳州市| 惠州市| 长治县| 建湖县| 汉沽区| 灵武市| 都兰县| 韶山市| 北川| 大田县| 苍溪县| 肇源县| 上虞市| 临邑县| 瑞昌市| 闸北区| 湖南省| 德州市| 马尔康县| 玉林市| 卢湾区| 晋中市| 肇州县| 西林县| 贺兰县| 凉山| 图木舒克市| 江安县| 江城| 云和县| 旌德县| 康定县| 阿图什市| 布尔津县| 寻乌县| 九江县| 岑巩县| 株洲市| 乡宁县|