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. jsonp的作用

    jsonp(即JSON with padding),也就是json填充. 背景: json格式的数据对比xml格式的数据,性能上已经有了很大的提升.但是json可以被本地执行仍然会导致几个重要的性能问 ...

  2. java 循环document 通用替换某个字符串或特殊字符

    document 生成xml时 报错 XML-20100: (Fatal Error) Expected ';'.  查了半天发现是 特殊字符 & 不能直接转出,需要进行转换,因为是通用方法很 ...

  3. MySQL+Keepalived实现主主高可用方案

    Mysql主主高可用方案 master配置 [root@master ~]# yum -y install keepalived [root@master ~]# vim /etc/keepalive ...

  4. Eclipse(含STS)安装插件/软件、更新

    安装方式 Eclipse安装插件的三种方式 直接复制安装 离线下载好插件,通常去这个插件的官网去找,就是在线安装的地址?如testng可以去http://beust.com/eclipse即http: ...

  5. eas更改用户组织范围和业务组织范围

    表: T_PM_OrgRangeIncludeSubOrg 10 20 30 分别代表 业务组织 行政组织 以及管辖组织.查行政组织,

  6. 11.6 【Linq】分组和延续

    11.6.1 使用 group...by 子句进行分组 class Program { static void Main(string[] args) { var query = from defec ...

  7. [luogu4251 SCOI2015] 小凸玩矩阵 (二分图最大匹配)

    传送门 Description Input Output 输出包含一行,为选出的 n 个数中第 k 大数的最小值. Sample Input 输入样例1: 2 3 1 1 2 4 2 4 1 输入样例 ...

  8. Java核心技术读书笔记01

    Volume I Chapter 1  An Introduction to Java  • 1.1 Java as a Programming Platform• 1.2 The Java ‘Whi ...

  9. 一键安装LNMP(适合centos7)

    1.准备工作,下载源码包 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar ...

  10. CodeForces - 445A - DZY Loves Chessboard

    先上题目: A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input ...