论坛首页 移动开发技术论坛

【示例代码】利用UDE开发黑白棋游戏(二)

浏览 3219 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-11-22  

3、源码解析

(1)主类(Main),用于控制游戏的主流程

public class Main extends Api {
//从游戏结果页面返回到游戏主界面
public static void back(Object[] args){
  UiApi.forward("/xml/main.xml");
  Player.initChessBoard();
  PlayerWhite.initChessBoard();
 
  setState.showScreen();
}

//初始化棋盘
public static void restart(Object[] args){
  Player.initChessBoard();
  PlayerWhite.initChessBoard(); 
  setState.showScreen();
}

//退出应用
public static void exitSys(Object[] args){ 
  Api.exit();
}
/**
  * 应用程序入口方法。
  *
  * @param args
  */

public static void main(Object[] args) {
  //读取应用样式
  UiApi.loadCss("/css/style.css");
  // 显示首页
  UiApi.forward("/xml/main.xml");
  Player.initChessBoard();
  PlayerWhite.initChessBoard();
 
  setState.showScreen();
}

//点击棋盘事件
public static void btn_pressed(Object[] args){
 
  int xp,yp;
  xp=Integer.parseInt((String)args[0]);
  yp=Integer.parseInt((String)args[1]);
  //设置走棋后的棋盘页面
  setState.next(xp,yp);
  //刷新界面
  setState.showScreen();
 
  WidgetUtil.UiInvoke("whiter_p", "setStyleClass", new String[]{"whiter"});
  WidgetUtil.UiInvoke("arrows_p", "setStyleClass", new String[]{"arrowsblack"});
  WidgetUtil.UiInvoke("blacker_p", "setStyleClass", new String[]{"blackerhover"});
 
  setState.check();
 
}

//棋子的焦点事情
public static void onbtnFocus(Object[] args){
  String x=null;
  String y=null;
  x=(String)args[0];
  y=(String)args[1];
}
}

(2) 棋盘类(Game),定义并初始化棋盘矩阵
public class Game
{
//定义一个变量,用来判断轮到谁下棋,初始化为黑棋
public static int m_turn = AllDef.BLACK;
//当前状态为准备状态
public int m_state = AllDef.READY;

//定义棋盘盘面,初始化为-1
public static int[][] m_board = {{-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1},
  {-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1},
  {-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1}};

public Game()
{
}}

(3) 全局状态类(AllDef),棋子和游戏的状态
public class AllDef
{
//白棋
public static final int WHITE = 0;
//黑棋
public static final int BLACK = 1;
//错误或越界
public static final boolean ERROR = false;
//正常
public static final boolean OK    = true;
//输棋
public static final int NOWIN = 2;
//棋盘已满
public static final int NOSET = 3;
//赢棋
public static final int WIN   = 4;

//游戏状态
//下棋
public static final int RUN    = 1;
//等待
public static final int READY  = 2;
//结束
public static final int SCORE  = 3;
//帮助
public static final int ABOUT  = 4;

}

(4) 黑棋类(Player.java),控制黑棋的活动

