精易论坛

标题: 简单网页小游戏 贪吃滑稽 [打印本页]

作者: 零碎不堪    时间: 2021-1-22 09:36
标题: 简单网页小游戏 贪吃滑稽
开局会有一个从左向右移动的 称为玩家,通过键盘的上下左右可以控制方向,每3秒会出现一个新的称为食物,当两个相遇的时候玩家会吃掉食物变大自己,如果碰到墙壁会重新开始恢复初始大小,很简单的一个小游戏,代码也比较少 对于新手来说有研究价值,通过超文本浏览框也可以嵌入到易语言中使用


有兴趣的可以继续完善




简单的介绍一下项目结构


img/0.jpg中是表情的图片  大家可以替换为自己喜欢的头像 名字和格式相同可以直接替换 否则需要改一下代码的引用

js/jquery.min.js   常用的js类库
index.html   主要页面和代码都在这

主要js代码如下  初始值大家可以根据需要更改

  1. var interval = null//定时器
  2.         var direction = 4;//上下左右/1234
  3.         var maxWidth = 500;//墙壁宽度
  4.         var maxHeight = 500;//墙壁高度
  5.         var width = 30;//初始宽度
  6.         var height = 30;//初始高度
  7.         var user = {width: 30,height: 30};//当前大小
  8.         var point = {left: 20,top: 20};//当前位置
  9.         var foodList = []//食物集合
  10.         var c = 0;//食物数量计数
  11.         $(function(){
  12.                 $(".top").css(user);//设置初始大小
  13.                 $(".top").css(point);//设置初始位置
  14.                 interval = setInterval(function(){
  15.                         controlDirection();
  16.                 },20);
  17.                 //3秒新增1个食物
  18.                 var interval2 = setInterval(function(){
  19.                         if(foodList.length < 10){//超过10个食物暂停
  20.                                 //取随机位置
  21.                                 var left = Math.floor(Math.random()*500);
  22.                                 if(left > 470){
  23.                                         left = 470;
  24.                                 }
  25.                                 var top = Math.floor(Math.random()*500);
  26.                                 if(top > 470){
  27.                                         top = 470;
  28.                                 }
  29.                                 c++;
  30.                                 $(".wall").append('<img src="img/0.jpg" class="top'+c+' food" style="left: '+left+'px;top: '+top+'px;" />');
  31.                                 foodList.push({left:left,top:top,n: c});
  32.                         }
  33.                 },500)
  34.         })
  35.        
  36.         //控制方向
  37.         function controlDirection(){
  38.                 if(direction == 1){//上
  39.                         point.top--;
  40.                         if(point.top >= maxHeight || point.top < 1){
  41.                                 gameOver();
  42.                         }
  43.                 }else if(direction == 2){//下
  44.                         point.top++;
  45.                         if(point.top >= maxHeight - 30 || point.top < 1){
  46.                                 gameOver();
  47.                         }
  48.                 }else if(direction == 3){//左
  49.                         point.left--;
  50.                         if(point.left >= maxHeight || point.left < 1){
  51.                                 gameOver();
  52.                         }
  53.                 }else if(direction == 4){//右
  54.                         point.left++;
  55.                         if(point.left >= maxHeight - 30 || point.left < 1){
  56.                                 gameOver();
  57.                         }
  58.                 }
  59.                 //判断食物位置
  60.                 for(var i=0;i<foodList.length;i++){
  61.                         var topn = 31;//自身范围
  62.                         if(foodList[i].top > point.top){
  63.                                 topn = foodList[i].top - point.top;
  64.                         }else{
  65.                                 topn = point.top - foodList[i].top;
  66.                         }
  67.                         var leftn = 31;//自身范围
  68.                         if(foodList[i].left > point.left){
  69.                                 leftn = foodList[i].left - point.left;
  70.                         }else{
  71.                                 leftn = point.left - foodList[i].left;
  72.                         }
  73.                         if(topn < 30 && leftn < 30){//相遇后吃掉食物
  74.                                 console.log("相遇");
  75.                                 $(".top" + foodList[i].n).remove();
  76.                                 foodList.splice(i, 1);
  77.                                 user.width += 5;
  78.                                 user.height += 5;
  79.                                 $(".top0").css(user)
  80.                         }
  81.                 }
  82.                 $(".top0").css(point);//更改位置
  83.         }
  84.        
  85.         function gameOver(){
  86.                 console.log("碰到墙壁");
  87.                 /* clearInterval(interval);//碰到墙以后结束游戏
  88.                 alert("Game over"); */
  89.                 //碰到墙以后重新开始
  90.                 user = {width: 30,height: 30};
  91.                 $(".top0").css(user);//回复初始大小
  92.                 direction = 4;//往右走
  93.                 point = {left: 20,top: 20};//当前位置=初始位置
  94.                
  95.         }
  96.        
  97.         //监控按键
  98.         $(document).keydown(function(event){
  99.                 console.log(event.keyCode);
  100.                 if(event.keyCode == 38){
  101.                         direction = 1;
  102.                 }else if(event.keyCode == 40){
  103.                         direction = 2;
  104.                 }else if(event.keyCode == 37){
  105.                         direction = 3;
  106.                 }else if(event.keyCode == 39){
  107.                         direction = 4;
  108.                 }
  109.         });
复制代码

源码在此

Snake.zip (143.66 KB, 下载次数: 15)




作者: yinghao2005    时间: 2021-1-22 10:39
沙发支持一下
作者: afd    时间: 2021-1-22 11:25
有bug  吃不了
作者: 2161045897    时间: 2021-1-22 11:55
感谢分享~~~~~~~~~~
作者: 零碎不堪    时间: 2021-1-22 13:53
大鲨鱼 发表于 2021-1-22 11:25
有bug  吃不了

虽然变大了但是默认的吃掉范围还是30  所以得用中间去吃
作者: 1031308775    时间: 2021-1-22 15:03
滑稽
作者: 外星星人    时间: 2021-1-22 15:43
感谢分享。
作者: sinewtec    时间: 2021-1-22 19:13
支持一下~~~~~~
作者: 点点丶滴滴    时间: 2021-1-23 01:55
瞅瞅看
作者: 微信的木头人    时间: 2021-1-29 17:06
没有易语言的?用来消遣一下时间
作者: fengshou    时间: 2021-2-4 22:32
滑稽越吃越大,嘿嘿
作者: ghost12    时间: 2022-5-9 23:00
感谢发布原创作品,一定好好学习,天天向上




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4