9 Star 31 Fork 12

边城 / tetris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Tetris (TypeScript) - 网页版俄罗斯方块

思否 (SegmentFault) 专栏:边城客栈 作者:边城(James Fan)


采用技术

项目结构

目录/文件 说明
src/
src/index.html 主页面(也是唯一 HTML 页面)
src/less/ 样式表源码
src/scripts/ TypeScript 脚本源码
docs/ 文档
dist/ 构建结果(未构建前不存在此目录)
dist/index.html 发布页面,直接从 src/index.html 拷贝过来
dist/css/ src/less/ 构建出来的样式表
dist/js/ src/scripts/ 构建出来的脚本,需要浏览器支持 type="module"
dist/libs/ 页面上引用的第三方库,目前只有 jQuery

环境搭建

开发系统环境(以及 npm scripts 的环境)是 Windows 10,较低 Windows 版本或 Linux 需要对 package.jsonscripts 下的脚本进行修改。

虽然是前端,仍然采用了项目构建的思想,目标脚本和样式表通过构建生成。构建在 NodeJs 14+ 环境下进行。

开发中使用了 TypeScript,需要 TypeScript 语言服务。虽然大多数支持 TypeScript 的编辑器或 IDE 已经内置了 TypeScript 语言服务支持,可能可能不是最新版本。可以通过 npm 安装最新版本的 TypeScript,之后通过 tsc --version 检查安装的版本。

TypeScript 可以全局安装,也可以局部安装,建议采用局部安装方式:

tetris/> npm install typescript
tetris/> npx tsc --version

构建

最新重构使用 <script type="module" ...></script> 来引用脚本,使用现代 JS 语法,不需要打包,也不需要 Babel 转译,所以去掉了 Babel 和 Gulp,采用最简化的构造方式,由 npm scripts 实现。

完整构建使用 npm run build 命令,注意该命令不包含对旧构建结果的清理工作。

  • 构建 TypeScript 直接使用 tsc 命令,详见 package.json 中的 compile 脚本。

    由于 TypeScript 对 type="module" 支持不够友好,源代码中 import 语句需要给脚本文件加上 .js 扩展名(特别注意——不是 .ts)。

  • 构建 LESS 样式表使用 lessc 命令,详见 package.json 中的 lessc 脚本。

  • 拷贝 index.htmljQuery 使用系统命令,详见 package.json 中的 copy-htmlcopy-jquery 脚本。

  • 清理构建结果使用 Powershell Corerm 命令,详见 pckage.json 中的 clean 脚本。

运行

由于使用 type="module",不能直接通过浏览器打开本地 index.html 来运行,需要将构建结果(dist 目录)作为静态 Web 发布运行,通过 HTTP 协议访问。Web 服务容器可选用 Nginx、IIS、Apache 等。

基于 Node 技术栈建议选择 http-server,建议全局安装方便在各种开发环境下使用:

/> npm install -g http-server
/> cd d:/workspace/tetris/
/> http-server dist

操作

快捷键 功能
[UP] 方块旋转(顺时针)
[LEFT] 向左移动
[RIGHT] 向右移动
[DOWN] 向下移动
[SPACE] 直降到底

相关阅读:SegmentFault 上的系列博文

MIT License Copyright (c) 2021 边城 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

网页 JavaScript 版俄罗斯方块 展开 收起
JavaScript 等 3 种语言
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/jamesfancy/tetris.git
git@gitee.com:jamesfancy/tetris.git
jamesfancy
tetris
tetris
main

搜索帮助