用TS从零开始制作贪吃蛇游戏--2

本内容学习来自B站李立超老师 原文链接

本项目软件使用vscode,样式文件使用less,面对对象的TS,打包通过webpack进行制作。


上一节我们已经搭建好了项目的文件夹结构,这节我们来进行编写贪吃蛇游戏的html结构。

目录

编写贪吃蛇游戏的HTML结构

编写CSS样式

结语


编写贪吃蛇游戏的HTML结构

在之前建好的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>
  </head>
  <body>
    <!-- 居中游戏区域 -->
    <div id="main">
      <!-- 游戏活动区域 -->
      <div id="stage">
        <!-- 蛇 -->
        <div id="snake">
          <!-- 蛇的身体 -->
          <div></div>
        </div>
        <!-- 食物 -->
        <div id="food"></div>
      </div>
      <!-- 得分表 -->
      <div id="score-panel">
        <!-- 分数 -->
        <div>
          SCORE:
          <span id="score">0</span>
        </div>
        <!-- 等级 -->
        <div>
          level:
          <span id="level">1</span>
        </div>
      </div>
    </div>
  </body>
</html>

编写CSS样式

 在index.less中编写如下代码

@bg-color: #b7d4a8;
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font: bold 20px 'Courier';
  color: #000;
}

#main {
  width: 360px;
  height: 420px;
  background: @bg-color;
  margin: 100px auto;
  border: 10px solid #000;
  border-radius: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  #stage {
    width: 304px;
    height: 304px;
    border: 2px solid #000;
    position: relative;
    #snake {
      transition: left 0.1s ease-in-out, top 0.1s ease-in-out;
      & > div {
        width: 10px;
        height: 10px;
        background: #000;
        border: 1px solid @bg-color;
        position: absolute;
      }
    }
    #food {
      width: 10px;
      height: 10px;
      background: #fff;
      border: 1px solid @bg-color;
      position: absolute;
    }
  }
  #score-panel {
    width: 300px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

 编写完成后运行项目,即可看到效果。

 如果css样式没生效的话检查是否在index.ts文件里引入了index.less

import './index.less'

结语

        这节把贪吃蛇游戏的HTML结构编写了出来,下节我们进行编写贪吃蛇游戏逻辑TS代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

瑞雨溪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值