📄 monster.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Monster
{
int x,y;
int dx,dy;
int bulletspeed;
int bulletclock,bulletclock2;
boolean alive;
public Sprite sprite;//内置的Sprite
private int width,height;//屏幕的高和宽
MyGameCanvas parent;
int life;//生命
int bexplosion;
Image img;
int mission;
int tempclock;
public Monster(int width,int height,MyGameCanvas p)
{
try
{
img = Image.createImage("/Monster1.png");
}
catch (Exception ex)
{}
sprite=new Sprite(img,60,60);
this.width=width;
this.height=height;
parent=p;
alive=false;
mission=1;
}
public void initObject(int gate)//初始化某一个关的老怪
{
mission=gate;
switch(gate)
{
case 1://Monster1 24桢
x=width/2-30;
y=-60;
dx=2;
dy=2;
alive=true;
sprite.setFrame(0);
bulletspeed=30;
bulletclock=0;
life=100;
break;
case 2:
try
{
img = Image.createImage("/Monster2.png");
}
catch (Exception ex)
{}
sprite.setImage(img,66,66);
x=width/2-33;
y=-66;
dx=2;
dy=2;
alive=true;
sprite.setFrame(0);
bulletclock=0;
bulletclock2=0;
life=150;
tempclock=100;
break;
case 3:break;
case 4:break;
}
}
public void update()
{
int i,j;
int ObjectXCenter,ObjectYCenter;
int planeXCenter=parent.plane.getX()+12;
int planeYCenter=parent.plane.getY()+12;
if (alive)
{
if (mission==1)
{
if (y<10)
{
y+=dy;
}
else
{
if (x<-30 || x>width-30)
{
dx=-dx;
}
x+=dx;
}
bulletclock++;
if (bulletclock==bulletspeed)//可以发子弹了
{
parent.bullets.initBullet(x,y+60,-4,3,false);
parent.bullets.initBullet(x+30,y+60,0,3,false);
parent.bullets.initBullet(x+60,y+60,4,3,false);
bulletclock=0;
}
}
else if (mission==2)
{
if (y<10)
{
y+=dy;
}
else
{
if ( x<0 )
{
if (tempclock>0)
{
bulletclock++;
if (bulletclock==5)//可以发子弹了
{
parent.bullets.initBullet(x+33,y+66,0,6,false);
parent.bullets.initBullet(x+33,y+54,0,6,false);
parent.bullets.initBullet(x+33,y+42,0,6,false);
bulletclock=0;
}
tempclock--;
}
else
{
x+=dx;
}
}
else if (x>width-66)
{
if (tempclock>0)
{
bulletclock++;
if (bulletclock==5)//可以发子弹了
{
parent.bullets.initBullet(x+33,y+66,0,6,false);
parent.bullets.initBullet(x+33,y+54,0,6,false);
parent.bullets.initBullet(x+33,y+42,0,6,false);
bulletclock=0;
}
tempclock--;
}
else
{
x+=dx;
}
}
else
{
x+=dx;
tempclock=50;
bulletclock2++;
if (bulletclock2==15)//可以发子弹了
{
parent.bullets.initBullet(x,y+66,-4,3,false);
parent.bullets.initBullet(x+33,y+66,0,3,false);
parent.bullets.initBullet(x+66,y+66,4,3,false);
bulletclock2=0;
}
if (x>=width/2-40 && x<=width/2-25)
{
parent.enemy.initObject(width/2-2,66,0,4,5,-1);
}
}
if (x<0 || x>width-66)
{
dx=-dx;
}
}
}
//和飞机的冲突检测
ObjectXCenter=x+30;
ObjectYCenter=y+30;
if(Math.abs(planeXCenter-ObjectXCenter) < 42)
{
if (Math.abs(planeYCenter - ObjectYCenter )< 42)
{
parent.gameover = true;
}
}
//和飞机子弹的冲突检测
for(j=0;j<=parent.bullets.mycurrentnum;j++)
{
if (parent.bullets.mybullets[j][4]!=0)
{
if(Math.abs(parent.bullets.mybullets[j][0]+3-ObjectXCenter) < 30)
{
if (Math.abs(parent.bullets.mybullets[j][1]+3 - ObjectYCenter )< 30)
{
parent.bullets.mybullets[j][4]=0;
parent.explosions.initObject(parent.bullets.mybullets[j][0]-12,parent.bullets.mybullets[j][1]-12,0);
life--;
if (life<=0)
{
alive=false;
parent.score+=1000;
bexplosion=100;
}
}
}
}
}
}
else
{
//被击毁,爆炸效果
if (bexplosion>0)
{
bexplosion--;
switch (bexplosion)
{
case 99:
parent.explosions.initObject(x,y,0);
parent.explosions.initObject(x+30,y,0);
parent.explosions.initObject(x+20,y+20,0);
parent.explosions.initObject(x+30,y+15,0);
parent.explosions.initObject(x+40,y+40,0);
break;
case 94:
parent.explosions.initObject(x+15,y,0);
parent.explosions.initObject(x,y+20,0);
parent.explosions.initObject(x+30,y+30,0);
parent.explosions.initObject(x+40,y+15,0);
parent.explosions.initObject(x+40,y+30,0);
break;
case 89:
parent.explosions.initObject(x+15,y+15,0);
parent.explosions.initObject(x+30,y+15,0);
parent.explosions.initObject(x+20,y+20,0);
parent.explosions.initObject(x+30,y,0);
parent.explosions.initObject(x+40,y+20,0);
break;
case 84:
parent.explosions.initObject(x,y,0);
parent.explosions.initObject(x+30,y,0);
parent.explosions.initObject(x+20,y+20,0);
parent.explosions.initObject(x+30,y+15,0);
parent.explosions.initObject(x+40,y+10,0);
break;
case 79:
parent.explosions.initObject(x+15,y,0);
parent.explosions.initObject(x,y+20,0);
parent.explosions.initObject(x+30,y+30,0);
parent.explosions.initObject(x+40,y+15,0);
parent.explosions.initObject(x+40,y+30,0);
break;
case 1:
parent.missionfinished=100;
break;
}
}
}
}
public void paint(Graphics g)
{
if (alive)
{
sprite.setPosition(x,y);
sprite.nextFrame();
sprite.paint(g);
}
if (bexplosion>68)
{
sprite.paint(g);
}
}
public void reset()
{
alive=false;
bexplosion=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -