Browse Source

init commit

master
周彬 7 months ago
commit
1439403ca8
  1. 349
      README.md
  2. 4930
      package-lock.json
  3. 25
      package.json
  4. BIN
      src/assets/bomb.png
  5. BIN
      src/assets/dude.png
  6. BIN
      src/assets/platform.png
  7. BIN
      src/assets/sky.png
  8. BIN
      src/assets/star.png
  9. BIN
      src/favicon.ico
  10. 20
      src/index.html
  11. 163
      src/main.js
  12. 51
      webpack.development.js
  13. 46
      webpack.production.js

349
README.md

@ -0,0 +1,349 @@
# 首先得安装好npm和node.js !!!
# webpack+phaser+javascript
# 开发配置
初始化项目目录:
npm init -y
安装phaser:
npm install --save phaser
安装webpack和webpack扩展:
npm install --save-dev webpack
npm install --save-dev webpack-cli
安装webpack的copy和clean插件:
npm install --save-dev copy-webpack-plugin
npm install --save-dev clean-webpack-plugin
npm install --save-dev html-webpack-plugin
再创建一个测试脚本:
npm install --save-dev webpack-dev-server
这里有个要说明,如果安装phaser使用npm安装使用--save-dev,那么phaser就会会放到devDependencies。
dependencies:生产环境需要的依赖库;
devDependencies:开发环境需要的依赖库;
创建一个webpack.development.js:
```js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: {
// 入口点,即主 JavaScript 文件
app: './src/main.js',
},
output: {
// 输出文件,捆绑了所有库
filename: '[name]-[contenthash].bundle.js',
// 输出包的路径,“dist” 文件夹
path: path.resolve(__dirname, 'dist/test'),
// 静态资源文件
assetModuleFilename: "asset-packs/[name]-[hash][ext][query]",
},
// 处于开发模式
mode: 'development',
// 需要一个源映射
devtool: 'inline-source-map',
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html', // 指定你的HTML模板
filename: 'index.html', // 输出的文件名
inject: 'body', // 将脚本插入到body底部
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'src/favicon.ico', to: '' },
{ from: 'src/assets', to: 'assets' }
],
}),
],
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
// 开发服务器根目录是 “src” 文件夹
devServer: {
static: './src'
}
};
```
创建一个webpack.production.js:
```js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
// 入口点,即主 JavaScript 文件
app: './src/main.js',
},
output: {
// 输出文件,捆绑了所有库
filename: '[name]-[contenthash].bundle.js',
// 输出包的路径,“dist” 文件夹
path: path.resolve(__dirname, 'dist/test'),
// 静态资源文件
assetModuleFilename: "asset-packs/[name]-[hash][ext][query]",
},
// 处于生产模式
mode: 'production',
// 需要一个源映射
devtool: 'inline-source-map',
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html', // 指定你的HTML模板
filename: 'index.html', // 输出的文件名
inject: 'body', // 将脚本插入到body底部
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'src/favicon.ico', to: '' },
{ from: 'src/assets', to: 'assets' }
],
}),
],
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
};
```
package.json中启动脚本改成如下:
```text
"scripts": {
"dev": "webpack serve --open --config webpack.development.js",
"test": "webpack --config webpack.development.js",
"pro": "webpack --config webpack.production.js"
},
```
# 前端相关的代码
创建新目录src,新建文件src/main.js,src/index.html
其中index.html代码如下:
```html
<!doctype html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Making your first Phaser 3 Game</title>
<!-- 指定favicon.ico的路径 -->
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon">
<link rel="icon" href="./favicon.ico" type="image/x-icon">
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
</body>
</html>
```
main.js代码如下:
```js
import Phaser from 'phaser';
class PlayGame extends Phaser.Scene {
constructor() {
super('PlayGame');
this.player = null;
this.stars = null;
this.bombs = null;
this.platforms = null;
this.cursors = null;
this.score = 0;
this.gameOver = false;
this.scoreText = null;
}
preload() {
// 加载游戏资源
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
this.load.spritesheet('dude', 'assets/dude.png', {frameWidth: 32, frameHeight: 48});
}
create() {
// 为我们的游戏设置一个简单的背景
this.add.image(400, 300, 'sky');
// platforms组包含了地面和我们可以跳跃的两个平台
this.platforms = this.physics.add.staticGroup();
// 这里我们创建了地面。
// 调整它的大小以适应游戏的宽度(原始大小为400x32)
this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();
// 现在让我们创建一些平台
this.platforms.create(600, 400, 'ground');
this.platforms.create(50, 250, 'ground');
this.platforms.create(750, 220, 'ground');
// 玩家和它的设置
this.player = this.physics.add.sprite(100, 450, 'dude');
// 玩家的物理属性。给这个小家伙一点弹性。
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
// 我们的玩家动画,转身,向左走和向右走。
this.anims.create({
key: 'left', frames: this.anims.generateFrameNumbers('dude', {start: 0, end: 3}), frameRate: 10, repeat: -1
});
this.anims.create({
key: 'turn', frames: [{key: 'dude', frame: 4}], frameRate: 20
});
this.anims.create({
key: 'right', frames: this.anims.generateFrameNumbers('dude', {start: 5, end: 8}), frameRate: 10, repeat: -1
});
// 输入事件
this.cursors = this.input.keyboard.createCursorKeys();
// 一些星星供收集,总共12个,沿x轴均匀间隔70像素
this.stars = this.physics.add.group({
key: 'star', repeat: 11, setXY: {x: 12, y: 0, stepX: 70}
});
this.stars.children.iterate(function (child) {
// 给每个星星稍微不同的弹性
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.bombs = this.physics.add.group();
// 得分
this.scoreText = this.add.text(16, 16, 'score: 0', {fontSize: '32px', fill: '#000'});
// 让玩家和星星与平台碰撞
this.physics.add.collider(this.player, this.platforms);
this.physics.add.collider(this.stars, this.platforms);
this.physics.add.collider(this.bombs, this.platforms);
// 检查玩家是否与任何星星重叠,如果是,则调用collectStar函数
this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this);
this.physics.add.collider(this.player, this.bombs, this.hitBomb, null, this);
}
update() {
if (this.gameOver) {
return;
}
// 控制玩家移动
this.controlPlayerMovement();
// 玩家跳跃
if (this.cursors.up.isDown && this.player.body.touching.down) {
this.player.setVelocityY(-330);
}
}
controlPlayerMovement() {
if (this.cursors.left.isDown) {
this.player.setVelocityX(-160);
this.player.anims.play('left', true);
} else if (this.cursors.right.isDown) {
this.player.setVelocityX(160);
this.player.anims.play('right', true);
} else {
this.player.setVelocityX(0);
this.player.anims.play('turn');
}
}
collectStar(player, star) {
// 收集星星
star.disableBody(true, true);
// 增加分数
this.score += 10;
this.scoreText.setText('Score: ' + this.score);
if (this.stars.countActive(true) === 0) {
// 生成新的星星
this.stars.children.iterate((child) => {
child.enableBody(true, child.x, 0, true, true);
});
let x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
let bomb = this.bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
bomb.allowGravity = false;
}
}
hitBomb(player, bomb) {
// 碰撞炸弹
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('turn');
this.gameOver = true;
}
}
const config = {
type: Phaser.AUTO, width: 800, height: 600, physics: {
default: 'arcade', arcade: {
gravity: {y: 300}, debug: false
}
}, scene: PlayGame
};
const game = new Phaser.Game(config);
```
在到src目录下创建asserts,放图片资源。
项目启动测试环境:
npm run dev
项目打包测试环境:
npm run test
项目打包生产环境:
npm run pro

