整个游戏由3个类构成。Bird类,Pipe类,stage类
第一步:首先写一个Bird类
//鸟类
public class Bird {
private int flyHeight;//飞行高度
private int xpos;//距离y轴(窗口左边缘)的位置,
public static int Up=1;//向上飞
public static int Down=-1;//向下飞
public Bird()
{
flyHeight=200;
xpos=30;
}
public void fly(int direction)
{
if(direction==Bird.Up)
flyHeight-=20;//每次向上飞20m
if(direction==Bird.Down)
flyHeight+=20;//每次向下飞20m
}
public int getFlyHeight()//获得当前飞行高度
{
return flyHeight;
}
public int getXpos()//获得当前鸟的水平位置
{
return xpos;
}
public Boolean hit(Pipe pipe[])//检测是否碰到管道。只有在鸟经过管道的过程才有可能相撞
{
for(int i=0;i<pipe.length;i++)//遍历管道进行检测,是否相撞
{
if(getXpos()+20>=pipe[i].getXpos()&&getXpos()<=pipe[i].getXpos()+20)//鸟经过管道
if(flyHeight<pipe[i].getUpHeight()||flyHeight>pipe[i].getDownHeight())//鸟与管道相撞
return true;
}
return false;
}
}