FlappyBird模拟(不完整版本)

准备材料

  1. land地

  2. sky天

  3. pipe管道

  1. 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模拟(不完整版本)的更多相关文章

  1. PHP:API 接口规范完整版本

    整体规范建议采用RESTful 方式来实施. 协议 API与用户的通信协议,总是使用HTTPs协议,确保交互数据的传输安全. 域名 应该尽量将API部署在专用域名之下.https://api.exam ...

  2. 必应缤纷桌面的必应助手-软件分析和用户市场需求之-----二.体验部分 Ryan Mao (毛宇11061171) (完整版本请参考团队博客)

    <必应缤纷桌面的必应助手> 2.体验部分 Ryan Mao (毛宇11061171) (完整分析报告请参考团队博客http://www.cnblogs.com/Z-XML/) 我花了2天的 ...

  3. openldap完整版本搭建记录

    文档信息 目        的:搭建一套完整的OpenLDAP系统,实现账号的统一管理.                     1:OpenLDAP服务端的搭建                   ...

  4. 对学长所谓“改变世界的游戏”《shield star》的运行感想-毛宇部分(完整版本请参考团队博客)

    对于学长项目<shield star>的思考和看法: Ryan Mao ((毛宇)  110616-11061171 试用了一下学长黄杨等人开发的<shield star>游戏 ...

  5. dubbo环境搭建与tomcat集成、DEMO示例、常见问题(最完整版本、带管理控制台、监控中心、zookeeper)

    以windows为例,linux基本相同,开发环境一般linux,个人环境一般windows(如果不开额外vm的话). 示例以dubbo官方自带demo为例子,进行整合和稍加修改测试. 0.dubbo ...

  6. 模拟IE各种版本的方法

    下载360极速浏览器.开启“兼容模式” 默认会是IE7.可以通过控制台(Ctrl + shift + I)调整各个版本

  7. 基于Unity的A星寻路算法(绝对简单完整版本)

    前言 在上一篇文章,介绍了网格地图的实现方式,基于该文章,我们来实现一个A星寻路的算法,最终实现的效果为: 项目源码已上传Github:AStarNavigate 在阅读本篇文章,如果你对于里面提到的 ...

  8. 完整版本的推箱子小游戏,最简单的纯C语言打造

    /* 推箱子小游戏 1.定义绘制样式 用二维数组的方式 2.绘制图像 3.找出当前位置 4.逻辑判断,制造动作 根据数学xy轴的规律,这里使用ij 上移,行轴上升,行数减少 下移,行数下降,函数增加 ...

  9. java多线程分块上传并支持断点续传最新修正完整版本[转]

    package com.test; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.Fi ...

随机推荐

  1. POST请求成功,但接口返回数据不正确

    事件:使用Jmeter做APP的搜索接口测试,请求成功了,但无论keyword是什么“内容”(这里的内容是带引号的哦),接口返回的内容都是:未匹配到搜索结果 排查问题: keyword=“世界” st ...

  2. JS 常用语法

    通常,通过 JavaScript,您需要操作 HTML 元素. 1.通过 id 找到 HTML 元素 2.通过标签名找到 HTML 元素 3.通过类名找到 HTML 元素 提示:通过类名查找 HTML ...

  3. Network-Flow

    //Created by pritry int graph[MAX][MAX]; //原图 int source; //起点,这里为0 int sink; //终点,这里为n-1 int e[MAX] ...

  4. Linux循环

    echo 'for循环' do echo $i done

  5. Centos 7, Torque 单节点部署

    1.准备工作 安装Torque必须首先配置linux主机名称,服务器主机名称大多默认localhost,不建议直接使用localhost. linux主机名称修改地址:http://www.cnblo ...

  6. 3分钟实现小程序唤起微信支付 Laravel教程

    微信支付的接入,如果不使用成熟的开发包,将是巨大的工作量. 依赖 EasyWechat 先在 laravel 项目中依赖 easywechat 这个包 composer require "o ...

  7. 移动端rem.js使用方法

    下面的代码一是我根据rem的使用经验,自己写的一个rem.js,发现很好用,能适用所有移动端h5页面的自适应需求: 代码一: ``` window.onload = function(){ /*720 ...

  8. 14.Idea在html里面的快捷键

    1.IntelliJ-Idea里常用的快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*…*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 C ...

  9. 嵌入式linux实现NAT端口映射

    场景: 1.嵌入式linux系统内已经在2个网卡,分别为eth0(内网物理网卡,ip地址:192.168.1.4)以及ppp1(VPN客户端通过PPTP协议拨号生成的虚拟网卡,ip地址:192.168 ...

  10. 第五节、矩阵分解之LU分解

    一.A的LU分解:A=LU 我们之前探讨过矩阵消元,当时我们通过EA=U将A消元得到了U,这一节,我们从另一个角度分析A与U的关系 假设A是非奇异矩阵且消元过程中没有行交换,我们便可以将矩阵消元的EA ...