Vue-js 零基础 国外案例 DEMO 全课程讲解 3 我是---- 静静
Creating the Vue Instance and Styling the Healthbars 创建Vue实例和为Healthbars设置样式
整个游戏 样子 52 有需要源码的留言或者私聊我
new Vue({el: "#app",data: {playerHealth: 100,monsterHealth: 100,gameIsRuning: false},methods: {startGame() {this.gameIsRuning = true;this.playerHealth = 100;this.monsterHealth = 100;},attack() {var damage = calculateDamage(3, 10);this.monsterHealth = this.monsterHealth - damage;if (this.monsterHealth <= 0) {alert("you won! 你赢了 ");this.gameIsRuning = false;return;}max = 12;min = 5;damage = Math.max(Math.floor(Math.random() * max) + 1, min);this.playerHealth -= damage;},// specialAttack() {},// heal() {},// giveUp() {},calculateDamage(min, max) {return Math.max(Math.floor(Math.random() * max) + 1, min);}}
});
复制代码
简便封装 写法
new Vue({el: "#app",data: {playerHealth: 100,monsterHealth: 100,gameIsRuning: false},methods: {startGame() {this.gameIsRuning = true;this.playerHealth = 100;this.monsterHealth = 100;},attack() {this.monsterHealth = this.monsterHealth - this.calculateDamage(3, 10);if (this.monsterHealth <= 0) {alert("you won! 你赢了 ");this.gameIsRuning = false;return;}this.playerHealth -= this.calculateDamage(5, 12);},specialAttack() {},heal() {},giveUp() {},<!--计算伤害 使用 return 返回 一个 最大的随机数,然后 向下取整,然后生成随机数,乘以 最大 的 加一 -->calculateDamage(min, max) {return Math.max(Math.floor(Math.random() * max) + 1, min);}}
});
复制代码
这时候我们点击上面的所有按钮都好使了
new Vue({el: "#app",data: {playerHealth: 100,monsterHealth: 100,gameIsRuning: false,turns: []},methods: {startGame() {this.gameIsRuning = true;this.playerHealth = 100;this.monsterHealth = 100;},attack() {this.monsterHealth = this.monsterHealth - this.calculateDamage(3, 10);if (this.checkWin()) {return;}this.playerHealth -= this.calculateDamage(5, 12);this.checkWin();},specialAttack() {this.monsterHealth -= this.calculateDamage(10, 20);if (this.checkWin()) {return;}this.monsetAttacks();},heal() {if (this.playerHealth <= 90) {this.playerHealth += 10;} else if (this.playerHealth <= 60) {this.playerHealth += 30;} else {this.playerHealth = 100;}this.monsetAttacks();},giveUp() {this.gameIsRuning = false;},calculateDamage(min, max) {return Math.max(Math.floor(Math.random() * max) + 1, min);},checkWin() {if (this.monsterHealth <= 0) {if (confirm("you won! 你赢了开始新游戏")) {this.startGame();} else {this.gameIsRuning = false;}return true;} else if (this.playerHealth <= 0) {if (confirm("you low 你输了开始新游戏")) {this.startGame();} else {this.gameIsRuning = false;}return true;}return false;},monsetAttacks() {this.playerHealth -= this.calculateDamage(5, 12);this.checkWin();}}
});
上面我这里我为了不破坏代码的一致性,我没有加任何注释,现在给大家解释一下,
1、el:注册了一个 #app 就不说了
2、在数据data:{ playerHealth: 100,monsterHealth: 100,gameIsRuning: false,}中我们给了play 玩家 一个 血量是100 然后 怪物血量是100 然后给 game 是否运行一个false 这里贴图html 标签
复制代码
贴图 HTML 为了怕我看不懂给的是中文
这里可以看出来在html 标签中data 里面的 play 和 monst 已经放在 插值表达式中, 在 v-if中取反 游戏运行。
3、methods:{3.1 startGame(){this.gameIsRuning = true;this.playerHealth = 100;this.monsterHealth = 100;},3.2 attack() {this.monsterHealth = this.monsterHealth - this.calculateDamage(3, 10);if (this.checkWin()) {return;}this.playerHealth -= this.calculateDamage(5, 12);this.checkWin();}3.3specialAttack() {this.monsterHealth -= this.calculateDamage(10, 20);if (this.checkWin()) {return;}this.monsetAttacks();},3.4heal() {if (this.playerHealth <= 90) {this.playerHealth += 10;} else if (this.playerHealth <= 60) {this.playerHealth += 30;} else {this.playerHealth = 100;}this.monsetAttacks();},3.5giveUp() {this.gameIsRuning = false;},3.6calculateDamage(min, max) {return Math.max(Math.floor(Math.random() * max) + 1, min);},3.7checkWin() {if (this.monsterHealth <= 0) {if (confirm("you won! 你赢了开始新游戏")) {this.startGame();} else {this.gameIsRuning = false;}return true;} else if (this.playerHealth <= 0) {if (confirm("you low 你输了开始新游戏")) {this.startGame();} else {this.gameIsRuning = false;}return true;}return false;},3.8 monsetAttacks() {this.playerHealth -= this.calculateDamage(5, 12);this.checkWin();}
} 在data 同级的 方法中 我们开始写方法 ,这里就不要问我为什么没有startGame:function(){}我这个是es6的一样,以后不再说这个问题了
startGame()方法中使用this进行指向上面的data初始化数据,然后游戏运行为 true取反3.2 上面的this..monsterHealth怪兽,没有使用简写的方法,后面的this.calculateDamage(3,10) 是我在下面写的以方法这里调用的方法进行实参的传入,最大数和最小数
下面的this.玩家,使用了简写的方法,调用方法传入随机数
复制代码
if判断怪物 checkWin胜利查看这里是的话return 出去 下面查看 玩家是否胜利
3.3 这里的 超级攻击 我们使用 this 指向 加大了随机数以减少怪物血量,然后 if判断是否取得胜利,然后 return
下面的调用封装的怪物共计我们的方法 以简化代码3.4在heal中 我们进行判断玩家的血量少于多少的时候给定一定的加血数量,然后在最后在最后调用怪物攻击我们的血量。3.5就是放弃游戏,然后把gameIsRuning 变成 fasle3.6 calculateDamage中创建一个随机数 然后上面好像写了3.7 checkWin 游戏是否胜利 写的是一个if ..... else if() 这里先判断 怪物血量少于零的时候,弹出confirm系统的跳出框,告知用户是否赢得游戏,然后 this.startGame(); 指向调用开始游戏,否则就是你输了,
然后 this.gameIsRuning = false; 然后在最后return 出去 true 变成真的。 elseif是一样的自己看代码整理一下3.8 就是怪物攻击封装一个方法
复制代码
完全代码
new Vue({el: "#app",data: {playerHealth: 100,monsterHealth: 100,gameIsRuning: false,turns: []},methods: {startGame() {this.gameIsRuning = true;this.playerHealth = 100;this.monsterHealth = 100;this.turns = [];},attack() {var damage = this.calculateDamage(3, 10);this.monsterHealth -= damage;this.turns.unshift({isPlayer: true,text: "player hits monster for 玩家击中怪物" + damage});if (this.checkWin()) {return;}this.playerHealth -= this.calculateDamage(5, 12);this.checkWin();},specialAttack() {var damage = this.calculateDamage(10, 20);this.monsterHealth -= damage;this.turns.unshift({isPlayer: true,text: "player hits monsterhard for 玩家击中了怪物" + damage});if (this.checkWin()) {return;}this.monsetAttacks();},heal() {if (this.playerHealth <= 90) {num1 = this.playerHealth += 10;} else if (this.playerHealth <= 60) {num2 = this.playerHealth += 30;} else {this.playerHealth = 100;}this.turns.unshift({isPlayer: true,text:"player heals for 血量增加 " + num1? parseInt(num1) + "血量增加": parseInt(num2) + "血量增加"});this.monsetAttacks();},giveUp() {this.gameIsRuning = false;},calculateDamage(min, max) {return Math.max(Math.floor(Math.random() * max) + 1, min);},checkWin() {if (this.monsterHealth <= 0) {if (confirm("you won! 你赢了开始新游戏")) {this.startGame();} else {this.gameIsRuning = false;}return true;} else if (this.playerHealth <= 0) {if (confirm("you low 你输了开始新游戏")) {this.startGame();} else {this.gameIsRuning = false;}return true;}return false;},monsetAttacks() {var damage = this.calculateDamage(5, 12);this.playerHealth -= damage;this.turns.unshift({isPlayer: true,text: "monster hits player for 怪物击中玩家 " + damage});this.checkWin();}}
});
复制代码
转载于:https://juejin.im/post/5caf50ec518825186d6537f0
Vue-js 零基础 国外案例 DEMO 全课程讲解 3 我是---- 静静相关推荐
- Auto.js零基础入门_安卓全分辨率免ROOT引流脚本开发教程
为什么要学脚本? 目前世面上面大部分的脚本都是模拟器运行的,因为脚本是看机型的,比如你在这款手机上面写的脚本,放到其他手机就用不了.所以你如果要做自己手机上面用的脚本的话,就必须要自己写脚本,并且现在 ...
- 零基础学python书籍-图书推荐:《零基础学Python(全彩版)》
原标题:图书推荐:<零基础学Python(全彩版)> 书皮 书皮 内容简介 <零基础学 Python>是针对零基础编程学习者研发的 Python 入门教程.从初学者角度出发,通 ...
- 视频教程-WebGL 可视化3D绘图框架:Three.js 零基础上手实战-其他
WebGL 可视化3D绘图框架:Three.js 零基础上手实战 网名风舞烟,中国科技大学计算机专业.微软认证讲师(MCE).微软数据分析讲师.10多年软件行业从业经验,参与过数百万的企业级ERP系统 ...
- Vue.js 框架基础笔记
Vue.js 框架基础笔记 1. Vue.js 基本概念 1.1 遇见 Vue.js 1.1.1 为什么要学习 Vue.js 1.1.2 前端开发的复杂化 1.1.3 Vue.js 特点 1.1.4 ...
- python零基础入门教程视频下载-Python零基础入门学习视频教程全42集,资源教程下载...
课程名称 Python零基础入门学习视频教程全42集,资源教程下载 课程目录 001我和Python的第一次亲密接触 002用Python设计第一个游戏 003小插曲之变量和字符串 004改进我们的小 ...
- python基础教程视频教程百度云-Python零基础入门学习视频教程全42集百度云网盘下载...
课程简介 Python零基础入门学习视频教程全42集百度云网盘下载 课程目录 042魔法方法:算术运算 041魔法方法:构造和析构 040类和对象:一些相关的BIF 039类和对象拾遗 038类和对象 ...
- 乐学偶得《零基础Python入门编程全栈量化AI》课程238课的详细完整代码怎么样实现?靠谱答案来啦啦啦╭(╯^╰)╮
学习打卡内容搬运于乐学偶得公众号:乐学Fintech ,仅用于我学习打卡之用.也在此和学习Python的小伙伴分享~ 我学习的是乐学偶得<零基础Python入门编程全栈量化AI>课程,属于 ...
- python基础教程百度云-Python零基础入门学习视频教程全42集百度云网盘下载
课程简介 Python零基础入门学习视频教程全42集百度云网盘下载 课程目录 042魔法方法:算术运算 041魔法方法:构造和析构 040类和对象:一些相关的BIF 039类和对象拾遗 038类和对象 ...
- 视频教程-PHP零基础七天入门视频课程(免费50章)-PHP
PHP零基础七天入门视频课程(免费50章) 04年进入计算机行业.拥有6年net和php项目开发经验,8年java项目开发经验. 现前端全栈工程师,主攻产品设计,微信开发等. 黄菊华 ¥159.00 ...
最新文章
- Python练习2-基本聊天程序-虚拟茶会话
- Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
- Nginx代理webSocket经常中断的解决方案, 如何保持长连接
- 旋转根组件 Learn Unreal Engine (with C++)
- 使用.gitignore忽略编译自动生成的那些文件
- win10 多任务 多视图 多窗口 处理快捷键
- 【面试】数据分析助理面试
- lambda表达式与正则表达式
- Excel 函数大全之查找和引用函数 01 ADDRESS、AREAS、CHOOSE 、CHOOSECOLS、CHOOSEROWS、COLUMN 、COLUMNS教程含使用方法
- 校招回顾,大疆校招可内推
- win7下用VS2008写视频聊天程序,求VFW教程?qzvgK
- 机器学习中对数据集进行拆分及模型训练
- Mega2560(Arduino)Bootloader烧录指南
- 【项目管理】项目风险分析
- 四面体内接圆圆心坐标计算模板
- MapKeyboard键盘改键
- sql语句往某个字段指定位置追加或者插入值
- 贷款业务流程以及互联网金融的主要形态
- 常用MySQL图形化管理工具
- vod硬件服务器,点播视频 (VOD) 服务器
热门文章
- 技术学院技能发展网络在线技能培训在线技能Rtaj比赛进行到一乔
- 【C语言】大一小白的一个C语言扎金花小程序
- DEEP DOUBLE DESCENT: WHERE BIGGER MODELS AND MORE DATA HURT
- 有效沟通bic法则_在职场中有效沟通的法则
- 计算机报名入口官网登录一级,一级计算机考试报名入口诚信互利
- 怎么sketch画板导出html,sketch符号和导出画板 – Sketch入门UI设计教程
- 计算机人要具备的基础知识!
- 程序员放弃阿里工作回家当公务员,朋友:年纪轻轻为什么不奋斗
- 最全软件著作权申请流程
- 2020年一线城市程序员工资大调查