MainGame.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:10k
源码类别:
J2ME
开发平台:
Java
- package cardclient;
- /**
- * <p>Title: CardClient</p>
- *
- * <p>Description: lizhenpeng</p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: LP&P</p>
- *
- * @author lipeng
- * @version 1.0
- */
- import java.io.*;
- import java.util.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.io.*;
- public class MainGame extends Canvas implements Runnable,CommandListener
- {
- public MainGame()
- {
- //super.setFullScreenMode(true);
- w = this.getWidth();
- h = this.getHeight();
- loadResource();
- gm = new GameManager();
- inst = this;
- input.addCommand(commandReg);
- input.setCommandListener(this);
- }
- public void loadResource()
- {
- try
- {
- cardImage=Image.createImage("/card.png");
- cursor=Image.createImage("/cursor.png");
- }
- catch(Exception e)
- {
- }
- }
- public void start()
- {
- gameThread=new Thread(this);
- gameThread.start();
- }
- public void run()
- {
- try
- {
- Thread currentThread=Thread.currentThread();
- while(currentThread==gameThread)
- {
- startTime=System.currentTimeMillis();
- switch(gm.gameState)
- {
- case 0:
- gm.menuAction();
- break;
- case 1:
- break;
- case 2:
- gm.waitAction();
- break;
- case 3:
- gm.gameAction();
- break;
- }
- repaint(0,0,w,h);
- serviceRepaints();
- endTime=System.currentTimeMillis();
- if((endTime-startTime)<FRAME_TIME)
- {
- Thread.sleep(FRAME_TIME-(endTime-startTime));
- }
- }
- } catch(InterruptedException ie)
- {
- System.out.println(ie.toString());
- }
- }
- public void paint(Graphics g)
- {
- g.setColor(0,128,0);
- g.fillRect(0,0,w,h);
- switch(gm.gameState)
- {
- case 0:
- drawMenu(g);
- break;
- case 2:
- drawWait(g);
- break;
- case 3:
- drawMainGame(g);
- break;
- }
- }
- private void drawMenu(Graphics g)
- {
- int menuOffset=(h-font.getHeight()*3)/2;
- g.setFont(font);
- int item0Color=0;
- int item1Color=0;
- int item2Color=0;
- switch(gm.menuWhichActive)
- {
- case 0:
- item0Color=255<<16;
- break;
- case 1:
- item1Color=255<<16;
- break;
- case 2:
- item2Color=255<<16;
- break;
- }
- g.setColor(item0Color);
- g.drawString("登陆系统",(w-font.stringWidth("登陆系统"))/2,
- menuOffset+font.getHeight()*0,g.LEFT|g.TOP);
- g.setColor(item1Color);
- g.drawString("帮助",(w-font.stringWidth("帮助"))/2,
- menuOffset+font.getHeight()*1,g.LEFT|g.TOP);
- g.setColor(item2Color);
- g.drawString("退出",(w-font.stringWidth("退出"))/2,
- menuOffset+font.getHeight()*2,g.LEFT|g.TOP);
- }
- private void drawWait(Graphics g)
- {
- g.setColor(255,0,0);
- g.drawString("等待",(w-font.stringWidth("等待"))/2,
- font.getHeight()*2,g.LEFT|g.TOP);
- }
- private void drawMainGame(Graphics g)
- {
- //每行14张牌
- int num=GameManager.inst.userCardNum;
- int intern=w/14;
- int x=0;
- int y=h-22*2;
- if(num<=14)
- {
- y=h-22;
- for(int i=0;i<num;i++)
- {
- if(GameManager.inst.isSelect)
- {
- if(GameManager.inst.cardSelect==i)
- {
- y -= 5;
- }
- else
- {
- y=h-22;
- }
- }
- if(GameManager.inst.userCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].kind-1),
- y-22*GameManager.inst.userCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- } else
- {
- x = 0;
- y=h-22*2;
- for(int i=0;i<14;i++)
- {
- if(GameManager.inst.isSelect)
- {
- if(GameManager.inst.cardSelect==i)
- {
- y-=5;
- } else
- {
- y=h-22*2;
- }
- }
- if(GameManager.inst.userCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].kind-1),
- y-22*GameManager.inst.userCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- x = 0;
- y = h-22;
- for(int i=14;i<num;i++)
- {
- if(GameManager.inst.isSelect)
- {
- if(GameManager.inst.cardSelect==i)
- {
- y-=5;
- } else
- {
- y=h-22;
- }
- }
- if(GameManager.inst.userCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].kind-1),
- y-22*GameManager.inst.userCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- }
- num=GameManager.inst.oppoCardNum;
- if(num<=22)
- {
- y=0;
- for(int i=0;i<num;i++)
- {
- if(GameManager.inst.oppoCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].kind-1),
- y-22*GameManager.inst.oppoCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- } else
- {
- x=0;
- y=22;
- for(int i=0;i<14;i++)
- {
- if(GameManager.inst.oppoCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].kind-1),
- y-22*GameManager.inst.oppoCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- x=0;
- y=22*2;
- for(int i=14;i<num;i++)
- {
- if(GameManager.inst.oppoCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].kind-1),
- y-22*GameManager.inst.oppoCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- }
- if(GameManager.inst.oppoPostCardNum!=0)
- {
- y = h/4;
- x = (w-GameManager.inst.oppoPostCardNum*intern)/2;
- for(int i = 0;i<GameManager.inst.oppoPostCardNum;i++)
- {
- if(GameManager.inst.oppoCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoPostCard[i].kind-1),
- y-22*GameManager.inst.oppoPostCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.oppoPostCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- }
- if(GameManager.inst.userPostCardNum!=0)
- {
- y=h*3/4;
- x=(w-GameManager.inst.userPostCardNum*intern)/2;
- for(int i=0;i<GameManager.inst.userPostCardNum;i++)
- {
- if(GameManager.inst.oppoCard[i].kind<5)
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userPostCard[i].kind-1),
- y-22*GameManager.inst.userPostCard[i].num,g.LEFT|g.TOP);
- x+=intern;
- } else
- {
- g.setClip(x,y,18,22);
- g.drawImage(cardImage,x-18*(GameManager.inst.userPostCard[i].num),
- y,g.LEFT|g.TOP);
- x+=intern;
- }
- }
- }
- g.setClip(0,0,w,h);
- if(GameManager.inst.iControl)
- {
- g.drawImage(cursor,GameManager.inst.cursorX,GameManager.inst.cursorY,
- g.LEFT|g.TOP);
- }
- }
- protected void keyPressed(int keyCode)
- {
- gm.keyPressd(keyCode,getGameAction(keyCode));
- }
- protected void keyReleased(int keyCode)
- {
- gm.keyReleased(keyCode,getGameAction(keyCode));
- }
- public void commandAction(Command command,Displayable displayable)
- {
- if(command==commandReg)
- {
- String userName = ((TextBox)displayable).getString();
- GameManager.inst.RegiterUser(userName);
- }
- else if(command==commandBegin)
- {
- UserList list = (UserList)displayable;
- int index = list.getSelectedIndex();
- String name = list.getString(index);
- GameManager.inst.BeginGame(name);
- }
- }
- Command commandReg = new Command("输入", Command.OK, 1);
- Command commandBegin = new Command("请求", Command.OK, 1);
- public UserList userList;
- public TextBox input = new TextBox("请输入用户名","",16,TextField.ANY);
- static public MainGame inst;
- private GameManager gm;
- private volatile Thread gameThread=null;
- private long startTime;
- private long endTime;
- private Image cardImage;
- private Image cursor;
- public int w;
- public int h;
- private static final int FRAME_TIME=80;
- private Font font=Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,
- Font.SIZE_MEDIUM);
- }