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. vs2008 打开项目 无法读取项目文件

    卸载vs2015之后 出现问题 C:\Windows\SysWOW64\regedit.exe 64系统运行这个 删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MS ...

  2. Docker 数据卷重复挂载测试

    没想到一年没写博客了,这中间都是记在自己的笔记本上,大部分网上都有,这个好像没有,所以发上来吧! 本文是测试Docker容器(相同目录/父子目录)同时挂载到宿主机(同目录/不同目录)时的情况,废话少说 ...

  3. HTML 5语义化标签

    HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式的id和class形容这块内容的意义).这些标 ...

  4. 算法87-----DAG有向无环图的拓扑排序

    一.题目:课程排表---210 课程表上有一些课,是必须有修学分的先后顺序的,必须要求在上完某些课的情况下才能上下一门.问是否有方案修完所有的课程?如果有的话请返回其中一个符合要求的路径,否则返回[] ...

  5. [luogu4161 SCOI2009]游戏 (DP)

    传送门 Solution 可以发现实际上是把n分为几个循环节,然后找循环节的\(lcm\)是这次的排数 而\(lcm\)必然是一些最高次幂的质数的成积,那么就dp求一下所有情况就好了 PS:注意并不是 ...

  6. Bootstrap关于表单(二):水平表单

    Bootstrap框架默认的表单是垂直显示风格,但很多时候我们需要的水平表单风格(标签居左,表单控件居右) 在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件: 1.在<form ...

  7. linux 中使用iptables 防止ddocs及cc攻击配置 。

    #防止SYN攻击,轻量级预防 iptables -N syn-floodiptables -A INPUT -p tcp –syn -j syn-floodiptables -I syn-flood ...

  8. SLES documentation

    https://www.suse.com/documentation/sles11/book_sle_admin/data/sec_basicnet_yast.html

  9. mysql-windows修改root密码

    安装mysql完成后 直接进入方式 mysql -u root -p 设置mysql的root密码 mysql -u root -p password 8888

  10. javascript--闭包与this

    理解javascript中的闭包与this http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html http: ...