public class Player
{
public static int m_turn;
public int m_scroe;

Player(int who)
{
  //初始化当前玩家(黑棋)
  m_turn = who;
  //初始化结果(子数)
  m_scroe = 0;
}
//初始化棋盘,棋盘中心黑子2粒、白子2粒
public static void initChessBoard()
{
  m_turn = AllDef.BLACK;
  //全部先初始化为空棋盘
  for(int x=1; x<=8; x++)
  {
   for(int y=1; y<=8; y++)
   {
    Game.m_board[x][y] = -1;//
   }
  }
  //棋盘中心黑子2粒、白子2粒
  Game.m_board[4][4] = AllDef.BLACK;//黑棋
  Game.m_board[5][5] = AllDef.BLACK;//黑棋
  Game.m_board[4][5] = AllDef.WHITE;//白棋
  Game.m_board[5][4] = AllDef.WHITE;//白棋
}

//获取坐标点上的棋子
public static int getChessMan(int x, int y)
{
  return Game.m_board[x][y];
}
//吃棋子,更新棋盘状态
public static void changeBoard(int x, int y, int who)
{
  Game.m_board[x][y] = -1;
  Game.m_board[x][y] = who;
}

private static boolean eat(int x, int y, int xChange, int yChange)
{
  //如果放子位置坐标过大或过小,返回false
  if(x<1 || x>8 || y<1 || y>8)
  {
   return AllDef.ERROR;
  }
  if(getChessMan(x,y) != -1)
  {
   return AllDef.ERROR;
  }
  int xNext = x + xChange;
  int yNext = y + yChange;
  while(true)
  {
   //如果延着制定方向,下一个子的坐标过大或过小,及超出棋盘范围,返回false
   //如果下一个棋子和要下的棋子是同一种颜色,返回false
   if(xNext<1 || xNext>8 || yNext<1 || yNext>8||
    getChessMan(xNext, yNext) == -1 ||
    getChessMan(x+xChange, y+yChange) == m_turn)
   {
    return AllDef.ERROR;
   }
   //吃棋子
   if(getChessMan(xNext, yNext) == m_turn)
   {
    do
    {
     x = x + xChange;
     y = y + yChange;
     changeBoard(x,y,m_turn); //吃棋子
    }while(x!=xNext || y!=yNext);
    break;
   }
   xNext = xNext + xChange;
   yNext = yNext + yChange;
  }
  return AllDef.OK;
}
//判断棋局
private static boolean judge(int x, int y, int xChange, int yChange)
{
  int xNext = x + xChange;
  int yNext = y + yChange;
  if(getChessMan(x,y) != -1)
  {
   return AllDef.ERROR;
  }
  while(true)
  {
   //越界判断
   if(xNext<1 || xNext>8 || yNext<1 || yNext>8||
    getChessMan(xNext, yNext) == -1 ||
    getChessMan(x+xChange, y+yChange) == m_turn)
   {
    return AllDef.ERROR;
   }
   //已下棋子区域判断
   if(getChessMan(xNext, yNext) == m_turn)
   {
    break;
   }
   xNext = xNext + xChange;
   yNext = yNext + yChange;
  }
  return AllDef.OK;
}
//判断棋局输赢
static public int result()
{
  //返回结果 WIN NOWIN NOSET
  int x,y;
  boolean result = false;
  for(x=1; x<=8 && false==result; x++)
  {
   for(y=1; y<=8 && false==result; y++)
   {
    result = judge(x,y,0,-1) || result;
    result = judge(x,y,0,1)  || result;
    result = judge(x,y,-1,-1)|| result;
    result = judge(x,y,-1,0) || result;
    result = judge(x,y,-1,1) || result;
    result = judge(x,y,1,-1) || result;
    result = judge(x,y,1,0)  || result;
    result = judge(x,y,1,1)  || result;
   }
  }
  if(false == result)
  {
   return AllDef.NOSET;
  }
 
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if (getChessMan(x, y) != -1 &&
      getChessMan(x,y) != m_turn)
    {
     return AllDef.NOWIN;
    }
   }
  }
  return AllDef.WIN;
}

//计算棋盘中的黑棋或白棋数
public static int getNum()
{
  int num = 0;
  for(int x=1; x<=8; x++)
  {
   for(int y=1; y<=8; y++)
   {
    if (getChessMan(x, y) != -1 &&
      getChessMan(x,y) == m_turn)
    {
     num++;
    }
   }
  }
  return num;
}
//判断能不能下函数
public static boolean setChess(int x, int y)

  //返回结果 OK ERROR
  boolean result = false;
  //分别在八个方向上吃子
  result = eat(x,y,0,-1) || result;
  result = eat(x,y,0,1)  || result;
  result = eat(x,y,-1,-1)|| result;
  result = eat(x,y,-1,0) || result;
  result = eat(x,y,-1,1) || result;
  result = eat(x,y,1,-1) || result;
  result = eat(x,y,1,0)  || result;
  result = eat(x,y,1,1)  || result;
  if(AllDef.OK == result)
  {
   changeBoard(x, y, m_turn);
  }
  return result;
}

public static int getChess(int x, int y)
{
  //获取坐标点上的棋子
  return getChessMan(x, y);
}
public void init()
{
  //初始化棋盘,棋盘中心黑子2粒、白子2粒
  initChessBoard();
}
public int[] aiXY()
{
  int x,y;
  int xy[] = new int[2];
  boolean temp[][] = new boolean[1+8][1+8];
  //遍历可下棋子位置,true为OK,false为NO
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    temp[x][y] = false;
    boolean result = false;
    result = judge(x,y,0,-1) || result;
    result = judge(x,y,0,1) || result;
    result = judge(x,y,-1,-1) || result;
    result = judge(x,y,-1,0) || result;
    result = judge(x,y,-1,1) || result;
    result = judge(x,y,1,-1) || result;
    result = judge(x,y,1,0) || result;
    result = judge(x,y,1,1) || result;
    if(result == true )
    {
     temp[x][y] = true;
    }
   }
  }
  //查找最优位置下棋点
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
 
    if(true==temp[x][y])
    {
     if((1==x||3==x||6==x||8==x) && (1==y||3==y||6==y||8==y) && x!=y && x!=y+3 && y!=x+3)
     {
      xy[0]= x;
      xy[1] = y;
      return xy;
     }
    }
   }
  }
  //查找次优位置下棋点
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if((2==x||7==x||2==y||7==y) && true==temp[x][y])
    {
     continue;
    }
    if(true==temp[x][y])
    {
     xy[0]= x;
     xy[1] = y;
     return xy;
    }
   }
  }
  //查找较优位置下棋点
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if((2==x&&7==y) || (7==x&&2==y) || (2==x&&2==y) || (7==x&&7==y) ||
       (1==x&&2==y) || (2==x&&1==y) || (1==x&&7==y) || (7==x&&1==y) ||
       (8==x&&2==y) || (2==x&&8==y) || (8==x&&7==y) || (7==x&&8==y)
       )
    {
     continue;
    }
    if(true==temp[x][y])
    {
     xy[0]= x;
     xy[1] = y;
     return xy;
    }
   }
  }
  //没有最佳位置时,默认选择第一个可放位置
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if(true==temp[x][y])
    {
     xy[0]= x;
     xy[1] = y;
     return xy;
    }
   }
  }
  //无处放棋子时,返回-2
  xy[0] = xy[1] = -2;
  return xy;
}
}

(5) 白棋类(PlayerWhite),控制白棋的活动
public class PlayerWhite
{

public int m_scroe;

PlayerWhite(int who)
{
  //初始化当前玩家(白棋)
  m_turn = who;
  //初始化结果(白棋子数)
  m_scroe = 0;
}

//初始化棋盘,棋盘中心黑子2粒、白子2粒
public static void initChessBoard()
{
  m_turn = AllDef.WHITE;
  //全部先初始化为空棋盘
  for(int x=1; x<=8; x++)
  {
   for(int y=1; y<=8; y++)
   {
    Game.m_board[x][y] = -1;
   }
  }
  //棋盘中心黑子2粒、白子2粒
  Game.m_board[4][4] = AllDef.BLACK;//黑棋
  Game.m_board[5][5] = AllDef.BLACK;//黑棋
  Game.m_board[4][5] = AllDef.WHITE;//白棋
  Game.m_board[5][4] = AllDef.WHITE;//白棋
}

//获取坐标点上的棋子
public static int getChessMan(int x, int y)
{
  return Game.m_board[x][y];
}

//吃棋子,更新棋盘状态
public static void changeBoard(int x, int y, int who)
{
  Game.m_board[x][y] = who;
}

private static boolean eat(int x, int y, int xChange, int yChange)
{
  //如果放子位置坐标过大或过小,返回false
  if(x<1 || x>8 || y<1 || y>8)
  {
   return AllDef.ERROR;
  }
  if(getChessMan(x,y) != -1)
  {
   return AllDef.ERROR;
  }
  int xNext = x + xChange;
  int yNext = y + yChange;
  while(true)
  {
   //如果延着制定方向,下一个子的坐标过大或过小,及超出棋盘范围,返回false
   //如果下一个棋子和要下的棋子是同一种颜色,返回false
   if(xNext<1 || xNext>8 || yNext<1 || yNext>8||
    getChessMan(xNext, yNext) == -1 ||
    getChessMan(x+xChange, y+yChange) == m_turn)
   {
    return AllDef.ERROR;
   }
   //吃棋子
   if(getChessMan(xNext, yNext) == m_turn)
   {
    do
    {
     x = x + xChange;
     y = y + yChange;
     changeBoard(x,y,m_turn); //吃棋子
    }while(x!=xNext || y!=yNext);
    break;
   }
   xNext = xNext + xChange;
   yNext = yNext + yChange;
  }
  return AllDef.OK;
}

//判断棋局
private static boolean judge(int x, int y, int xChange, int yChange)
{
  int xNext = x + xChange;
  int yNext = y + yChange;
  //无子可下
  if(getChessMan(x,y) != -1)
  {
   return AllDef.ERROR;
  }
  while(true)
  {
   //越界判断
   if(xNext<1 || xNext>8 || yNext<1 || yNext>8||
    getChessMan(xNext, yNext) == -1 ||
    getChessMan(x+xChange, y+yChange) == m_turn)
   {
    return AllDef.ERROR;
   }
   //已下棋子区域判断
   if(getChessMan(xNext, yNext) == m_turn)
   {
    break;
   }
   xNext = xNext + xChange;
   yNext = yNext + yChange;
  }
  return AllDef.OK;
}
//判断棋局输赢
public static  int result()
{
  //返回结果 WIN NOWIN NOSET
  int x,y;
  boolean result = false;
  for(x=1; x<=8 && false==result; x++)
  {
   for(y=1; y<=8 && false==result; y++)
   {
    result = judge(x,y,0,-1) || result;
    result = judge(x,y,0,1)  || result;
    result = judge(x,y,-1,-1)|| result;
    result = judge(x,y,-1,0) || result;
    result = judge(x,y,-1,1) || result;
    result = judge(x,y,1,-1) || result;
    result = judge(x,y,1,0)  || result;
    result = judge(x,y,1,1)  || result;
   }
  }
  if(false == result)
  {
   //无子可下
   return AllDef.NOSET;
  }
 
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if (getChessMan(x, y) != -1 &&
      getChessMan(x,y) != m_turn)
    {
     //输棋
     return AllDef.NOWIN;
    }
   }
  }
  //赢棋
  return AllDef.WIN;
}

