*********************
setJMenuBar(menuBar);
setTitle("贪吃蛇");
setResizable(false);
setBounds(300,200,400,400);
validate();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new TanChiShe();
}
//******************菜单监听******************************
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==kaiShiYouXi)
{
length = 6;
life = 0;
foods = 0;
if(she==null)
{
she=new Thread(this);
she.start();
}
else if(she!=null)
{
she=null;
she= new Thread(this);
she.start();
}
}
if(e.getSource()==exitItem)
{
System.exit(0);
}
if(e.getSource()==zuoZheItem)
{
JOptionPane.showMessageDialog(this, "北京java编程爱好者制作"+"\n\n"+" "+"QQ号:860695120"+"\n");
}
if(e.getSource()==fenShuItem)
{
JOptionPane.showMessageDialog(this,"最高记录为"+fenshu+"");
}
}
//**************检查一下*******************************
public void check()
{
isDead();
if(she!=null)
{
if(growth==0)
{
reform(); //得到食物
}
else
{
upgrowth(); //生成食物
}
if(x==objectX&;&;y==objectY)
{
object=0;
growth=1;
toolkit.beep();
}
//****************产生食物坐标**********************
if(object==0)
{
object=1;
objectX=(int)Math.floor(Math.random()*39)*10;
objectY=(int)Math.floor(Math.random()*29)*10+50;
}
this.repaint(); //重绘
}
}
void isDead()
{
//判断游戏是否结束的方法
if(z==4)
{
x=x+10;
}
else if(z==3)
{
x=x-10;
}
else if(z==2)
{
y=y+10;
}
else if(z==1)
{
y=y-10;
}
if(x<0||x>390||y<50||y>390)
{
she=null;
}
for(i=1;i
{
if(m[i]==x&;&;n[i]==y)
{
she=null;
}
}
}
public void upgrowth()
{
//当蛇吃到东西时的方法
if(length<50)
{
length++;
}
growth--;
time=time-10;
reform();
life+=100;
if(fenshu {
fenshu = life;
}
foods++;
}
public void reform()
{
for(i=length-1;i>0;i--)
{
m[i]=m[i-1];
n[i]=n[i-1];
}
if(z==4)
{
m[0]=m[0]+10;
}
if(z==3)
{
m[0]=m[0]-10;
}
if(z==2)
{
n[0]=n[0]+10;
}
if(z==1)
{
n[0]=n[0]-10;
}
}
public void keyPressed(KeyEvent e)
{
if(she!=null)
{