前期准备

技术栈

  • vue3(runtime-core)
  • canvas 游戏引擎 pixi.js
  • webpack

    开发工具

    vscode

搭建项目并完成初始化

使用npm初始化项目管理

  1. # 初始化项目管理生成 package.json
  2. npm -y
  3. # 使用 git 管理项目
  4. ## 初始化 git, 生成 git 仓库
  5. git init
  6. ## 添加远程仓库
  7. git remote add <name> <address>
  8. ### 例如: git remote add origin git@github.com:xx/xx.git

安装架包

  1. # 核心架包 @vue/runtime
  2. yarn add @vue/runtime-core
  3. # canvas 游戏引擎 pixi.js
  4. yarn add pixi.js
  5. # js 动画库 tween.js
  6. yarn add @tweenjs/tween.js
  7. # 打包工具 webpack
  8. yarn add webpack webpack-cli -D
  9. # webpack 热更新工具
  10. yarn add webpack-dev-server -D
  11. # 静态文件处理工具(图片资源)
  12. yarn add file-loader -D

在 package.json 添加开发和打包命令

  1. "scripts": {
  2. "serve": "webpack-dev-server --open",
  3. "build": "webpack"
  4. }


初始化目录结构

  1. #整体结构
  2. ┌─public
  3. └─index.html
  4. │─src 代码编辑区
  5. └─**** 查看下方src文件夹下目录结构
  6. ├─.gitignore git 过滤文件
  7. ├─webpack.config.js webpack 配置文件
  8. └─package.json 架包管理文件
  9. #src文件夹下目录
  10. ┌─apis 请求数据接口
  11. ├─config 项目配置文件
  12. └─index.js
  13. │─components 组件目录
  14. ├─Map.js 可复用的组件(地图组件)
  15. └─... 更多组件
  16. ├─pages 业务页面文件存放的目录
  17. ├─GamePage.js 游戏界面
  18. └─... 更多页面
  19. ├─assets 存放应用引用静态资源(如图片、视频等)的目录,注意:静态资源只能存放于此
  20. └─images 图片资源
  21. ├─runtime-canvas
  22. └─index.js vue3 自定义渲染器实现
  23. ├─utils 工具文件存在在该目录下
  24. ├─main.js Vue初始化入口文件
  25. ├─App.js 应用配置,用来配置App全局样式以及监听 应用生命周期
  26. └─Game.js 游戏配置, canvas 元素添加到Dom

可以自定义目录,根据项目需求。

附录:

package.json 配置

  1. {
  2. "name": "game-aircraft-battle",
  3. "version": "1.0.0",
  4. "description": "基于vue3 的 canvas 游戏",
  5. "main": "main.js",
  6. "scripts": {
  7. "serve": "webpack-dev-server --open",
  8. "build": "webpack"
  9. },
  10. "repository": {
  11. "type": "git",
  12. "url": "git+https://github.com/Fuarmy/game-aircraft-battle.git"
  13. },
  14. "keywords": [],
  15. "author": "",
  16. "license": "ISC",
  17. "bugs": {
  18. "url": "https://github.com/Fuarmy/game-aircraft-battle/issues"
  19. },
  20. "homepage": "https://github.com/Fuarmy/game-aircraft-battle#readme",
  21. "dependencies": {
  22. "@tweenjs/tween.js": "^18.6.4",
  23. "@vue/runtime-core": "3.0.0-rc.5",
  24. "pixi.js": "^5.3.5"
  25. },
  26. "devDependencies": {
  27. "file-loader": "^6.2.0",
  28. "webpack": "4.44.1",
  29. "webpack-cli": "3.3.12",
  30. "webpack-dev-server": "^3.11.0"
  31. }
  32. }

webpack.config.js 配置

  1. const path = require("path");
  2. const { webpack } = require("webpack");
  3. module.exports = {
  4. entry: path.resolve(__dirname, "src/main.js"),
  5. output: {
  6. filename: "main.bundle.js",
  7. path: path.resolve(__dirname, "dist")
  8. },
  9. devtool: "source-map",
  10. devServer: {
  11. contentBase: path.resolve(__dirname, "public")
  12. },
  13. module: {
  14. rules: [
  15. {
  16. test: /\.(png|jpe?g|gif)$/i,
  17. use: [
  18. {
  19. loader: "file-loader",
  20. options: {
  21. outputPath: "assets/images/",
  22. publicPath: ""
  23. }
  24. }
  25. ]
  26. }
  27. ]
  28. }
  29. }

文件来源于 @开课吧

参考资料:

Vue3 官方文档
Vue3 中文文档