MainGame.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:10k
源码类别:

J2ME

开发平台:

Java

  1. package cardclient;
  2. /**
  3.  * <p>Title: CardClient</p>
  4.  *
  5.  * <p>Description: lizhenpeng</p>
  6.  *
  7.  * <p>Copyright: Copyright (c) 2005</p>
  8.  *
  9.  * <p>Company: LP&P</p>
  10.  *
  11.  * @author lipeng
  12.  * @version 1.0
  13.  */
  14. import java.io.*;
  15. import java.util.*;
  16. import javax.microedition.lcdui.*;
  17. import javax.microedition.io.*;
  18. public class MainGame extends Canvas implements Runnable,CommandListener
  19. {
  20.   public MainGame()
  21.   {
  22.     //super.setFullScreenMode(true);
  23.     w = this.getWidth();
  24.     h = this.getHeight();
  25.     loadResource();
  26.     gm = new GameManager();
  27.     inst = this;
  28.     input.addCommand(commandReg);
  29.     input.setCommandListener(this);
  30.   }
  31.   public void loadResource()
  32.   {
  33.     try
  34.     {
  35.       cardImage=Image.createImage("/card.png");
  36.       cursor=Image.createImage("/cursor.png");
  37.     }
  38.     catch(Exception e)
  39.     {
  40.     }
  41.   }
  42.   public void start()
  43.   {
  44.     gameThread=new Thread(this);
  45.     gameThread.start();
  46.   }
  47.   public void run()
  48.   {
  49.     try
  50.     {
  51.       Thread currentThread=Thread.currentThread();
  52.       while(currentThread==gameThread)
  53.       {
  54.         startTime=System.currentTimeMillis();
  55.         switch(gm.gameState)
  56.         {
  57.           case 0:
  58.             gm.menuAction();
  59.             break;
  60.           case 1:
  61.             break;
  62.           case 2:
  63.             gm.waitAction();
  64.             break;
  65.           case 3:
  66.             gm.gameAction();
  67.             break;
  68.         }
  69.         repaint(0,0,w,h);
  70.         serviceRepaints();
  71.         endTime=System.currentTimeMillis();
  72.         if((endTime-startTime)<FRAME_TIME)
  73.         {
  74.           Thread.sleep(FRAME_TIME-(endTime-startTime));
  75.         }
  76.       }
  77.     } catch(InterruptedException ie)
  78.     {
  79.       System.out.println(ie.toString());
  80.     }
  81.   }
  82.   public void paint(Graphics g)
  83.   {
  84.     g.setColor(0,128,0);
  85.     g.fillRect(0,0,w,h);
  86.     switch(gm.gameState)
  87.     {
  88.       case 0:
  89.         drawMenu(g);
  90.         break;
  91.       case 2:
  92.         drawWait(g);
  93.         break;
  94.       case 3:
  95.         drawMainGame(g);
  96.         break;
  97.     }
  98.   }
  99.   private void drawMenu(Graphics g)
  100.   {
  101.     int menuOffset=(h-font.getHeight()*3)/2;
  102.     g.setFont(font);
  103.     int item0Color=0;
  104.     int item1Color=0;
  105.     int item2Color=0;
  106.     switch(gm.menuWhichActive)
  107.     {
  108.       case 0:
  109.         item0Color=255<<16;
  110.         break;
  111.       case 1:
  112.         item1Color=255<<16;
  113.         break;
  114.       case 2:
  115.         item2Color=255<<16;
  116.       break;
  117.     }
  118.     g.setColor(item0Color);
  119.     g.drawString("登陆系统",(w-font.stringWidth("登陆系统"))/2,
  120.                  menuOffset+font.getHeight()*0,g.LEFT|g.TOP);
  121.     g.setColor(item1Color);
  122.     g.drawString("帮助",(w-font.stringWidth("帮助"))/2,
  123.                  menuOffset+font.getHeight()*1,g.LEFT|g.TOP);
  124.     g.setColor(item2Color);
  125.     g.drawString("退出",(w-font.stringWidth("退出"))/2,
  126.                  menuOffset+font.getHeight()*2,g.LEFT|g.TOP);
  127.   }
  128.   private void drawWait(Graphics g)
  129.   {
  130.     g.setColor(255,0,0);
  131.     g.drawString("等待",(w-font.stringWidth("等待"))/2,
  132.                  font.getHeight()*2,g.LEFT|g.TOP);
  133.   }
  134.   private void drawMainGame(Graphics g)
  135.   {
  136.     //每行14张牌
  137.     int num=GameManager.inst.userCardNum;
  138.     int intern=w/14;
  139.     int x=0;
  140.     int y=h-22*2;
  141.     if(num<=14)
  142.     {
  143.       y=h-22;
  144.       for(int i=0;i<num;i++)
  145.       {
  146.         if(GameManager.inst.isSelect)
  147.         {
  148.           if(GameManager.inst.cardSelect==i)
  149.           {
  150.             y -= 5;
  151.           }
  152.           else
  153.           {
  154.             y=h-22;
  155.           }
  156.         }
  157.         if(GameManager.inst.userCard[i].kind<5)
  158.         {
  159.           g.setClip(x,y,18,22);
  160.           g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].kind-1),
  161.                       y-22*GameManager.inst.userCard[i].num,g.LEFT|g.TOP);
  162.           x+=intern;
  163.         } else
  164.         {
  165.           g.setClip(x,y,18,22);
  166.           g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].num),
  167.                       y,g.LEFT|g.TOP);
  168.           x+=intern;
  169.         }
  170.       }
  171.     } else
  172.     {
  173.       x = 0;
  174.       y=h-22*2;
  175.       for(int i=0;i<14;i++)
  176.       {
  177.         if(GameManager.inst.isSelect)
  178.         {
  179.           if(GameManager.inst.cardSelect==i)
  180.           {
  181.             y-=5;
  182.           } else
  183.           {
  184.             y=h-22*2;
  185.           }
  186.         }
  187.         if(GameManager.inst.userCard[i].kind<5)
  188.         {
  189.           g.setClip(x,y,18,22);
  190.           g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].kind-1),
  191.                       y-22*GameManager.inst.userCard[i].num,g.LEFT|g.TOP);
  192.           x+=intern;
  193.         } else
  194.         {
  195.           g.setClip(x,y,18,22);
  196.           g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].num),
  197.                       y,g.LEFT|g.TOP);
  198.           x+=intern;
  199.         }
  200.       }
  201.       x = 0;
  202.       y = h-22;
  203.       for(int i=14;i<num;i++)
  204.       {
  205.         if(GameManager.inst.isSelect)
  206.         {
  207.           if(GameManager.inst.cardSelect==i)
  208.           {
  209.             y-=5;
  210.           } else
  211.           {
  212.             y=h-22;
  213.           }
  214.         }
  215.         if(GameManager.inst.userCard[i].kind<5)
  216.         {
  217.           g.setClip(x,y,18,22);
  218.           g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].kind-1),
  219.                       y-22*GameManager.inst.userCard[i].num,g.LEFT|g.TOP);
  220.           x+=intern;
  221.         } else
  222.         {
  223.           g.setClip(x,y,18,22);
  224.           g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].num),
  225.                       y,g.LEFT|g.TOP);
  226.           x+=intern;
  227.         }
  228.       }
  229.     }
  230.     num=GameManager.inst.oppoCardNum;
  231.     if(num<=22)
  232.     {
  233.       y=0;
  234.       for(int i=0;i<num;i++)
  235.       {
  236.         if(GameManager.inst.oppoCard[i].kind<5)
  237.         {
  238.           g.setClip(x,y,18,22);
  239.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].kind-1),
  240.                       y-22*GameManager.inst.oppoCard[i].num,g.LEFT|g.TOP);
  241.           x+=intern;
  242.         } else
  243.         {
  244.           g.setClip(x,y,18,22);
  245.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].num),
  246.                       y,g.LEFT|g.TOP);
  247.           x+=intern;
  248.         }
  249.       }
  250.     } else
  251.     {
  252.       x=0;
  253.       y=22;
  254.       for(int i=0;i<14;i++)
  255.       {
  256.         if(GameManager.inst.oppoCard[i].kind<5)
  257.         {
  258.           g.setClip(x,y,18,22);
  259.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].kind-1),
  260.                       y-22*GameManager.inst.oppoCard[i].num,g.LEFT|g.TOP);
  261.           x+=intern;
  262.         } else
  263.         {
  264.           g.setClip(x,y,18,22);
  265.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].num),
  266.                       y,g.LEFT|g.TOP);
  267.           x+=intern;
  268.         }
  269.       }
  270.       x=0;
  271.       y=22*2;
  272.       for(int i=14;i<num;i++)
  273.       {
  274.         if(GameManager.inst.oppoCard[i].kind<5)
  275.         {
  276.           g.setClip(x,y,18,22);
  277.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].kind-1),
  278.                       y-22*GameManager.inst.oppoCard[i].num,g.LEFT|g.TOP);
  279.           x+=intern;
  280.         } else
  281.         {
  282.           g.setClip(x,y,18,22);
  283.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].num),
  284.                       y,g.LEFT|g.TOP);
  285.           x+=intern;
  286.         }
  287.       }
  288.     }
  289.     if(GameManager.inst.oppoPostCardNum!=0)
  290.     {
  291.       y = h/4;
  292.       x = (w-GameManager.inst.oppoPostCardNum*intern)/2;
  293.       for(int i = 0;i<GameManager.inst.oppoPostCardNum;i++)
  294.       {
  295.         if(GameManager.inst.oppoCard[i].kind<5)
  296.         {
  297.           g.setClip(x,y,18,22);
  298.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoPostCard[i].kind-1),
  299.                       y-22*GameManager.inst.oppoPostCard[i].num,g.LEFT|g.TOP);
  300.           x+=intern;
  301.         } else
  302.         {
  303.           g.setClip(x,y,18,22);
  304.           g.drawImage(cardImage,x-18*(GameManager.inst.oppoPostCard[i].num),
  305.                       y,g.LEFT|g.TOP);
  306.           x+=intern;
  307.         }
  308.       }
  309.     }
  310.     if(GameManager.inst.userPostCardNum!=0)
  311.     {
  312.       y=h*3/4;
  313.       x=(w-GameManager.inst.userPostCardNum*intern)/2;
  314.       for(int i=0;i<GameManager.inst.userPostCardNum;i++)
  315.       {
  316.         if(GameManager.inst.oppoCard[i].kind<5)
  317.         {
  318.           g.setClip(x,y,18,22);
  319.           g.drawImage(cardImage,x-18*(GameManager.inst.userPostCard[i].kind-1),
  320.                       y-22*GameManager.inst.userPostCard[i].num,g.LEFT|g.TOP);
  321.           x+=intern;
  322.         } else
  323.         {
  324.           g.setClip(x,y,18,22);
  325.           g.drawImage(cardImage,x-18*(GameManager.inst.userPostCard[i].num),
  326.                       y,g.LEFT|g.TOP);
  327.           x+=intern;
  328.         }
  329.       }
  330.     }
  331.     g.setClip(0,0,w,h);
  332.     if(GameManager.inst.iControl)
  333.     {
  334.       g.drawImage(cursor,GameManager.inst.cursorX,GameManager.inst.cursorY,
  335.                   g.LEFT|g.TOP);
  336.     }
  337.   }
  338.   protected void keyPressed(int keyCode)
  339.   {
  340.     gm.keyPressd(keyCode,getGameAction(keyCode));
  341.   }
  342.   protected void keyReleased(int keyCode)
  343.   {
  344.     gm.keyReleased(keyCode,getGameAction(keyCode));
  345.   }
  346.   public void commandAction(Command command,Displayable displayable)
  347.   {
  348.     if(command==commandReg)
  349.     {
  350.       String userName = ((TextBox)displayable).getString();
  351.       GameManager.inst.RegiterUser(userName);
  352.     }
  353.     else if(command==commandBegin)
  354.     {
  355.       UserList list = (UserList)displayable;
  356.       int index = list.getSelectedIndex();
  357.       String name = list.getString(index);
  358.       GameManager.inst.BeginGame(name);
  359.     }
  360.   }
  361.   Command commandReg = new Command("输入", Command.OK, 1);
  362.   Command commandBegin = new Command("请求", Command.OK, 1);
  363.   public UserList userList;
  364.   public TextBox input = new TextBox("请输入用户名","",16,TextField.ANY);
  365.   static public MainGame inst;
  366.   private GameManager  gm;
  367.   private volatile Thread gameThread=null;
  368.   private long startTime;
  369.   private long endTime;
  370.   private Image cardImage;
  371.   private Image cursor;
  372.   public int w;
  373.   public int h;
  374.   private static final int FRAME_TIME=80;
  375.   private Font font=Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,
  376.                                  Font.SIZE_MEDIUM);
  377. }