4930
package-lock.json

File diff suppressed because it is too large

25
package.json

@ -0,0 +1,25 @@
{
"name": "phaserdemo",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack serve --open --config webpack.development.js",
"test": "webpack --config webpack.development.js",
"pro": "webpack --config webpack.production.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"phaser": "^3.85.2"
},
"devDependencies": {
"clean-webpack-plugin": "^4.0.0",
"copy-webpack-plugin": "^12.0.2",
"html-webpack-plugin": "^5.6.0",
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.1.0"
}
}

BIN
src/assets/bomb.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 495 B

BIN
src/assets/dude.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
src/assets/platform.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
src/assets/sky.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
src/assets/star.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 B

BIN
src/favicon.ico

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

20
src/index.html

@ -0,0 +1,20 @@
<!doctype html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Making your first Phaser 3 Game</title>
<!-- 指定favicon.ico的路径 -->
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon">
<link rel="icon" href="./favicon.ico" type="image/x-icon">
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
</body>
</html>

163
src/main.js

@ -0,0 +1,163 @@
import Phaser from 'phaser';
class PlayGame extends Phaser.Scene {
constructor() {
super('PlayGame');
this.player = null;
this.stars = null;
this.bombs = null;
this.platforms = null;
this.cursors = null;
this.score = 0;
this.gameOver = false;
this.scoreText = null;
}
preload() {
// 加载游戏资源
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
this.load.spritesheet('dude', 'assets/dude.png', {frameWidth: 32, frameHeight: 48});
}
create() {
// 为我们的游戏设置一个简单的背景
this.add.image(400, 300, 'sky');
// platforms组包含了地面和我们可以跳跃的两个平台
this.platforms = this.physics.add.staticGroup();
// 这里我们创建了地面。
// 调整它的大小以适应游戏的宽度(原始大小为400x32)
this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();
// 现在让我们创建一些平台
this.platforms.create(600, 400, 'ground');
this.platforms.create(50, 250, 'ground');
this.platforms.create(750, 220, 'ground');
// 玩家和它的设置
this.player = this.physics.add.sprite(100, 450, 'dude');
// 玩家的物理属性。给这个小家伙一点弹性。
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
// 我们的玩家动画,转身,向左走和向右走。
this.anims.create({
key: 'left', frames: this.anims.generateFrameNumbers('dude', {start: 0, end: 3}), frameRate: 10, repeat: -1
});
this.anims.create({
key: 'turn', frames: [{key: 'dude', frame: 4}], frameRate: 20
});
this.anims.create({
key: 'right', frames: this.anims.generateFrameNumbers('dude', {start: 5, end: 8}), frameRate: 10, repeat: -1
});
// 输入事件
this.cursors = this.input.keyboard.createCursorKeys();
// 一些星星供收集,总共12个,沿x轴均匀间隔70像素
this.stars = this.physics.add.group({
key: 'star', repeat: 11, setXY: {x: 12, y: 0, stepX: 70}
});
this.stars.children.iterate(function (child) {
// 给每个星星稍微不同的弹性
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.bombs = this.physics.add.group();
// 得分
this.scoreText = this.add.text(16, 16, 'score: 0', {fontSize: '32px', fill: '#000'});
// 让玩家和星星与平台碰撞
this.physics.add.collider(this.player, this.platforms);
this.physics.add.collider(this.stars, this.platforms);
this.physics.add.collider(this.bombs, this.platforms);
// 检查玩家是否与任何星星重叠,如果是,则调用collectStar函数
this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this);
this.physics.add.collider(this.player, this.bombs, this.hitBomb, null, this);
}
update() {
if (this.gameOver) {
return;
}
// 控制玩家移动
this.controlPlayerMovement();
// 玩家跳跃
if (this.cursors.up.isDown && this.player.body.touching.down) {
this.player.setVelocityY(-330);
}
}
controlPlayerMovement() {
if (this.cursors.left.isDown) {
this.player.setVelocityX(-160);
this.player.anims.play('left', true);
} else if (this.cursors.right.isDown) {
this.player.setVelocityX(160);
this.player.anims.play('right', true);
} else {
this.player.setVelocityX(0);
this.player.anims.play('turn');
}
}
collectStar(player, star) {
// 收集星星
star.disableBody(true, true);
// 增加分数
this.score += 10;
this.scoreText.setText('Score: ' + this.score);
if (this.stars.countActive(true) === 0) {
// 生成新的星星
this.stars.children.iterate((child) => {
child.enableBody(true, child.x, 0, true, true);
});
let x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
let bomb = this.bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
bomb.allowGravity = false;
}
}
hitBomb(player, bomb) {
// 碰撞炸弹
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('turn');
this.gameOver = true;
}
}
const config = {
type: Phaser.AUTO, width: 800, height: 600, physics: {
default: 'arcade', arcade: {
gravity: {y: 300}, debug: false
}
}, scene: PlayGame
};
const game = new Phaser.Game(config);

51
webpack.development.js

@ -0,0 +1,51 @@
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: {
// 入口点,即主 JavaScript 文件
app: './src/main.js',
},
output: {
// 输出文件,捆绑了所有库
filename: '[name]-[contenthash].bundle.js',
// 输出包的路径,“dist” 文件夹
path: path.resolve(__dirname, 'dist/test'),
// 静态资源文件
assetModuleFilename: "asset-packs/[name]-[hash][ext][query]",
},
// 处于开发模式
mode: 'development',
// 需要一个源映射
devtool: 'inline-source-map',
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html', // 指定你的HTML模板
filename: 'index.html', // 输出的文件名
inject: 'body', // 将脚本插入到body底部
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'src/favicon.ico', to: '' },
{ from: 'src/assets', to: 'assets' }
],
}),
],
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
// 开发服务器根目录是 “src” 文件夹
devServer: {
static: './src'
}
};

46
webpack.production.js

@ -0,0 +1,46 @@
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
// 入口点,即主 JavaScript 文件
app: './src/main.js',
},
output: {
// 输出文件,捆绑了所有库
filename: '[name]-[contenthash].bundle.js',
// 输出包的路径,“dist” 文件夹
path: path.resolve(__dirname, 'dist/test'),
// 静态资源文件
assetModuleFilename: "asset-packs/[name]-[hash][ext][query]",
},
// 处于生产模式
mode: 'production',
// 需要一个源映射
devtool: 'inline-source-map',
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html', // 指定你的HTML模板
filename: 'index.html', // 输出的文件名
inject: 'body', // 将脚本插入到body底部
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'src/favicon.ico', to: '' },
{ from: 'src/assets', to: 'assets' }
],
}),
],
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
};
Loading…
Cancel
Save