原生JS 实现小游戏 贪吃蛇

(1) 游戏场景搭建

   给贪吃蛇一个场地布局
   
   代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
   
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      html,
      body {
   
        background: aliceblue;
      }

      #scene {
   
        position: relative;
        margin: 50px auto;
        background-color: rgba(144, 196, 118, 0.362);
        width: 1000px;
        height: 500px;
        border: solid 1px #333;
        box-shadow: #000 0 0 3px;
      }
    </style>
  </head>
  <body>
    <div id="scene"></div>
  </body>
</html>

效果如下:
在这里插入图片描述

(2) 获取标签、准备数据

   ① 创建贪吃蛇标签和样式
   
   代码如下:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
   
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      html,
      body {
   
        background: aliceblue;
      }

      #scene {
   
        position: relative;
        margin: 50px auto;
        background-color: rgba(144, 196, 118, 0.362);
        width: 1000px;
        height: 500px;
        border: solid 1px #333;
        box-shadow: #000 0 0 3px;
      }
      #head {
   
        width: 50px;
        height: 50px;
        border-radius: 30px;
        /* background: orangered; */
        position: absolute;
        left: 150px;
        top: 0;
      }
      #pause {
   
        position: absolute;
        width: 98px;
        height: 62px;
        background-color: rgba(86, 167, 167, 0.789);
        color: white;
        margin-left: 1300px;
        margin-top: 34px;
        font-size: 22px;
      }
      #pause:hover {
   
        background-color: rgba(77, 118, 118, 0.6);
      }
      p{
   
        position: absolute;
        left: 0;
        top: 0;
        width: 50px;
        height: 50px;
        background: #666;
        border-radius: 50%;
        background-color: aquamarine;
        border: solid 1px white;
      }
    </style>
  </head>
  <body>
    <div id="scene">
            <!-- 蛇头 -->
      <div id="head" style="left: 150px; top: 0">
        <img src="./img/head.png" alt="" width="50px" height="50px" />
      </div>
      <!-- 蛇身 -->
      <p style="left: 100px; top: 0"></p>
      <p style="left: 50px; top: 0"></p>
      <p style="left: 0px; top: 0"></p>
    </div>
  </body>
</html>

在这里插入图片描述

② 获取标签,声明需要的变量

这里不做过多解释  代码中注释很详细
  window.onload = function () {
   
        // 封装获取DOM对象的函数
        function $(selector) {
   
          return document.querySelectorAll(selector);
        }

        // 闭包+自执行函数,实现贪吃蛇独立开发
        !(function () {
   
          // 声明变量,记录贪吃蛇数据
          var speedX = 50; // 水平移动速度
          var speedY = 0; // 垂直移动速度
          var score = 0; // 积分(吃掉一个食物,增加1分)

          var pauseX = 0; // 暂停时记录的X速度
          var pauseY = 0; // 暂停时记录的Y速度
          var pause = false; // 当前程序正在执行
          var pauseBtn =
  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕遥慕遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值