fish.java
来自「大鱼吃小鱼游戏」· Java 代码 · 共 720 行 · 第 1/2 页
JAVA
720 行
efishsps.removeElementAt(i);
i--;
if(sp1.getFrame()==0||sp1.getFrame()==1||sp1.getFrame()==2)
{
if(efishsp.curspX>sp1.curspX)
sp1.setFrame(7);
else
sp1.setFrame(9);
}
if(sp1.getFrame()==3||sp1.getFrame()==4||sp1.getFrame()==5)
{
if(efishsp.curspX>sp1.curspX)
sp1.setFrame(7);
else
sp1.setFrame(9);
}
}
}
}
}
}
public void drawefishsps(Graphics g)
{
for(int i=0;i<efishsps.size();i++)
{
MySprite efishsp=(MySprite)efishsps.elementAt(i);
if(efishsp.getName()=="shuimu")
{
efishsp.curfre++;
if(efishsp.curfre==efishsp.changefre)
{
if(efishsp.getFrame()==0)
efishsp.setFrame(1);
else if(efishsp.getFrame()==1)
efishsp.setFrame(2);
else if(efishsp.getFrame()==2)
efishsp.setFrame(3);
else if(efishsp.getFrame()==3)
efishsp.setFrame(4);
else if(efishsp.getFrame()==4)
efishsp.setFrame(0);
efishsp.curfre=0;
}
efishsp.curspY-=efishsp.speed;
if(efishsp.way==0)
efishsp.curspX--;
else
efishsp.curspX++;
}
else
{
switch(efishsp.way)
{
case(0)://Canvas.LEFT:
efishsp.curfre++;
if(efishsp.curfre==efishsp.changefre)
{
if(efishsp.getFrame()==1)
efishsp.setFrame(0);
else
efishsp.setFrame(1);
efishsp.curfre=0;
}
//spwidth
if(efishsp.spwidth>sp1.spwidth&&efishsp.curspX<sp1.curspX&&efishsp.curspX>sp1.curspX-60)
{
if(efishsp.curspY<sp1.curspY+10&&efishsp.curspY>sp1.curspY-10)
efishsp.curspX+=efishsp.speed+2;
else
efishsp.curspX+=efishsp.speed;
}
else
efishsp.curspX+=efishsp.speed;
break;
case(1):// Canvas.RIGHT:
efishsp.curfre++;
if(efishsp.curfre==efishsp.changefre)
{
if(efishsp.getFrame()==2)
efishsp.setFrame(3);
else
efishsp.setFrame(2);
efishsp.curfre=0;
}
if(efishsp.spwidth>sp1.spwidth&&efishsp.curspX>sp1.curspX&&efishsp.curspX<sp1.curspX+70)
{
if(efishsp.curspY<sp1.curspY+10&&efishsp.curspY>sp1.curspY-10)
efishsp.curspX-=efishsp.speed+2;
else
efishsp.curspX-=efishsp.speed;
}
else
efishsp.curspX-=efishsp.speed;
break;
}
}
efishsp.draw(g);
if(efishsp.curspX>330||efishsp.curspX<-90||efishsp.curspY>260||efishsp.curspY<20)
{
efishsps.removeElementAt(i);
i--;
}
}
}
//产生鱼
public void createfish()
{
Random random = new Random();
for(int i=0;i<10;i++)
{
int randnum = random.nextInt();
//System.out.println(randnum);
MySprite fish=null;
switch(Math.abs(randnum%5))
{
case(0):
fish=new MySprite("e0",efish[0][Math.abs(randnum%5)],12,10,randnum);
break;
case(1):
fish=new MySprite("e1",efish[1][Math.abs(randnum%5)],17,14,randnum);
break;
case(2):
fish=new MySprite("e2",efish[2][Math.abs(randnum%5)],24,20,randnum);
break;
case(3)://鲨鱼
if(random.nextInt()%2==1)
{
fish=new MySprite("shayu",shayu,90,32,randnum);
fish.speed=2;
}
break;
case(4)://水母
{
if(random.nextInt()%3==1)
{
fish=new MySprite("shuimu",shuimu,28,35,randnum);
fish.curspY=260;
fish.curspX=Math.abs(randnum%240);
fish.speed=2;
}
}
break;
}
if(fish!=null)
efishsps.addElement(fish);
}
}
protected void keyPressed(int keyCode)
{
//System.out.println("pressed:"+keyCode);
if(action2!=-10000)
action1=action2;
if(action1==-10000)
action1=getGameAction(keyCode);
else
action2=getGameAction(keyCode);
if(life<=0)
{
if(getGameAction(keyCode)==Canvas.UP||getGameAction(keyCode)==Canvas.DOWN)
goon=!goon;
if(getGameAction(keyCode)==Canvas.FIRE)
{
if(goon==true)
{
while(efishsps.size()>0)
efishsps.removeElementAt(0);
fishsize=3;
efishkind=5;
bgkind=1;
grow=0;
life=3;
action1=-10000;
action2=-10000;
conti=true;
start=-1;
end=-1;
rate=8000;
//sp1=null,shayusp=null,shuimusp=null,sp4=null;
goon=true;
//fishCanvas(theMidlet);
}
else
this.theMidlet.destroyApp(true);
}
}
}
protected void keyReleased(int keyCode)
{
//System.out.println("released:"+keyCode);
if(getGameAction(keyCode)==action1)//放开第一次按键
action1=-10000;
else if(getGameAction(keyCode)==action2)//放开第二次按键
action2=-10000;
}
}
//鱼精灵
class MySprite
{
private int Frames=0,curFrame=0;
private int spsX,spsY;
public int spwidth,spheight;//一个精灵的宽度,高度
private int spsrows,spscols;//所有精灵总的行数,列数
private int cursprow,curspcol;//当前精灵所在的行号,列号
public int curspX=100,curspY=250;//当前精灵的坐标
private Image spsImage=null;
private boolean visible=true;
public int way,speed,changefre,curfre=0;
public String name=null;
public int eat=0;
public MySprite(String name,Image sourceImage,int width,int height,int randnum)
{
this.name=name;
this.way=Math.abs(randnum%2);
this.speed=Math.abs(randnum%3)+1;
this.changefre=6-this.speed;
this.curspY=Math.abs(randnum%230)+30;
if(this.way==0)
{
if(name=="shayu")
this.curspX=-100+Math.abs(randnum%25);
else
this.curspX=-50+Math.abs(randnum%25);
}
else
this.curspX=290-Math.abs(randnum%30);
if((this.curspY+height)>265)
this.curspY=265-height;
if(this.curspY<25)
this.curspY=25;
this.spsImage=sourceImage;
this.spwidth=width;
this.spheight=height;
if(sourceImage.getWidth()%width==0&&sourceImage.getHeight()%height==0)
{
this.spsrows=sourceImage.getHeight()/height;
this.spscols=sourceImage.getWidth()/width;
this.Frames=spsrows*spscols;
}
else
{
this.Frames=0;
}
}
public void setFrame(int num)
{
if(Frames!=0)
{
this.curFrame=num;
cursprow=num/spscols;
curspcol=num%spscols;
}
else
{
this.curFrame=0;
}
}
public int getFrame()
{
return this.curFrame;
}
public String getName()
{
return this.name;
}
public void setPosition(int x,int y)
{
this.curspX=x;
this.curspY=y;
}
public void draw(Graphics g)
{
if(this.visible)
{
g.setClip(curspX,curspY,spwidth,spheight);
g.drawImage(spsImage,curspX-curspcol*spwidth,curspY-cursprow*spheight,g.TOP|g.LEFT);
}
}
public void draw(Graphics g,int x,int y)
{
setPosition(x,y);
draw(g);
}
public int getX()
{
return this.curspX;
}
public int getY()
{
return this.curspY;
}
public void setVisible(boolean visible)
{
this.visible=visible;
}
public boolean isVisible()
{
return this.visible;
}
public boolean collidesWith(MySprite sp)
{
int minx=sp.curspX-this.spwidth;
int maxx=sp.curspX+sp.spwidth;
int miny=sp.curspY-this.spheight;
int maxy=sp.curspY+sp.spheight;
if(sp.name=="shayu")
{
if(this.curspX>minx&&this.curspX<maxx&&this.curspY>miny+13&&this.curspY<maxy)//身体
return true;
else if(this.curspX>minx+36&&this.curspX<maxx-36&&this.curspY>miny&&this.curspY<maxy-20)//鳍
return true;
else
return false;
}
else
{
if(this.curspX>minx&&this.curspX<maxx&&this.curspY>miny&&this.curspY<maxy)
return true;
else
return false;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?