//计算棋盘中的黑棋或白棋数
public static int getNum()
{
  int num = 0;
  for(int x=1; x<=8; x++)
  {
   for(int y=1; y<=8; y++)
   {
    if (getChessMan(x, y) != -1 &&
      getChessMan(x,y) == m_turn)
    {
     num++;
    }
   }
  }
  return num;
}

//判断能不能下函数
public static boolean setChess(int x, int y)
{
  //返回结果 OK ERROR
  boolean result = false;
  //分别在八个方向上吃子
  result = eat(x,y,0,-1) || result;
  result = eat(x,y,0,1)  || result;
  result = eat(x,y,-1,-1)|| result;
  result = eat(x,y,-1,0) || result;
  result = eat(x,y,-1,1) || result;
  result = eat(x,y,1,-1) || result;
  result = eat(x,y,1,0)  || result;
  result = eat(x,y,1,1)  || result;
  if(AllDef.OK == result)
  {
   changeBoard(x, y, m_turn);
  }
  return result;
}

//获取坐标点上的棋子
public static int getChess(int x, int y)
{
  return getChessMan(x, y);
}

//初始化棋盘,棋盘中心黑子2粒、白子2粒
public void init()
{
  initChessBoard();
}


public static int[] aiXY()
{
  int x,y;
  int xy[] = new int[2];
  //初始化棋盘
  boolean temp[][] = {{false,false,false,false,false,false,false,false,false},{false,false,false,false,false,false,false,false,false},
    {false,false,false,false,false,false,false,false,false},{false,false,false,false,false,false,false,false,false},
    {false,false,false,false,false,false,false,false,false},{false,false,false,false,false,false,false,false,false},
    {false,false,false,false,false,false,false,false,false},{false,false,false,false,false,false,false,false,false},
    {false,false,false,false,false,false,false,false,false}};
 
  //遍历可下棋子位置,true为OK,false为NO
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    temp[x][y] = false;
    boolean result = false;
    result = judge(x,y,0,-1) || result;
    result = judge(x,y,0,1) || result;
    result = judge(x,y,-1,-1) || result;
    result = judge(x,y,-1,0) || result;
    result = judge(x,y,-1,1) || result;
    result = judge(x,y,1,-1) || result;
    result = judge(x,y,1,0) || result;
    result = judge(x,y,1,1) || result;
    if(result == true )
    {
     temp[x][y] = true;
    }
   }
  }
  //查找最优位置下棋点
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
 
    if(true==temp[x][y])
    {
     if((1==x||3==x||6==x||8==x) && (1==y||3==y||6==y||8==y) && x!=y && x!=y+3 && y!=x+3)
     {
      xy[0]= x;
      xy[1] = y;
      return xy;
     }
    }
   }
  }
  //查找较优位置下棋点
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if((2==x||7==x||2==y||7==y) && true==temp[x][y])
    {
     continue;
    }
    if(true==temp[x][y])
    {
     xy[0]= x;
     xy[1] = y;
     return xy;
    }
   }
  }
  //查找次优位置下棋点
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if((2==x&&7==y) || (7==x&&2==y) || (2==x&&2==y) || (7==x&&7==y) ||
       (1==x&&2==y) || (2==x&&1==y) || (1==x&&7==y) || (7==x&&1==y) ||
       (8==x&&2==y) || (2==x&&8==y) || (8==x&&7==y) || (7==x&&8==y)
       )
    {
     continue;
    }
    if(true==temp[x][y])
    {
     xy[0]= x;
     xy[1] = y;
     return xy;
    }
   }
  }
  //没有最佳位置时,默认选择第一个可放位置
  for(x=1; x<=8; x++)
  {
   for(y=1; y<=8; y++)
   {
    if(true==temp[x][y])
    {
     xy[0]= x;
     xy[1] = y;
     return xy;
    }
   }
  }
  //无处放棋子时,返回-2
  xy[0] = xy[1] = -2;
  return xy;
}
}

(6) 棋子状态类(setState),设置棋子的状态
public class setState {
 
public static void resultPage(int w,int b,String winner){
  Api.info("resultPage----------------"); 
  UiApi.forward("/xml/result.xml");
  WidgetUtil.setText("txtWhiteScore", "白棋数是:"+w);
  WidgetUtil.setText("txtBlackScore", "黑棋数是:"+b);
  if("BLACK".equals(winner)){
   WidgetUtil.setText("txtWinner", "黑棋赢了!");
  }else{
   WidgetUtil.setText("txtWinner", "白棋赢了!");
  }
}

//设置页面显示样式效果
public static void setUiState(String id,String state){
  String btnId=null;
  btnId=id;
  String btnState=null;
  btnState=state;
  if("white".equals(btnState)){
   WidgetUtil.UiInvoke(btnId+"_p", "setStyleClass", new String[]{"onwhitestate"});
  }else if("none".equals(btnState)){
   WidgetUtil.UiInvoke(btnId+"_p", "setStyleClass", new String[]{"normalstate"});
  }else{
   WidgetUtil.UiInvoke(btnId+"_p", "setStyleClass", new String[]{"onblackstate"});
  }
}

//设置走棋后的棋盘页面
public static void next(int xp,int yp){
  //如果当前是黑棋
  if( AllDef.BLACK == Game.m_turn )
  {
   if(Player.setChess(xp, yp))
   {
    if(Game.m_turn == AllDef.BLACK)
     Game.m_turn = AllDef.WHITE;
    else
     Game.m_turn = AllDef.BLACK;
   }
  }
  //如果当前是白棋
  if( AllDef.WHITE == Game.m_turn )
  {
   WidgetUtil.UiInvoke("whiter_p", "setStyleClass", new String[]{"whiterhover"});
   WidgetUtil.UiInvoke("arrows_p", "setStyleClass", new String[]{"arrowswhiter"});
   WidgetUtil.UiInvoke("blacker_p", "setStyleClass", new String[]{"blacker"});
  
   int xx[] = PlayerWhite.aiXY();
   if(PlayerWhite.setChess(xx[0], xx[1]))
   {
    if(Game.m_turn == AllDef.BLACK)
     Game.m_turn = AllDef.WHITE;
    else
     Game.m_turn = AllDef.BLACK;
   }
  }
}

//页面显示
public static void showScreen(){
  for(int x=1; x<=8; x++){
   for(int y=1; y<=8; y++){
   
    if(-1 == Player.getChess(x, y)){
     setState.setUiState(x+"_"+y, "none");
     continue;
    }else if(AllDef.BLACK == Player.getChess(x, y)){
     setState.setUiState(x+"_"+y, "black");
    
    }else if (AllDef.WHITE == PlayerWhite.getChess(x, y)){
     setState.setUiState(x+"_"+y, "white");
    }
   }
  }
}

//显示黑白棋有效棋子数目
public static void check(){
  WidgetUtil.setText("txtWhite", "白棋:"+PlayerWhite.getNum());
  WidgetUtil.setText("txtBlack", " 黑棋:"+Player.getNum());
  int ws=PlayerWhite.getNum();
  int bs=Player.getNum();
  String winner=""; 
 
  //等待白棋走下一步
  if(AllDef.NOSET == Player.result())
  {
   Game.m_turn = AllDef.WHITE;
   next(1,1);
  }
  //等待黑棋走下一步
  if(AllDef.NOSET == PlayerWhite.result())
  {
   Game.m_turn = AllDef.BLACK;
  }
 
  //黑棋获胜
  if(AllDef.WIN == Player.result())
  {
   Api.info("WIN");
   Player.initChessBoard();
   PlayerWhite.initChessBoard();
   setState.showScreen();
   //显示获胜页面
   resultPage(ws,bs,"BLACK");
  }
 
  //白棋获胜
  else if(AllDef.WIN == PlayerWhite.result())
  {
   Api.info(" PlayerWhite WIN");
   Player.initChessBoard();
   PlayerWhite.initChessBoard();
   setState.showScreen();
   //显示获胜页面
   resultPage(ws,bs,"WHITE");
  }
 
  if(PlayerWhite.getNum()+Player.getNum() == 64)
  {
   if(PlayerWhite.getNum() > Player.getNum())
   {
    Api.info(" White WIN");
    WidgetUtil.setText("txtMsg", "白棋获胜,得分是 "+PlayerWhite.getNum());
    winner="WHITE";
   }
   else if(PlayerWhite.getNum() < Player.getNum())
   {
    WidgetUtil.setText("txtMsg", "黑棋获胜 ,得分是 "+Player.getNum());
    winner="BLACK";
   }
   else
   {
    WidgetUtil.setText("txtMsg", "平局 !");
   }
  
   Player.initChessBoard();
   PlayerWhite.initChessBoard();
  
   setState.showScreen();
   resultPage(ws,bs,winner);
  }
}
}

