📄 gamescreen.java
字号:
bul.bul_life=false;
num num=new num(Image_num,npc.npc_curspX,npc.npc_curspY-10,npc.npc_kind);
nums.addElement(num);
npc.npc_life=false;
npc.setFrame(9);
npc.npc_kind=4;
}
}
}
}
npc.npc_curspX-=(currentViewPosX-npc.npc_curViewPosX);
npc.npc_curViewPosX=currentViewPosX;
}
if(npc.npc_bjump)
{
npc.t+=2;
switch(npc.npc_way)
{
case(0):
if(npc.npc_kind==10)
;//npc.npc_curspX--;
else
npc.npc_curspX-=2;
break;
case(1):
if(npc.npc_kind==10)
;//npc.npc_curspX++;
else
npc.npc_curspX+=2;
break;
}
npc.jump();
npc.npc_dead=10;
}
if(npc.npc_curspX>(getWidth()+100)||npc.npc_curspX<-100||npc.npc_curspY>getHeight())//当npc的坐标超出某一范围则删除该npc
npcs.removeElementAt(i);
if(npc.npc_life==false&&npc.npc_kind!=2&&npc.npc_kind!=8)//除了鸭子壳以外的npc如果没有生命并且死了10轮以上则删除
{
if(npc.npc_dead<0)
npcs.removeElementAt(i);
else
npc.npc_dead--;
}
}
}
public void mn_action()//mario和npc之间的运动(踩、碰撞)
{
for(int i=0;i<npcs.size();i++)
{
NpcSprite npc=(NpcSprite)npcs.elementAt(i);
if(npc.npc_kind==10||npc.npc_kind==8)//吃长大蘑菇或者花
{
if((mariosprite.curspX+mariosprite.spwidth)>(npc.npc_curspX+1)&&mariosprite.curspX<(npc.npc_curspX+npc.npc_width-1))
{
if((mariosprite.curspY+mariosprite.spheight)>npc.npc_curspY&&mariosprite.curspY<(npc.npc_curspY+npc.npc_height))
{
if(npc.npc_kind==10)//吃到长大蘑菇
{
//判读向上13个像素的tile能否通过
mariosprite.CanMove(currentViewPosX-getWidth()/2+mariosprite.curspX,currentViewPosY-getHeight()/2+mariosprite.curspY-10);//判断可以向哪些方向移动
mariosprite.ChangeSize(Image_bigmario,mariosprite.curspX,mariosprite.curspY,13,26,1);
mariosprite.state=1;//变成big mario
}
else//吃到花
{
if(mariosprite.state==1)//如果是big mario则变成带子弹的mario
mariosprite.state=2;
else if(mariosprite.state==0)//如果是small mario则长大
{
mariosprite.CanMove(currentViewPosX-getWidth()/2+mariosprite.curspX,currentViewPosY-getHeight()/2+mariosprite.curspY-10);//判断可以向哪些方向移动
mariosprite.ChangeSize(Image_bigmario,mariosprite.curspX,mariosprite.curspY,13,26,1);
mariosprite.state=1;//变成big mario
}
}
npcs.removeElementAt(i);
num num=new num(Image_num,npc.npc_curspX,npc.npc_curspY-10,npc.npc_kind);
nums.addElement(num);
}
}
}
else//mario与怪兽们发生碰撞
{
if(mariosprite.xiajiang==false)//mario在行走过程中与怪兽npc发生碰撞
{
if((mariosprite.curspX+mariosprite.spwidth)>=npc.npc_curspX&&mariosprite.curspX<=(npc.npc_curspX+npc.npc_width))
{
if((mariosprite.curspY+mariosprite.spheight)==(npc.npc_curspY+npc.npc_height))
{
if(npc.npc_kind==2&&npc.npc_life==false)//碰到静止不动的鸭子壳,则将鸭子壳踢开
{
if(mariosprite.curspX>=npc.npc_curspX)
{
npc.npc_way=0;
for(int j=0;j<=7;j++)
{
npc.npc_curspX--;
npc.CanMove((currentViewPosX-getWidth()/2+npc.npc_curspX),(currentViewPosY-getHeight()/2+npc.npc_curspY));
if(npc.left_canmove==false)
{
npc.npc_way=1;
break;
}
}
}
else
{
npc.npc_way=1;
for(int j=0;j<=7;j++)
{
npc.npc_curspX++;
npc.CanMove((currentViewPosX-getWidth()/2+npc.npc_curspX),(currentViewPosY-getHeight()/2+npc.npc_curspY));
if(npc.right_canmove==false)
{
npc.npc_way=0;
break;
}
}
}
npc.npc_life=true;
num num=new num(Image_num,npc.npc_curspX,npc.npc_curspY-10,npc.npc_kind);
nums.addElement(num);
}
else if(npc.npc_life==true)
{
if(mariosprite.state==0)//如果是small mario的话:死亡
{
mariosprite.setFrame(12);
mariosprite.life=false;
}
else if(mariosprite.state==1||mariosprite.state==2)//如果是big mario的话:状态是由大变小过程中
{
mariosprite.ChangeSize(Image_smallmario,mariosprite.curspX,mariosprite.curspY,13,13,0);
mariosprite.state=6;
mariosprite.statetimes=100;
}
}
}
}
}
else//mario踩到怪兽后,怪兽牺牲
{
if(npc.npc_kind==0||npc.npc_kind==1)//踩到mario或duck
{
if((mariosprite.curspX+mariosprite.spwidth)>=npc.npc_curspX&&mariosprite.curspX<=(npc.npc_curspX+npc.npc_width))
{
if((mariosprite.curspY+mariosprite.spheight)>=npc.npc_curspY&&mariosprite.curspY<=npc.npc_curspY)
{
if(npc.npc_life==true)
{
mariosprite.whichjump(2);
mariosprite.t=0;
walkdown=false;
mariosprite.y1=0;
mariosprite.y2=0;
mariosprite.y0=-1;
mariosprite.stopcurspY=-1;
mariosprite.bjump=true;
npc.npc_life=false;
num num=new num(Image_num,npc.npc_curspX,npc.npc_curspY-10,npc.npc_kind);
nums.addElement(num);
if(npc.npc_kind==0)//ghost
npc.setFrame(2);
else if(npc.npc_kind==1)//duck
{
npc.npc_kind=2;
npc.setFrame(8);
}
}
}
}
}
else if(npc.npc_kind==2)//踩到duck壳
{
if((mariosprite.curspX+mariosprite.spwidth)>npc.npc_curspX&&mariosprite.curspX<(npc.npc_curspX+npc.npc_width))
{
if((mariosprite.curspY+mariosprite.spheight)>=(npc.npc_curspY+8)&&mariosprite.curspY<=(npc.npc_curspY+8))
{
int cha;
cha=(mariosprite.curspX+mariosprite.spwidth/2)-(npc.npc_curspX+npc.npc_width/2);
if(npc.npc_life==false)
{
if(cha>=0)
{
npc.npc_way=0;
for(int j=0;j<=7;j++)
{
npc.npc_curspX--;
npc.CanMove((currentViewPosX-getWidth()/2+npc.npc_curspX),(currentViewPosY-getHeight()/2+npc.npc_curspY));
if(npc.left_canmove==false)
{
npc.npc_way=1;
break;
}
}
}
else
{
npc.npc_way=1;
for(int j=0;j<=7;j++)
{
npc.npc_curspX++;
npc.CanMove((currentViewPosX-getWidth()/2+npc.npc_curspX),(currentViewPosY-getHeight()/2+npc.npc_curspY));
if(npc.right_canmove==false)
{
npc.npc_way=0;
break;
}
}
}
npc.npc_life=true;
}
else
{
npc.npc_life=false;
mariosprite.whichjump(2);
mariosprite.t=0;
walkdown=false;
mariosprite.y1=0;
mariosprite.y2=0;
mariosprite.y0=-1;
mariosprite.stopcurspY=-1;
mariosprite.bjump=true;
}
num num=new num(Image_num,npc.npc_curspX,npc.npc_curspY-10,npc.npc_kind);
nums.addElement(num);
}
}
}
}
}
}
}
}
class num
{
public int curX0,curX,curY,num,weishu=0,fentimes=0;
public Image Image_num;
public num(Image img,int x,int y,int num)
{
this.Image_num=img;
this.curX=x;
this.curY=y;
this.num=num;
if(num==0||num==2||num==1)
GameScreen.score+=100;
else if(num==100)
GameScreen.score+=200;
else if(num==10||num==8)
GameScreen.score+=1000;
}
public void draw(Graphics g)
{
if(num==0||num==2||num==1)//毒蘑菇:100分,鸭子壳:100分,鸭子:100分
{
g.setClip(curX,curY-fentimes,4,7);
g.drawImage(Image_num,curX-0*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+5,curY-fentimes,4,7);
g.drawImage(Image_num,curX+5-9*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+10,curY-fentimes,4,7);
g.drawImage(Image_num,curX+10-9*4,curY-fentimes,g.TOP|g.LEFT);
}
else if(num==100)//鸭子:200分
{
g.setClip(curX,curY-fentimes,4,7);
g.drawImage(Image_num,curX-1*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+5,curY-fentimes,4,7);
g.drawImage(Image_num,curX+5-9*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+10,curY-fentimes,4,7);
g.drawImage(Image_num,curX+10-9*4,curY-fentimes,g.TOP|g.LEFT);
}
else if(num==10||num==8)//长大蘑菇:1000分;花:1000分
{
g.setClip(curX,curY-fentimes,4,7);
g.drawImage(Image_num,curX-0*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+5,curY-fentimes,4,7);
g.drawImage(Image_num,curX+5-9*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+10,curY-fentimes,4,7);
g.drawImage(Image_num,curX+10-9*4,curY-fentimes,g.TOP|g.LEFT);
g.setClip(curX+15,curY-fentimes,4,7);
g.drawImage(Image_num,curX+15-9*4,curY-fentimes,g.TOP|g.LEFT);
}
fentimes++;
}
}
class bullet
{
public int curspX,curspY,way,curFrame=0,viewposX,curspX0;
private Image Image_bullet,Image_explode;
private int yy=4;
private int left_u_tile,left_d_tile,right_u_tile,right_d_tile,up_l_tile,up_r_tile,down_l_tile,down_r_tile,width=6,height=6;
public boolean left_canmove,left_u_canmove,left_d_canmove,right_canmove,right_u_canmove,right_d_canmove,up_canmove,up_l_canmove,up_r_canmove,down_canmove,down_l_canmove,down_r_canmove;
public boolean xiajiang=true,bul_life=true;
public int explode_time=30,explode_curFrame=0;
public bullet(Image img1,Image img2,int x,int y,int way,int vpX)
{
this.Image_bullet=img1;
this.Image_explode=img2;
this.curspX0=x+13;
this.curspX=x+13;
this.curspY=y+8;
this.way=way;
this.viewposX=vpX;
}
public void draw(Graphics g,int x)
{
curspX+=viewposX-x;
viewposX=x;
if(bul_life==true)
{
if(curFrame==3)
curFrame=0;
curFrame++;
g.setClip(curspX,curspY,6,6);
g.drawImage(Image_bullet,curspX-curFrame*6,curspY,g.TOP|g.LEFT);
}
else
{
explode_curFrame=2-explode_time/10;
g.setClip(curspX,curspY,10,10);
g.drawImage(Image_explode,curspX-explode_curFrame*10,curspY,g.TOP|g.LEFT);
}
}
public void CanMove(int x,int y)
{
this.left_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
this.left_d_tile=World.tileMap[(y+height-1)/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
this.right_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x+width)/World.TILE_WIDTH];
this.right_d_tile=World.tileMap[(y+height-1)/World.TILE_HEIGHT][(x+width)/World.TILE_WIDTH];
this.up_l_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+1)/World.TILE_WIDTH];
this.up_r_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+width-2)/World.TILE_WIDTH];
this.down_l_tile=World.tileMap[(y+height)/World.TILE_HEIGHT][(x+1)/World.TILE_WIDTH];
this.down_r_tile=World.tileMap[(y+height)/World.TILE_HEIGHT][(x+width-2)/World.TILE_WIDTH];
//假设各个方向均不能移动
this.left_canmove=false;
this.left_u_canmove=false;
this.left_d_canmove=false;
this.right_canmove=false;
this.right_u_canmove=false;
this.right_d_canmove=false;
this.up_canmove=false;
this.up_l_canmove=false;
this.up_r_canmove=false;
this.down_canmove=false;
this.down_l_canmove=false;
this.down_r_canmove=false;
//判断那些方向可以移动
for(int i=0;i<World.CANPASS_TILE.length;i++)
{
if(this.left_u_tile==World.CANPASS_TILE[i])
this.left_u_canmove=true;
if(this.left_d_tile==World.CANPASS_TILE[i])
this.left_d_canmove=true;
if(this.right_u_tile==World.CANPASS_TILE[i])
this.right_u_canmove=true;
if(this.right_d_tile==World.CANPASS_TILE[i])
this.right_d_canmove=true;
if(this.up_l_tile==World.CANPASS_TILE[i])
this.up_l_canmove=true;
if(this.up_r_tile==World.CANPASS_TILE[i])
this.up_r_canmove=true;
if(this.down_l_tile==World.CANPASS_TILE[i])
this.down_l_canmove=true;
if(this.down_r_tile==World.CANPASS_TILE[i])
this.down_r_canmove=true;
}
if(left_u_canmove==true&&left_d_canmove==true)
this.left_canmove=true;
if(right_u_canmove==true&&right_d_canmove==true)
this.right_canmove=true;
if(up_l_canmove==true&&up_r_canmove==true)
this.up_canmove=true;
if(down_l_canmove==true&&down_r_canmove==true)
this.down_canmove=true;
if(this.down_canmove==false)
xiajiang=false;
if(xiajiang==false)
{
if(yy==0)
{
xiajiang=true;
yy=4;
}
yy--;
}
}
public void shot(GameScreen gs)
{
switch(this.way)
{
case(0)://子弹向左移动
curspX-=4;
break;
case(1)://子弹向右移动
curspX+=4;
break;
}
if(xiajiang==true)
curspY+=3;
else
curspY-=3;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -