import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
class mainGame extends GameCanvas implements Runnable{
private int width,height;
private boolean isPlay;
private Graphics g;
private boolean result=false;
private PL1 mySprite;
private PL2 mySprite2;
private Image img;
private Sprite pl1b;
private Sprite pl2b;
private int sx;
private int sy;
private int bx;
private int by;
private int S_UP = 0;
private int S_DOWN = 1;
private int S_RIGHT = 2;
private int S_LEFT = 3;
private int i=1;
private Image tileImg;
private TiledLayer bg;
private LayerManager lm;
private int life = 8;
private boolean success = false;
private int[][] map = { //背景图分成180格,12列,15行
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36},
{37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48},
{49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
{61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72},
{73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84},
{85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96},
{97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108},
{109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120},
{121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132},
{133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144},
{145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156},
{157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168},
{169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180}
};
private boolean couldstate3=true;
private boolean state3=false;
private boolean state4=false;
public mainGame(){
super(true);
width = getWidth();//取得背景图宽度
height = getHeight();//取得背景图高度
g = getGraphics();//用画笔画
try{
img = Image.createImage("/player.png");
mySprite = new PL1(img,50,73);
img = Image.createImage("/player2.png");
mySprite2 = new PL2(img,50,73);
tileImg = Image.createImage("/tiles.png");//背景图
bg = new TiledLayer(12,15,tileImg,20,20);
for(int i = 0;i<12;i++)
for(int j =0 ;j<15;j++)
bg.setCell(i,j,map[j][i]);
lm = new LayerManager();//设置层管理
lm.append(bg);//显示层
img = Image.createImage("/player1blood.png");//自己血样的图像显示
pl1b = new Sprite(img,100,20);
img = Image.createImage("/player2blood.png");//AI的血样图像显示
pl2b = new Sprite(img,100,20);
}catch(Exception e){
e.printStackTrace();
}
//开始时的自己和AI的位置设置
sx = 40;
sy = 210;
bx=160;
by=210;
}
public void start(){
isPlay = true;//大招可以用
Thread t = new Thread(this);//线程应用
t.start();
}
public void run(){
while(isPlay){
input();
drawScreen(g);
AImove();
drawScreen2(g);
try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void input(){//按键设置
int ks = getKeyStates();
if( (ks & LEFT_PRESSED) != 0 ){ //如果按左
sx = sx -20;//自己横坐标左移20
if(sx < 0)
sx = 0;
if((sx-bx)<20&sy==by&(sx+20-bx)>0)//纵坐标相等且在Ai右边的时候
sx = sx +20;//自己横坐标右移20
mySprite.setTransform(Sprite.TRANS_MIRROR);//ai自动向右看
}
else if( (ks & RIGHT_PRESSED) != 0 ){ //如果按右
sx= sx +20;
if(sx <= width - 20){ //没有到最右边
int ci = sx/20;//
int ri = sy/20;//
if(map[ri][ci] == 4){//
sx=sx-20;//
}
if(map[ri][ci] == 5){//火焰
map[ri][ci] = 1;
life--;
}
if(map[ri][ci] == 6){//水晶
map[ri][ci] = 1;
life++;
}
bg.setCell(ci,ri,map[ri][ci]);
}
if(sx >width-20){//超出屏幕宽度范围,则横坐标停止在宽度减20的位置
sx = width -20;
}
if((bx-sx)<20&sy==by&(bx-sx+20)>0)//纵坐标一样,ai在自己右边时候,自己再往右则停止在ai左边
sx = sx -20;
mySprite.setTransform(Sprite.TRANS_NONE);//ai不用转脸
}
else if( (ks & UP_PRESSED) != 0 ){ //如果按上
sy = sy - 7 ;
if(sy < 210){
sy = 210;
}
//mySprite.setFrame(0);
}
else if( (ks & DOWN_PRESSED) != 0 ){ //如果按下
sy = sy + 7;
if(sy>217)
sy = 217;
//mySprite.setFrame(0);
}
else if( (ks & FIRE_PRESSED) != 0 ){ //如果按select
if(state3)//进入状态3
{
mySprite.setFrame(4);
mySprite.acst1=System.currentTimeMillis() ;
i--;
gongji();
}
else{//否则
mySprite.setFrame(1);//显示第二帧即出拳的状态
mySprite.acst=System.currentTimeMillis() ;//记下当前时刻
mySprite.state=1;//自己进入状态1
}
}
else if( (ks & GAME_A_PRESSED ) != 0 ){ //如果按1键即大招
System.out.println("ab");
if(couldstate3)//进入大招状态
{
System.out.println("fdr");
mySprite.setFrame(3);//显示第四帧
mySprite.s3acst=System.currentTimeMillis() ;//记录当前状态的时刻
mySprite.state=3;//进入状态3
state3 = true;
couldstate3=false;//大招用完一次消失,不可再出大招
// mySprite.setFrame(0);
}
}
}
public void AImove(){ //ai 移动设置
if((bx-sx)>40){ //AI自动向左移动
bx = bx -20;//AI横坐标左移20
if(bx < 0)
bx = 0;
mySprite.setTransform(Sprite.TRANS_NONE);//角色不转脸
}
if((sx-bx>40)){ //AI自动向右移动
bx=bx+20; //AI横坐标右移20
if(bx<0)
bx=0;
mySprite.setTransform(Sprite.TRANS_MIRROR);//角色自动向左看
}
if((sx-bx)<40&(bx-sx)<40&sy>by){
by=by+7;
if(by>217)
by = 217;
}
if((sx-bx)<40&(bx-sx)<40&sy<by){
by=by-7;
if(by>210)
by = 210;
}
if((bx-sx)<60&sy==by&(sx-bx<60)){
if(mySprite2.state==0) {//进入状态0
mySprite2.setFrame(1); //显示第二帧即出拳的状态
mySprite2.acst=System.currentTimeMillis() ; //记下当前时刻
mySprite2.state=1; //AI进入状态1
}
}
}
public void drawScreen2(Graphics g){
if(mySprite2.state==1)//自己进入状态1
{
mySprite2.acet=System.currentTimeMillis();
if(mySprite2.acet-mySprite2.acst>10)
{
mySprite2.state=0;//ai进入状态0
mySprite2.setFrame(0);
}
if(mySprite2.collidesWith(mySprite, true)&sy==by)//ai与角色碰撞检测
{
mySprite.setFrame(2);
mySprite.setTransform(Sprite.TRANS_NONE);
mySprite.life--;
bloodupdate();
if(mySprite.life<1)
mySprite.life=0;
}
}
if(mySprite2.state==2)
{
mySprite2.acet=System.currentTimeMillis();
if(mySprite2.acet-mySprite2.acst>100)
{
mySprite2.state=0;
mySprite2.setFrame(0);
}
}
}
public void drawScreen(Graphics g){
if(mySprite.life == 0){//ai获胜
success = false;
result=false;
pl1b.setFrame(5);
mySprite.setFrame(6);
mySprite2.setFrame(5);
}
if(mySprite2.life == 0){//自己获胜
success = true;
result=true;
pl2b.setFrame(5);
mySprite.setFrame(5);
mySprite2.setFrame(6);
}
lm.setViewWindow(0, 0, width,height);//全屏幕显示
lm.paint(g,0,0);//全屏幕显示画布
//设置精灵的位置
mySprite.setPosition(sx,sy);//起初自己的位置
if((bx-sx)>0)//ai在自己右边
mySprite2.setTransfor