FlappyBird模拟(不完整版本)
FlappyBird模拟(不完整版本)
准备材料
land地

sky天

pipe管道


- bird小鸟

Land.js
function Land(info) {
this.x = info.x;
this.canvas = info.canvas;
this.context = info.context;
this.image = info.image;
this.speed = 2;
}
Land.prototype = {
constructor: Land,
draw: function () {
this.x -= this.speed;
if (this.x <= -this.image.width) {
//这里的3是画布中最多出现三个地板,最好也放在info传进来,但本例子就不传,直接写了
this.x = 3 * this.image.width;
}
context.drawImage(this.image, this.x, this.canvas.height-this.image.height, this.image.width, this.image.height);
}
}
Sky.js
function Sky(info) {
this.x = info.x;
this.context = info.context;
this.canvas = info.canvas;
this.image = info.image;
this.speed = 2;
}
Sky.prototype = {
constructor: Sky,
//自已绘制自己
draw: function () {
//往左移动
this.x -= this.speed;
//当它移出舞台, 就马上跟到队伍的是后面
if (this.x <= -this.canvas.width) {
this.x = this.canvas.width;
}
//把自己绘制到画布上去
this.context.drawImage(this.image, this.x, 0, this.canvas.width, this.canvas.height);
}
}
Pipe.js
function Pipe(info) {
//上面的部分的图片
this.topImage = info.topImage;
//下面的部分的图片
this.bottomImage = info.bottomImage;
//x
this.x = info.x;
this.canvas = info.canvas;
this.context = info.context;
//底部的间隔
this.offsetY = info.offsetY;
//柱子和柱子之是的间隔
this.gap = info.gap;
//速度
this.speed = 2;
//上下两根柱子的高度
this.topHeight = 0;
this.bottomHeight = 0;
//调用一下initHeight
this.initHeight();
}
Pipe.prototype = {
constructor: Pipe,
draw: function () {
this.x -= this.speed;
if (this.x <= - this.topImage.width) {
this.initHeight();
this.x = this.gap * 6 + this.topImage.width * 5;
}
//画上面的柱子
this.context.drawImage(this.topImage, this.x, 0, this.topImage.width, this.topHeight);
//画下面的柱子
this.context.drawImage(this.bottomImage, this.x, this.topHeight + 100, this.bottomImage.width, this.bottomHeight);
},
//初始化柱子的高度
initHeight: function () {
//生成上半部分的柱子的高度, 100到250的一个随机值
this.topHeight = 100 + 150 * Math.random();
//100就是柱子上下之间的间隔
this.bottomHeight = this.canvas.height - this.offsetY - this.topHeight - 100;
}
}
index.js
//1. 加载出所有的图片
var birdsImg = new Image();
birdsImg.src = "./img/birds.png";
var landImg = new Image();
landImg.src = "./img/land.png";
var skyImg = new Image();
skyImg.src = "./img/sky.png";
var pipe1Img = new Image();
pipe1Img.src = "./img/pipe1.png";
var pipe2Img = new Image();
pipe2Img.src = "./img/pipe2.png";
var imagesArr = [birdsImg, landImg, skyImg, pipe1Img, pipe2Img];
var count = 0;
imagesArr.forEach(function (image) {
image.onload = function () {
count += 1;
if (count == imagesArr.length) {
//所有的角色的数组
var rolesArr = [];
//使用类来创建角色
function createRoles() {
//1. 创建所有的天空对象(生孩子)
for (var i = 0; i<2; i++) {
var sky = new Sky({
x: i * canvas.width,
image: skyImg,
canvas: canvas,
context: context
})
rolesArr.push(sky);
}
//2. 创建所有的陆地对象(生孩子)
for (var i = 0; i<4; i++) {
var land = new Land({
x: i * landImg.width,
canvas: canvas,
context: context,
image: landImg
});
rolesArr.push(land);
}
//3. 创建所有的管道对象(生孩)
var gap = (canvas.width - 6 * pipe1Img.width)/5;
for (var i = 0; i<6; i++) {
var pipe = new Pipe({
topImage: pipe2Img,
bottomImage: pipe1Img,
x: 300 + (pipe1Img.width + gap) * i,
canvas: canvas,
context: context,
offsetY: landImg.height,
gap: gap
});
rolesArr.push(pipe);
}
}
//调用创建对象的方法
createRoles();
//开始action
function action() {
//1. 画布要清空
context.clearRect(0, 0, canvas.width, canvas.height);
//2. 角色就开始自己绘制自己
rolesArr.forEach(function (role) {
role.draw();
});
//2. 添加动画
window.requestAnimationFrame(action);
}
//开始绘制
action();
}
}
})
FlappyBird模拟(不完整版本)的更多相关文章
- PHP:API 接口规范完整版本
整体规范建议采用RESTful 方式来实施. 协议 API与用户的通信协议,总是使用HTTPs协议,确保交互数据的传输安全. 域名 应该尽量将API部署在专用域名之下.https://api.exam ...
- 必应缤纷桌面的必应助手-软件分析和用户市场需求之-----二.体验部分 Ryan Mao (毛宇11061171) (完整版本请参考团队博客)
<必应缤纷桌面的必应助手> 2.体验部分 Ryan Mao (毛宇11061171) (完整分析报告请参考团队博客http://www.cnblogs.com/Z-XML/) 我花了2天的 ...
- openldap完整版本搭建记录
文档信息 目 的:搭建一套完整的OpenLDAP系统,实现账号的统一管理. 1:OpenLDAP服务端的搭建 ...
- 对学长所谓“改变世界的游戏”《shield star》的运行感想-毛宇部分(完整版本请参考团队博客)
对于学长项目<shield star>的思考和看法: Ryan Mao ((毛宇) 110616-11061171 试用了一下学长黄杨等人开发的<shield star>游戏 ...
- dubbo环境搭建与tomcat集成、DEMO示例、常见问题(最完整版本、带管理控制台、监控中心、zookeeper)
以windows为例,linux基本相同,开发环境一般linux,个人环境一般windows(如果不开额外vm的话). 示例以dubbo官方自带demo为例子,进行整合和稍加修改测试. 0.dubbo ...
- 模拟IE各种版本的方法
下载360极速浏览器.开启“兼容模式” 默认会是IE7.可以通过控制台(Ctrl + shift + I)调整各个版本
- 基于Unity的A星寻路算法(绝对简单完整版本)
前言 在上一篇文章,介绍了网格地图的实现方式,基于该文章,我们来实现一个A星寻路的算法,最终实现的效果为: 项目源码已上传Github:AStarNavigate 在阅读本篇文章,如果你对于里面提到的 ...
- 完整版本的推箱子小游戏,最简单的纯C语言打造
/* 推箱子小游戏 1.定义绘制样式 用二维数组的方式 2.绘制图像 3.找出当前位置 4.逻辑判断,制造动作 根据数学xy轴的规律,这里使用ij 上移,行轴上升,行数减少 下移,行数下降,函数增加 ...
- java多线程分块上传并支持断点续传最新修正完整版本[转]
package com.test; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.Fi ...
随机推荐
- 理解Python中编码的应用
完全理解字符编码 与 Python 的渊源前,我们有必要把一些基础概念弄清楚,虽然有些概念我们每天都在接触甚至在使用它,但并不一定真正理解它.比如:字节.字符.字符集.字符码.字符编码. 字节 字节( ...
- 如何将一个已有的项目托管到github或是码云上?git的配置
场景一:已有的一个项目,要把它托管到Git上去,步骤和方法如下: 方法一: ①在工程的路径下 : git init 建一个裸仓库. ②远程仓库地址 :将本地的仓库和远程仓库关联 git remote ...
- yum的方式搭建mysql
1.安装相应的软件yum install mysql : 安装mysql客户端 yum install mysql-server 安装服务端 yum install mysql-devel 安装相关的 ...
- S-HR远程调试
- [frontend] 根据文字长度 自适应宽度 自适应高度+ Uncaught ReferenceError: xxx is not defined at HTMLDivElement.onclick
CSS一行代码就可以解决第一个问题: 1.1 根据文字长度,自适应标签宽度 解决方法:把width的设置删掉,加一行代码 display:table; .tag-footdetail{ /*widt ...
- Idea里面的postfix
感谢慕课网IntelliJ IDEA神器使用技巧 闪电侠讲师,感觉有些工具真的是听听别人讲的比自己琢磨快很多 https://www.imooc.com/learn/924 也可以这样找到postfi ...
- 域名IP绑定
该文为阿里云域名举例 首先具备3个前提: 买服务器并搭建环境:阿里云官网购买阿里云的服务器(我购买的是window系统,ECS服务器). 在自己的云服务器上布置上jdk,配置环境变量:安装上tomca ...
- Java代码规范和一些常见问题
本文中的代码规范,是Java标准代码规范中的一小部分,在我看来,是最重要的一部分. 理想目标:不需要写注释,不需要和别人介绍,别人就知道你的项目大致是做什么的,每个类大概实现了什么功能. ...
- python进行excel操作
使用模块 xlsxwriter : http://xlsxwriter.readthedocs.io/ 可以自定义表格合并.样式 加各种2D图表 写入格式化表格数据时,与pandas结合速度更快!
- myquant平台搭建及使用
1.主页 http://myquant.cn/ 点击“我要申请试用”,进入如下页面:http://myquant.cn/news/2015/03/25/try-gmsdk-v2.0/ 点击“试用注册” ...