import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
java贪吃蛇实验报告import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class Ì°³ÔÉß extends JFrame implements ActionListener, KeyListener,Runnable {
/**
    *
    */
private static final long serialVersionUID = 1L;
private JMenuBar menuBar;
private JMenu youXiMenu,nanDuMenu,fenShuMenu,guanYuMenu;
private JMenuItem kaiShiYouXi,exitItem,zuoZheItem,fenShuItem;
private JCheckBoxMenuItem cJianDan,cPuTong,cKunNan;
private int length = 6;
private Toolkit toolkit;
private int i,x,y,z,objectX,objectY,object=0,growth=0,time;//bojectX,Y
private int m[]=new int[50];
private int n[]=new int[50];
private Thread she = null;
private int life=0;
private int foods = 0;
private int fenshu=0;
public void run(){ 
  time=500;
  for(i=0;i<=length-1;i++)
  {
  m[i]=90-i*10;n[i]=60;
  }
 
  x=m[0];
  y=n[0];
  z=4;
 
 
  while(she!=null)
  {
 
  check();
  try
  {
    Thread.sleep(time);
  }
  catch(Exception ee)
  {
    System.out.println(z+"");
  }
  }
}
public Ì°³ÔÉß() {
  setVisible(true);
  menuBar = new JMenuBar();
  toolkit=getToolkit();
  youXiMenu = new JMenu("ÓÎÏ·");
  kaiShiYouXi = new JMenuItem("¿ªÊ¼ÓÎÏ·");
  exitItem = new JMenuItem("Í˳öÓÎÏ·");
  nanDuMenu = new JMenu("À§Äѳ̶È");
  cJianDan = new JCheckBoxMenuItem("¼òµ¥");
  cPuTong = new JCheckBoxMenuItem("ÆÕͨ");
  cKunNan = new JCheckBoxMenuItem("À§ÄÑ");
  fenShuMenu = new JMenu("»ý·ÖÅÅÐÐ");
  fenShuItem = new JMenuItem("×î¸ß¼Ç¼");
  guanYuMenu = new JMenu("¹ØÓÚ");
  zuoZheItem = new JMenuItem("¹ØÓÚ×÷Õß");
  guanYuMenu.add(zuoZheItem);
  nanDuMenu.add(cJianDan);
  nanDuMenu.add(cPuTong);
  nanDuMenu.add(cKunNan);
fenShuMenu.add(fenShuItem);
youXiMenu.add(kaiShiYouXi);
  youXiMenu.add(exitItem);
menuBar.add(youXiMenu);
  menuBar.add(nanDuMenu);
  menuBar.add(fenShuMenu);
  menuBar.add(guanYuMenu);
zuoZheItem.addActionListener(this);
  kaiShiYouXi.addActionListener(this);
  exitItem.addActionListener(this);
  addKeyListener(this);
  fenShuItem.addActionListener(this);
  KeyStroke keyOpen = KeyStroke('O',InputEvent.CTRL_DOWN_MASK);
  kaiShiYouXi.setAccelerator(keyOpen);
  KeyStroke keyExit = KeyStroke('X',InputEvent.CTRL_DOWN_MASK);
  exitItem.setAccelerator(keyExit);
  setJMenuBar(menuBar);
  setTitle("Ì°³ÔÉß");
  setResizable(false);
  setBounds(300,200,400,400);
  validate();
  setDefaultCloseOperation(EXIT_ON_CLOSE); 
}
public static void main(String args[]) {
  new Ì°³ÔÉß();
}
public void actionPerformed(ActionEvent e){
  Source()==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();
  }
  }
  Source()==exitItem)
  {
  it(0);
  }
  Source()==zuoZheItem)
  {
  JOptionPane.showMessageDialog(this, "¹Â¶ÀµÄÒ°ÀÇÖÆ×÷"+"\n\n"+"                  "+"QQºÅ:2442701497"+"\n");
  }
  Source()==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;
  }
  paint(); //ÖØ»æ
  }
}
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<length;i++)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。