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. Oracle中REGEXP_SUBSTR函数

    Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20',' ...

  2. Math工具类的使用

    1.包:java.lang  不需要导包 2.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 特点: 该类中的方法都是静态方法,所以可以直接使用类名.方法名(实参)调 ...

  3. Day4 循环结构

    for-in循环 如果明确的知道循环执行的次数或者是要对一个容器进行迭代(后面会讲到),那么我们推荐使用for-in循环,例如下面代码中计算$\sum_{n=1}^{100}n$. range函数用法 ...

  4. Codeforces Round #548 (Div. 2) A. Even Substrings

    You are given a string 

  5. swift UITableViewCell 中的单选控制样式

    我昨天在网上找了一晚上的资料,但是大多都是OC得语法,swift资料实在是太少了,使得我这个刚入门swift的彩笔好不吃力,后面一直各种翻阅资料,终于让我找到了 visibleCells 这个方法,直 ...

  6. 33.bulk json格式的理解

    bulk json格式的理解 一.常规格式 按常规理解,bulk中json格式可以是以下方式 [{ "action": { }, "data": { } }] ...

  7. JAVA关键技术

    通用技术方面 MVC 1)概念 MVC是一个架构模式,它分离了表现与交互.它被分为三个核心部件:模型-model.视图-view.控制器-controller 2)工作原理 所有的终端用户请求被发送到 ...

  8. 1.3 eclipse中配置Tomcat

    下载并成功安装Eclipse和Tomcat():   打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 步骤阅读 3 找到Server下方的Runtime Envi ...

  9. java构造函数重载this(true)

    看storm的代码的时候,发现这样一句java代码, 很是不理解 google之后,发现原来是java语法中,构造函数重载,this()调用的其实就是 构造函数.This is constructor ...

  10. [Cypress] Wait for XHR Responses in a Cypress Test

    When testing interactions that require asynchronous calls, we’ll need to wait on responses to make s ...