- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- import javax.swing.*;
- public class J_Snake extends JFrame implements ActionListener
- {
- int snakehead = 403, snaketail = 400;
- JButton b[] = new JButton[900];
- int delay = 500;
- int newstep[] = new int[1000];
- int oldstep = 1;
- int count1 = 0,count2 = 0;
- Timer m_Timer = new Timer(delay,this);
- Container c = getContentPane();
- Point[] p = new Point[1000];
- Point p1; int t ;
- Point newp[] = new Point[1000];
- int realstep[] = new int[1000];
- public J_Snake()
- {
- super("贪吃蛇");
- newstep[0] = oldstep = 1;
- JMenuBar mBar = new JMenuBar();
- setJMenuBar(mBar);
- JMenu m[] = {new JMenu("菜单"),new JMenu("难度")};
- JMenuItem mI[][] = {{new JMenuItem("开始"),new JMenuItem("重新开始"),new JMenuItem("退出")},{new JMenuItem("简单"),new JMenuItem("一般"),new JMenuItem("困难")}};
- for(int i = 0;i < 2;i ++)
- {
- mBar.add(m[i]);
- for(int j = 0;j < 3;j ++)
- {
- m[i].add(mI[i][j]);
- mI[i][j].addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- if(e.getActionCommand().equalsIgnoreCase("开始"))
- m_Timer.start();
- if(e.getActionCommand().equalsIgnoreCase("退出"))
- System.exit(0);
- if(e.getActionCommand().equalsIgnoreCase("简单"))
- m_Timer.setDelay(500);
- if(e.getActionCommand().equalsIgnoreCase("一般"))
- m_Timer.setDelay(300);
- if(e.getActionCommand().equalsIgnoreCase("困难"))
- m_Timer.setDelay(200);
- if(e.getActionCommand().equalsIgnoreCase("重新开始"))
- {
- dispose();
- J_Snake a = new J_Snake();
- a.main(null);
- }
- }
- });
- }
- }
- c.setLayout(new GridLayout(30,30));
- c.setFocusable(true);
- for(int i = 0;i < 900;i ++)
- {
- b[i] = new JButton("");
- b[i].setVisible(false);
- c.add(b[i]);
- }
- for(int i = snakehead;i >=snaketail ;i--)//设置初始模块
- {
- b[i].setVisible(true);
- b[i].setText("1");
- b[i].setFont(new Font("宋体",Font.PLAIN,0));
- }
- b[409].setVisible(true);
- c.addKeyListener(new KeyAdapter() {
- boolean run = true;
- int[] keyrecord = new int[1000];
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() != 32)
- {
- count1++;
- if (e.getKeyCode() == 37)// left
- {
- if(keyrecord[count1-1]==37)//重复按两次左无效
- count1--;
- if(keyrecord[count1-1]==39)//按完右接着按左无效
- count1--;
- //前一个既不是向左,也不是向右,则记录下蛇头位置及step
- if(keyrecord[count1]!=37&&keyrecord[count1]!=39)
- {
- newstep[count1] = -1;
- p[count1] = b[snakehead].getLocation();
- }
- keyrecord[count1] = 37;
- }
- if (e.getKeyCode() == 38)// up
- {
- if(keyrecord[count1-1]==38)//重复按两次上无效
- count1--;
- if(keyrecord[count1-1]==40)//按完下接着安上无效
- count1--;
- if(keyrecord[count1]!=38&&keyrecord[count1]!=40)
- {
- newstep[count1] = -30;
- p[count1] = b[snakehead].getLocation();
- }
- keyrecord[count1] = 38;
- }
- if (e.getKeyCode() == 39)// right
- {
- if(keyrecord[count1-1]==39)//重复按两次右无效
- count1--;
- if(keyrecord[count1-1]==37)//按完左接着按右无效
- count1--;
- if(keyrecord[count1]!=39&&keyrecord[count1]!=37)
- {
- newstep[count1] = 1;
- p[count1] = b[snakehead].getLocation();
- }
- keyrecord[count1] = 39;
- }
- if (e.getKeyCode() == 40)// down
- {
- if(keyrecord[count1-1]==40)//重复按两次下无效
- count1--;
- if(keyrecord[count1-1]==38)//按完上接着按下无效
- count1--;
- if(keyrecord[count1]!=40&&keyrecord[count1]!=38)
- {
- newstep[count1] = 30;
- p[count1] = b[snakehead].getLocation();
- }
- keyrecord[count1] = 40;
- }
- checkSimilar();
- }
- else//设置空格间为暂停开始键
- {
- if(run)
- {
- m_Timer.stop();
- run = false;
- }
- else
- {
- m_Timer.start();
- run = true;
- }
- }
- }
- });
- }
- public void actionPerformed(ActionEvent e)
- {
- move();
- }
- public void move()
- {
- if(!crash())
- {
- if (checkfood())
- {
- b[snakehead += newstep[count1]].setVisible(true);
- b[snakehead].setText("1");
- b[snakehead].setFont(new Font("宋体",Font.PLAIN,0));
- b[randomfood()].setVisible(true);
- }
- else
- {
- b[snakehead += newstep[count1]].setVisible(true);
- b[snakehead].setText("1");
- b[snakehead].setFont(new Font("宋体",Font.PLAIN,0));
- if(!b[snaketail].getLocation().equals(newp[count2]))
- {
- b[snaketail].setVisible(false);
- b[snaketail].setText("");
- b[snaketail].setFont(new Font("宋体",Font.PLAIN,0));
- snaketail += oldstep;
- }
- else
- {
- b[snaketail].setVisible(false);
- b[snaketail].setText("");
- b[snaketail].setFont(new Font("宋体",Font.PLAIN,0));
- count2++;
- oldstep = realstep[count2-1];
- snaketail += oldstep;
- }
- }
- }
- else
- {
- m_Timer.stop();
- JOptionPane.showMessageDialog(this, "sorry!");
- }
- }
- /*去掉蛇头相同的位置,只保留一个位置,并且只保留与相同位置对应的最后一个移动步数
- * 例如:蛇头拐弯位置p:p[1]=(123,234),p[2]=(123,234)p[3]=(326,521)p[4]=(256,245)
- * 对应移动步数newstep:newstep[1]=1,newstep[2]=2,newstep[3]=3,newstep[4]=4
- * checkSimilar后:
- * 蛇头拐弯位置newp:newp[1]=(123,234),newp[2]=(326,521),newp[3]=(256,245)
- * 移动步数realstep:realstep[1]=2,realstep[1]=3,realstep[1]=4
- * */
- public void checkSimilar()
- {
- int i = 1;
- t = 0;
- if(count1==1)
- {
- newp[t] = p[1];
- realstep[t] = newstep[1];
- t++;
- }
- if(count1==2)
- {
- if(p[i].equals( p[i + 1]))
- {
- newp[t] = p[i];
- realstep[t] = newstep[i+1];
- t++;
- }
- else
- {
- newp[t] = p[i];
- realstep[t] = newstep[i];
- t++;
- newp[t] = p[i+1];
- realstep[t] = newstep[i+1];
- t++;
- }
- }
- else
- {
- for ( i = 1; i < count1; i++)
- {
- if (p[i].equals( p[i + 1]))
- {
- for (int j = i+2; j <= count1 ; j++)
- if (!p[i+1].equals(p[j])||j==count1)
- {
- newp[t] = p[i+1];
- if(j==count1&&p[j].equals(p[j-1]))
- realstep[t] = newstep[j];
- else
- realstep[t] = newstep[j-1];
- i = j-1;
- t++;
- break;
- }
- }
- else
- {
- newp[t] = p[i];
- realstep[t] = newstep[i];
- t++;
- }
- }
- newp[t] = p[count1];
- realstep[t] = newstep[count1];
- t++;
- if(newp[t-2].equals( newp[t-1]))
- t--;
- }
- }
- public boolean checkfood()
- {
- if(b[snakehead+newstep[count1]].getText().isEmpty()&&b[snakehead+newstep[count1]].isVisible())
- return true;
- return false;
- }
- public int randomfood()
- {
- int num ,temp;
- Random rc = new Random();
- do
- {
- temp = rc.nextInt(899);
- num = temp;
- }while(!b[num].getText().isEmpty());
- return num;
- }
- public boolean crash()
- {
- int leftborder[] = new int[30];
- int rightborder[] = new int[30];
- for(int i = 0;i < 30;i ++)
- {
- leftborder[i] = i*30;
- rightborder[i] = 29 + i*30;
- }
- for(int i = 1;i < 30;i ++)
- {
- if((snakehead+newstep[count1])==rightborder[i-1]&&snakehead==leftborder[i])
- return true;
- if((snakehead+newstep[count1])==leftborder[i]&&snakehead==rightborder[i-1])
- return true;
- //if(snakehead==899)
- // return true;
- }
- if((snakehead+newstep[count1])<0||(snakehead+newstep[count1])>899)
- return true;
- if(!b[snakehead+newstep[count1]].getText().isEmpty())
- return true;
- return false;
- }
- public static void main(String args[])
- {
- J_Snake app = new J_Snake();
- app.setDefaultCloseOperation(EXIT_ON_CLOSE);
- app.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/4, Toolkit.getDefaultToolkit().getScreenSize().height/7);
- app.setSize(680,580);
- app.setVisible(true);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0501201511478.html
来源: http://www.codesnippet.cn/detail/0501201511478.html