Java实战开发小游戏——(贪吃蛇)附源码下载

  • 项目:贪吃蛇小游戏

目录

描述:

实现步骤:

效果图

源码下载:地址链接:https://pan.baidu.com/s/1JQESGI4GaZb3p5EqikrUhQ 提取码:gzlp 


  • 描述:

  1. 用java开发,使用jdk1.8,使用eclipse开发
  2. 用到JFrame、Panel、画笔、Timer、数组......
  3. 可以实现移动 自由转向、吃食物、幸运蛋、开始结束提示、计分等
  • 实现步骤:

  • 创建界面

         //创建窗口
        JFrame jf=new JFrame();
        jf.setTitle("贪吃蛇"); //标题
        jf.setBounds(400,0,900,700); //窗口大小位置
        jf.setResizable(false); //是否可拉大
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭方式
        SankePanel panel=new SankePanel(); //创建画布
        jf.add(panel);
        jf.setVisible(true); //是否可显示

 

 


  •  静态画蛇

       //蛇的数据结构
      int[] sankex= new int[850];
      int[] sankey= new int[850];
      int len=4;

String direction = "R"; //向右R 左L 下D 上U

       //画蛇

    //画蛇头
        if(direction.equals("R")){
            g.drawImage(headright.getImage(),sankex[0],sankey[0],25,25, this);
            //headright.paintIcon(this, g, sankex[0], sankey[0]);
        }else if(direction.equals("L")){
            g.drawImage(headleft.getImage(),sankex[0],sankey[0],25,25, this);
            //headleft.paintIcon(this, g, sankex[0], sankey[0]);
        }else if(direction.equals("U")){
            g.drawImage(headup.getImage(),sankex[0],sankey[0],25,25, this);
            //headup.paintIcon(this, g, sankex[0], sankey[0]);
        }else if(direction.equals("D")){
            g.drawImage(headdowm.getImage(),sankex[0],sankey[0],25,25, this);
            //headdowm.paintIcon(this, g, sankex[0], sankey[0]);
        }


        //画蛇身
        for(int i=1;i<len-1;i++){
         if(direction.equals("R")||direction.equals("L")){
            g.drawImage(heng.getImage(),sankex[i],sankey[i],25,25, this);
            //heng.paintIcon(this, g, sankex[i], sankey[i]);
            }else if(direction.equals("D")||direction.equals("U")){
                g.drawImage(shu.getImage(),sankex[i],sankey[i],25,25, this);
                //heng.paintIcon(this, g, sankex[i], sankey[i]
                }
        }


        //画蛇尾
       
        if(sankex[len-1]>=tailx&&sankey[len-1]==taily){
            g.drawImage(tailright.getImage(),sankex[len-1],sankey[len-1],25,25, this);
            //tailright.paintIcon(this, g, sankex[len], sankey[len]);
        }else if(sankex[len-1]<tailx&&sankey[len-1]==taily){
            g.drawImage(tailleft.getImage(),sankex[len-1],sankey[len-1],25,25, this);
            //tailleft.paintIcon(this, g, sankex[len], sankey[len]);
        }else if(sankex[len-1]==tailx&&sankey[len-1]<taily){
            g.drawImage(tailup.getImage(),sankex[len-1],sankey[len-1],25,25, this);
            //tailup.paintIcon(this, g, sankex[len], sankey[len]);
        }else if(sankex[len-1]==tailx&&sankey[len-1]>taily){
            g.drawImage(taildown.getImage(),sankex[len-1],sankey[len-1],25,25, this);
            //taildown.paintIcon(this, g, sankex[len], sankey[len]);
        }

 

 


  •  让蛇平移动

         //移动尾部
            tailx=s

  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值