JS贪吃蛇

效果展示

CSS样式

index.html

<!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>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
    <div class="content">
        <div class="btn startBtn"><button></button></div>
        <div class="btn pauseBtn"><button></button></div>
        <div id="snakeWrap">
        </div>
    </div>
    <script src="js/index.js"></script>
</body>
</html>

index.css

* {
   
    margin: 0;
    padding: 0;
}

.content {
   
    width: 640px;
    height: 640px;
    margin: 100px auto;
    position: relative;
}

.btn {
   
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.1);
    z-index: 2;
}

.btn button {
   
    background: none;
    border: none;
    outline: none;
    cursor: pointer;
    background-size: 100% 100%;
    position: absolute;
    left: 50%;
    top: 50%;
}

.startBtn button {
   
    width: 200px;
    height: 80px;
    background-image: url(../images/bb40248cdc9948fdbc67f8763b7965b0.gif);
    margin-top: -40px;
    margin-left: -100px;
}

.pauseBtn {
   
    display: none;
}

.pauseBtn button {
   
    width: 70px;
    height: 70px;
    background-image: url(../images/f2346ea52af94531915045ceafc009d7.png);
    margin-top: -35px;
    margin-left: -35px;
}

#snakeWrap {
   
    position: relative;
    width: 600px;
    height: 600px;
    background: #FCE4EC;
    border: 20px solid #F8BBD0;
}

#snakeWrap div {
   
    width: 20px;
    height: 20px;
}

.snakeHead {
   
    background-image: url(../images/fde4a7ae7f97468a93532e63a16befad.png);
    background-size: cover;
}

.snakeBody {
   
    background-color: #9CCC65;
    border-radius: 50%;
}

.food {
   
    background-image: url(../images/59d8305e0fed4a908a9ed9cc65a8b72a.png);
    background-size: 100% 100%;
}

构造方格

  • 需要一个构造方块的函数,用来存放蛇的全身以及蛇要经过的路径
var sw = 20, //一个方块的宽度
    sh = 20, //一个方块的高度
    tr = 30, //行数
    td = 30; //列数
var snake = null, //蛇的实例
    food = n
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值