4 界面控制

(1) 基础css样式设计(style.css)

应用工程每组分辨率下的每一个XML文件都对应一个页面,而每个页面布局及各组件的表现风格则通过style.css的样式设定。此样式文件语法规则和普通HTML页面的样式文件是完全一致的,即选择符(selector),属性(properties)和属性取值(value),语法:selector {property:value} (选择符 {属性:值})
例如:单选按钮组件的样式定义
radioButton{
align:left;  /* 字体对齐方式 */
bg-align:left;  /* 背景对齐方式 */
bg-image:url(radioButton.png,13,0,13,13);  /* 背景图片 */
bg-repeat:1 1;  /* 背景图片重绘方式 1-不重绘 0-重绘 */
min-size:240 13;   /* 最小的限制大小 */
padding:0 0 0 13;  /* 内间距 */
}

(2)黑白棋游戏主页面(mail.xml)
<!-- 根元素 -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- 屏幕元素,也就是J2ME中的画布 -->
<screen style="align:center;layout:inlinelayout(false,fill)">
<!--容器元素,类似样式中的DIV元素-->
<container style="layout: gridlayout(8,; " nearfocus="true">
  <!--按钮元素,在黑白棋游戏中,表示一个棋子,棋盘大小8X8-->
  <button id="1_1"><picture id="1_1_p"></picture></button>
  <button id="1_2"><picture id="1_2_p"></picture></button>
  <button id="1_3"><picture id="1_3_p"></picture></button>
  <button id="1_4"><picture id="1_4_p"></picture></button>
  <button id="1_5"><picture id="1_5_p"></picture></button>
  <button id="1_6"><picture id="1_6_p"></picture></button>
  <button id="1_7"><picture id="1_7_p"></picture></button>
  <button id="1_8"><picture id="1_8_p"></picture></button> 
……
</container>
<!--文本元素 -->
<text id="txtWhite"></text>
<text id="txtBlack"></text>
<text id="txtNextSet"></text>

<!--菜单元素 -->
<screenSecondMenu>退出</screenSecondMenu>
</screen>

(3)黑白棋游戏结果面(result.xml)

<?xml version="1.0" encoding="UTF-8"?>
<screen style="align:center;layout:inlinelayout(false,fill)">
<!--文本框:白棋子数 -->
<text id="txtWhiteScore"></text>
<!--文本框:黑棋子数 -->
<text id="txtBlackScore"></text>
<!--文本框:赢棋方 -->
<text id="txtWinner"></text>
<!-- 返回按钮事件 -->
<screenFirstMenu>BACK</screenFirstMenu>
<screenFirstMenu ></screenFirstMenu>
</screen>

统一开发环境UDE下载

【示例代码】利用UDE开发黑白棋游戏(一)
【示例代码】利用UDE开发黑白棋游戏(三)

 

论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics