贪吃蛇小游戏

最近写了一个贪吃蛇的小游戏,感觉难点就在于蛇的位置控制,由于蛇的状态不定,所在你在按下键的时候要判断它前一时刻的的形状,并处理它下一步怎么移动。(今天加入了鼠标事件,起到物体提示作用)

代码:

Code:
  1. import java.awt.BorderLayout;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.awt.Graphics;   
  5. import java.awt.event.*;   
  6. import javax.swing.AbstractAction;   
  7. import javax.swing.Action;   
  8. import javax.swing.JComponent;   
  9. import javax.swing.JFrame;   
  10. import javax.swing.JLabel;   
  11. import javax.swing.JOptionPane;   
  12. import javax.swing.JToolTip;   
  13. import javax.swing.KeyStroke;   
  14.   
  15. public class Snak  extends JComponent implements MouseMotionListener,Runnable{   
  16.     //背景图   
  17.     //ImageIcon image;   
  18.     //提示信息   
  19.     String message="";   
  20.     //墙的横竖的个数,蛇的最大长度   
  21.     static int N=50,M=50;   
  22.     //定义墙壁   
  23.     static int a[][]=new int[N][M];   
  24.     //定义蛇   
  25.     static int longer=5;//蛇的原始长度   
  26.     static int head=longer-1;//蛇头   
  27.     static int x[]=new int[]{4,5,6,7,8,0,0,0,0,0,0,0,0,0,0};   
  28.     static int y[]=new int[]{10,10,10,10,10,0,0,0,0,0,0,0,0,0,0};   
  29.     //定义食物   
  30.     static int food=0;   
  31.     static int eatnum=0;   
  32.     static int x1[]=new int[]{25,7,15,22,19,27,32,34,41,46,12};   
  33.     static int y1[]=new int[]{16,10,39,5,28,32,12,23,14,41,14};   
  34.        
  35.     //用于显示步数和时间   
  36.     String foot="";   
  37.     String time="";   
  38.     //记下步数   
  39.     static int footnum=0;   
  40.     //时间开始与停止标志   
  41.     public boolean flag=true;   
  42.     /**  
  43.      * @贪吃蛇游戏  
  44.      */  
  45.     public Snak(){   
  46.         //image = new ImageIcon(myclass.greedsnak.Snak.class.getResource("1.png"));   
  47.         //处理键盘响应事件即上下左右键,事件的处理在对应的Action中   
  48.         this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");   
  49.         this.getActionMap().put("up",up);   
  50.         this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");   
  51.         this.getActionMap().put("down",down);   
  52.         this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right");   
  53.         this.getActionMap().put("right",right);   
  54.         this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left");   
  55.         this.getActionMap().put("left",left);   
  56.         //增加鼠标事件   
  57.         addMouseMotionListener(this);   
  58.         //启动计时线程   
  59.         Thread thread=new Thread(this);   
  60.         thread.start();   
  61.     }   
  62.     public void init(){   
  63.         //产生墙壁   
  64.         for(int i=0;i<N;i++){   
  65.             for(int j=0;j<M;j++){   
  66.                 a[i][j]=1;   
  67.             }   
  68.         }   
  69.         for(int i=2;i<N-2;i++){   
  70.             for(int j=2;j<M-2;j++){   
  71.                 a[i][j]=0;   
  72.             }   
  73.         }   
  74.         //产生蛇   
  75.         for(int i=0;i<longer;i++){   
  76.             if(i==head)   
  77.                 //蛇头   
  78.                 a[x[i]][y[i]]=3;   
  79.             else  
  80.                 //蛇身   
  81.                 a[x[i]][y[i]]=2;   
  82.         }   
  83.         //产生食物   
  84.         a[x1[food]][y1[food]]=4;   
  85.            
  86.     }   
  87.        
  88.     public void paintComponent(Graphics g){   
  89.         init();   
  90.         //image.paintIcon(this, g, 0, 0);   
  91.         g.setColor(Color.blue);   
  92.         g.setFont(new Font("f",Font.BOLD,15));   
  93.         g.drawString(message, 160250);   
  94.         for(int i=0;i<N;i++){   
  95.             for(int j=0;j<M;j++){   
  96.                 if(a[i][j]==1){   
  97.                     g.setColor(Color.orange);   
  98.                     g.fill3DRect(i*10,j*10,1010,true);   
  99.                 }else if(a[i][j]==2){   
  100.                     g.setColor(Color.pink);   
  101.                     g.fillRect(10*i, 10*j, 1010);   
  102.                 }else if(a[i][j]==3){   
  103.                     g.setColor(Color.pink);   
  104.                     g.fillOval(10*i, 10*j, 1010);   
  105.                 }else if(a[i][j]==4){   
  106.                     g.setColor(Color.red);   
  107.                     g.fill3DRect(i*10,j*10,1010,true);   
  108.                 }   
  109.             }   
  110.         }   
  111.         g.setColor(Color.pink);   
  112.         g.drawString("步数:"100460);   
  113.         g.drawString(foot, 150460);   
  114.         g.drawString("用时:"300460);   
  115.         g.drawString(time, 350460);   
  116.            
  117.     }   
  118.        
  119.     //处理上下左右的键盘事件   
  120.     Action up = new AbstractAction( ) {   
  121.         public void actionPerformed(ActionEvent e) {   
  122.             message="提示:你正在往上走";   
  123.             up();   
  124.             foot=""+(++footnum);   
  125.             repaint();   
  126.             if(food==10) gameOver();   
  127.         }   
  128.     };   
  129.   
  130.     Action down = new AbstractAction( ) {   
  131.         public void actionPerformed(ActionEvent e) {   
  132.             message="提示:你正在往下走";   
  133.             down();   
  134.             foot=""+(++footnum);   
  135.             repaint();   
  136.             if(food==10) gameOver();   
  137.         }   
  138.     };   
  139.   
  140.     Action right = new AbstractAction( ) {   
  141.         public void actionPerformed(ActionEvent e) {   
  142.             message="提示:你正在往右走";   
  143.             right();   
  144.             foot=""+(++footnum);   
  145.             repaint();   
  146.             if(food==10) gameOver();   
  147.         }   
  148.     };   
  149.   
  150.     Action left = new AbstractAction( ) {   
  151.         public void actionPerformed(ActionEvent e) {   
  152.             message="提示:你正在往左走";   
  153.             left();   
  154.             foot=""+(++footnum);   
  155.             repaint();   
  156.             if(food==10) gameOver();   
  157.         }   
  158.     };   
  159.        
  160.     //处理蛇的各个动作   
  161.     public void up(){   
  162.         //处理碰壁   
  163.         if(a[x[head]][y[head]-1]==1) message="提示:你撞到墙壁了哦!";   
  164.         //处理向上碰自己    
  165.         else if(a[x[head]][y[head]-1]==2) touchself("up");   
  166.         //处理碰到食物   
  167.         else if(a[x[head]][y[head]-1]==4) eatFood("up");   
  168.         //处理移动   
  169.         else {   
  170.             moving("up");   
  171.         }      
  172.     }   
  173.     public void down(){   
  174.         //处理碰壁   
  175.         if(a[x[head]][y[head]+1]==1) message="提示:你撞到墙壁了哦!";   
  176.         //处理向上碰自己    
  177.         else if(a[x[head]][y[head]+1]==2) touchself("down");   
  178.         //处理碰到食物   
  179.         else if(a[x[head]][y[head]+1]==4) eatFood("down");   
  180.         //处理移动   
  181.         else {   
  182.             moving("down");   
  183.         }      
  184.     }   
  185.     public void right(){   
  186.         //处理碰壁   
  187.         if(a[x[head]+1][y[head]]==1) message="提示:你撞到墙壁了哦!";   
  188.         //处理向上碰自己    
  189.         else if(a[x[head]+1][y[head]]==2) touchself("right");   
  190.         //处理碰到食物   
  191.         else if(a[x[head]+1][y[head]]==4) eatFood("right");   
  192.         //处理移动   
  193.         else {   
  194.             moving("right");   
  195.         }      
  196.     }   
  197.     public void left(){   
  198.         //处理碰壁   
  199.         if(a[x[head]-1][y[head]]==1) message="提示:你撞到墙壁了哦!";   
  200.         //处理向上碰自己    
  201.         else if(a[x[head]-1][y[head]]==2) touchself("left");   
  202.         //处理碰到食物   
  203.         else if(a[x[head]-1][y[head]]==4) eatFood("left");   
  204.         //处理移动   
  205.         else {   
  206.             moving("left");   
  207.         }      
  208.     }   
  209.     public void touchself(String direction){   
  210.         if("up".equals(direction)){   
  211.             //如果向上从自己身上走过   
  212.             if(x[head]==x[head-1]&&y[head]==(y[head-1]+1)) message="提示:不能从自己身上过!";   
  213.             else message="提示:你不能碰到你自己呢!";   
  214.         }else if("down".equals(direction)){   
  215.             //如果向下从自己身上走过   
  216.             if(x[head]==x[head-1]&&y[head]==(y[head-1]-1)) message="提示:不能从自己身上过!";   
  217.             else message="提示:你不能碰到你自己呢!";   
  218.         }else if("right".equals(direction)){   
  219.             //如果向右从自己身上走过   
  220.             if((x[head]==x[head-1]-1)&&y[head]==y[head-1]) message="提示:不能从自己身上过!";   
  221.             else message="提示:你不能碰到你自己呢!";   
  222.         }else if("left".equals(direction)){   
  223.             //如果向左从自己身上走过   
  224.             if((x[head]==x[head-1]+1)&&y[head]==y[head-1]) message="提示:不能从自己身上过!";   
  225.             else message="提示:你不能碰到你自己呢!";   
  226.         }   
  227.     }   
  228.     public void gameOver(){   
  229.         //停止计时   
  230.         flag=false;   
  231.         JOptionPane.showMessageDialog(Snak.this"恭喜你过了这一关!","过关了哦!",JOptionPane.INFORMATION_MESSAGE);   
  232.     }   
  233.     //处理蛇吃食物   
  234.     public void eatFood(String direction){   
  235.         if("up".equals(direction)){   
  236.         //System.out.println("eatfood-up");   
  237.         //蛇的长度加1   
  238.         longer+=1;   
  239.         //加入这节长度的坐标,事实就把食物的位置改为增加一节的位置   
  240.         x[longer-1]=x[head];   
  241.         y[longer-1]=y[head]-1;   
  242.         //把蛇头换到第一节的位置   
  243.         head=longer-1;   
  244.         //显示第二个食物   
  245.         food+=1;   
  246.         message="提示:你成功吃下"+(++eatnum)+"个食物哦!";   
  247.         //向上移动   
  248.         moving("up");   
  249.         }else if("down".equals(direction)){   
  250.             //System.out.println("eatfood-down");   
  251.             //蛇的长度加1   
  252.             longer+=1;   
  253.             //加入这节长度的坐标,事实就把食物的位置改为增加一节的位置   
  254.             x[longer-1]=x[head];   
  255.             y[longer-1]=y[head]+1;   
  256.             //把蛇头换到第一节的位置   
  257.             head=longer-1;   
  258.             //显示第二个食物   
  259.             food+=1;   
  260.             message="提示:你成功吃下"+(++eatnum)+"个食物哦!";   
  261.             //向下移动   
  262.             moving("down");   
  263.         }else if("right".equals(direction)){   
  264.             //System.out.println("eatfood-right");   
  265.             //蛇的长度加1   
  266.             longer+=1;   
  267.             //加入这节长度的坐标,事实就把食物的位置改为增加一节的位置   
  268.             x[longer-1]=x[head]+1;   
  269.             y[longer-1]=y[head];   
  270.             //把蛇头换到第一节的位置   
  271.             head=longer-1;   
  272.             //显示第二个食物   
  273.             food+=1;   
  274.             message="提示:你成功吃下"+(++eatnum)+"个食物哦!";   
  275.             //向上移动   
  276.             moving("right");   
  277.         }else if("left".equals(direction)){   
  278.             //System.out.println("eatfood-left");   
  279.             //蛇的长度加1   
  280.             longer+=1;   
  281.             //加入这节长度的坐标,事实就把食物的位置改为增加一节的位置   
  282.             x[longer-1]=x[head]-1;   
  283.             y[longer-1]=y[head];   
  284.             //把蛇头换到第一节的位置   
  285.             head=longer-1;   
  286.             //显示第二个食物   
  287.             food+=1;   
  288.             message="提示:你成功吃下"+(++eatnum)+"个食物哦!";   
  289.             //向上移动   
  290.             moving("left");   
  291.         }   
  292.            
  293.     }   
  294.     //处理蛇的动作   
  295.     public void moving(String direction){   
  296.         if("up".equals(direction)){   
  297.             //从尾开始移动   
  298.             corewalk();   
  299.             //蛇头向上移一步   
  300.             y[longer-1]-=1;   
  301.   
  302.         }else if("down".equals(direction)){   
  303.             //从尾开始移动   
  304.             corewalk();   
  305.             //蛇头向下移一步   
  306.             y[longer-1]+=1;   
  307.         }else if("right".equals(direction)){   
  308.             //从尾开始移动   
  309.             corewalk();   
  310.             //蛇头向右移一步   
  311.             x[longer-1]+=1;   
  312.         }else if("left".equals(direction)){   
  313.             //从尾开始移动   
  314.             corewalk();   
  315.             //蛇头向左移一步   
  316.             x[longer-1]-=1;   
  317.         }   
  318.     }   
  319.   
  320.     //处理蛇移动的核心方法   
  321.     public void corewalk(){   
  322.         //从尾开始移动   
  323.         for(int i=1;i<=longer-1;i++){   
  324.             //如果后一节在前一节的左边   
  325.             if((x[i-1]==x[i]-1)&&y[i-1]==y[i])   
  326.                 x[i-1]+=1;     
  327.             //如果后一节在前一节的右边   
  328.             else if((x[i-1]==x[i]+1)&&y[i-1]==y[i])   
  329.                 x[i-1]-=1;   
  330.             //如果后一节在前一节的下边   
  331.             else if(x[i-1]==x[i]&&(y[i-1]==y[i]+1))   
  332.                 y[i-1]-=1;   
  333.             //如果后一节在前一节的上边   
  334.             else if(x[i-1]==x[i]&&(y[i-1]==y[i]-1))   
  335.                 y[i-1]+=1;   
  336.                
  337.         }   
  338.     }   
  339.        
  340.     @Override  
  341.     public void mouseDragged(MouseEvent e) {   
  342.            
  343.     }   
  344.     @Override  
  345.     public void mouseMoved(MouseEvent e) {   
  346.         int xx=e.getX();   
  347.         int yy=e.getY();   
  348.         //用来显示 Component的"提示"   
  349.         if(a[xx/10][yy/10]==1)   
  350.         //提示这是墙壁   
  351.         this.setToolTipText("这是墙壁");   
  352.         else if(a[xx/10][yy/10]==2)   
  353.         //提示这是蛇身   
  354.         this.setToolTipText("这是蛇身");   
  355.         else if(a[xx/10][yy/10]==3)   
  356.         //提示这是蛇头   
  357.         this.setToolTipText("这是蛇头");   
  358.         else if(a[xx/10][yy/10]==4)   
  359.         //提示这是食物   
  360.         this.setToolTipText("这是食物");   
  361.         else  
  362.         //提示这是空地   
  363.         this.setToolTipText("这是空地");   
  364.     }   
  365.   
  366.     @Override  
  367.     public void run() {   
  368.         long start=System.currentTimeMillis();   
  369.         while(flag){   
  370.             try {   
  371.                 Thread.sleep(1000);   
  372.                 long recond=(System.currentTimeMillis()-start)/1000;   
  373.                    
  374.                 if(recond>60){   
  375.                     time=recond/60+"分"+recond%60+"秒";   
  376.                 }else{   
  377.                     time=recond+"秒";   
  378.                 }   
  379.                 repaint();   
  380.             } catch (InterruptedException e) {   
  381.                 e.printStackTrace();   
  382.             }   
  383.                
  384.         }   
  385.            
  386.     }   
  387.        
  388.     public static void main(String[] args) {   
  389.         JFrame pn=new JFrame("界面");   
  390.         JLabel label=new JLabel("贪吃蛇游戏",JLabel.CENTER);   
  391.         pn.setSize(506550);   
  392.         pn.setResizable(false);   
  393.         pn.setDefaultCloseOperation(3);   
  394.         pn.getContentPane().add(label,BorderLayout.NORTH);   
  395.         pn.getContentPane().add(new Snak(),BorderLayout.CENTER);   
  396.         pn.setVisible(true);   
  397.   
  398.     }   
  399. }   

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 38
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值