Html5 の 微信飞机大战
(function () {
var imageUrl = "images/";
//获取画布对象
var c = $("#game-box").get(0);
//手机屏幕自适应
(function () {
$(window).on("resize", resize);
resize();
function resize() {
$(c).attr("height", $(window).height() < 800 ? $(window).height() : 800);
$(c).attr("width", $(window).width() < 480 ? $(window).width() : 480);
}
})();
//创建画布
var cxt = c.getContext("2d");
//图片资源, 存放图片对象
var sources = [];
//新建图片方法
function creatImg(src) {
if (typeof sources[src] != "undefined") {
return sources[src];
}
sources[src] = new Image();
sources[src].src = src;
return sources[src];
}
$(function () {
var images = ['bg.jpg', 'loading1.png', 'loading2.png', 'loading3.png', 'logo.png'];
loadImages(images, loading);
});
function loading() {
var loadingTime = 0;
var refresh = function () {
drawBg();
drawLogo();
load();
loadingTime++;
}
var loadingClock = setInterval(refresh, 1);
function drawBg() {
var bg_img = creatImg("bg.jpg");
var bg_img_width = bg_img.width;
var bg_img_height = bg_img.height;
cxt.drawImage(bg_img, 0, 0, bg_img_width, bg_img_height);
}
function drawLogo() {
var logo_img = creatImg("logo.png");
var logo_width = logo_img.width;
var logo_height = logo_img.height;
var c_width = $(c).width();
var x = (c_width - logo_width) / 2;
var y = 100;
cxt.drawImage(logo_img, x, y, logo_width, logo_height);
}
function load() {
if (loadingTime == 600) {
loadingTime = 0;
}
var pic = creatImg("loading" + (parseInt(loadingTime / 200) + 1) + ".png");
var pic_width = pic.width;
var pic_height = pic.height;
var c_width = $(c).width();
var x = (c_width - pic_width) / 2;
cxt.drawImage(pic, x, 220, pic_width, pic_height);
}
var images = ['cartridge.png', 'die1.png', 'die2.png', 'me.png', 'plain1.png', 'plain2.png', 'plain3.png', 'plain1_die1.png', 'plain1_die2.png', 'plain1_die3.png', 'plain2_die1.png', 'plain2_die2.png', 'plain2_die3.png', 'plain2_die4.png', 'plain3_die1.png', 'plain3_die2.png', 'plain3_die3.png', 'plain3_die4.png', 'plain3_die5.png', 'plain3_die6.png', 'me_die1.png', 'me_die2.png', 'me_die3.png', 'me_die4.png'];
loadImages(images, function () {
main();
clearInterval(loadingClock);
});
}
function loadImages(images, callback) {
var loadedImages = 0;
var numImages = images.length;
for (var i in images) {
sources[images[i]] = new Image();
sources[images[i]].src = imageUrl + images[i];
sources[images[i]].onload = function () {
loadedImages++;
if (loadedImages >= numImages) {
callback();
}
};
}
}
function main() {
var modal = {
"show": function (s) {
$("#modal").find(".content").html(s);
$("#modal").removeClass("hide");
},
"hide": function () {
$("#modal").addClass("hide");
}
};
var bg_img = creatImg("bg.jpg");
var bg_img_width = bg_img.width;
var bg_img_height = bg_img.height;
cxt.drawImage(bg_img, 0, 0, bg_img_width, bg_img_height);
var c_width = $(c).width();
var c_height = $(c).height();
var me = {};
me.status = true;
me.model = creatImg("me.png");
me.width = c_width / 480 * me.model.width;
me.height = me.width / me.model.width * me.model.height;
me.move = function (x, y) {
me.x = x - me.width / 2;
me.y = y - me.height / 2;
var c_width = $(c).width();
var c_height = $(c).height();
me.x = me.x > c_width - me.width ? c_width - me.width : me.x;
me.x = me.x < 0 ? 0 : me.x;
me.y = me.y > c_height - me.height ? c_height - me.height : me.y;
}
me.moveing = function () {
if (!me.status) {
return;
}
cxt.drawImage(me.model, me.x, me.y, me.width, me.height);
}
me.cartridges = [];
me.cartridge = function (x, y) {
var cartridgeModel = creatImg("cartridge.png");
this.model = cartridgeModel;
this.x = x;
this.y = y;
this.width = c_width / 480 * cartridgeModel.width;
this.height = this.width / cartridgeModel.width * cartridgeModel.height;
}
me.attackTime = 0;
me.attack = function () {
if (!me.status) {
return;
}
me.attackTime++;
if (me.attackTime % 20 != 0) {
return;
}
me.attackTime = 0;
var cartridge = new me.cartridge(0, 0);
cartridge.x = me.x + (me.width - cartridge.width) / 2;
cartridge.y = me.y - cartridge.height;
me.cartridges.push(cartridge);
}
me.attacking = function () {
me.attack();
var cartridgeSpeed = 10;
me.cartridges.map(function (cartridge, cartridgeIndex) {
cxt.drawImage(cartridge.model, cartridge.x, cartridge.y, cartridge.width, cartridge.height);
if (cartridge.y <= 0) {
me.cartridges.splice(cartridgeIndex, 1);
}
game.plains.map(function (plain) {
var X = cartridge.x;
var nextY = cartridge.y - cartridgeSpeed;
if (
X > plain.x
&& X < (plain.x + plain.width)
&& nextY < (plain.y + plain.height + plain.speed)
&& cartridge.y >= (plain.y + plain.height)
) {
me.cartridges.splice(cartridgeIndex, 1);
plain.byAttack();
}
});
cartridge.y = cartridge.y - cartridgeSpeed; //子弹向上移动
});
}
me.die = function () {
if (!me.status) {
return;
}
$(c).off("mousemove");
c.removeEventListener("touchmove");
me.status = false;
var dieSpeed = 25;
var x = this.x;
var y = this.y;
var h = this.height;
var w = this.width;
game.plainsDies.push((new die()));
function die() {
var dieTime = 4 * dieSpeed;
this.animationTime = 4 * dieSpeed;
this.call = function () {
if (this.animationTime == 1) {
game.over();
}
var dieModel = creatImg("me_die" + (parseInt((dieTime - this.animationTime) / dieSpeed) + 1) + ".png");
cxt.drawImage(dieModel, x, y, w, h);
this.animationTime--;
}
}
}
var game = {};
game.score = 0;
game.me = me;
game.time = 0;
game.refresh = function () {
game.time += 0.001;
game.bgScroll();
game.addPlain();
game.plainsScroll();
game.me.moveing();
game.me.attacking();
game.plainsDying();
$("#score").html(game.score);
}
game.bgScrollTime = 0;
game.bgScroll = function () {
game.bgScrollTime += 0.5;
if (game.bgScrollTime > bg_img_height) {
game.bgScrollTime = 0;
}
cxt.drawImage(bg_img, 0, game.bgScrollTime - bg_img_height, bg_img_width, bg_img_height);
cxt.drawImage(bg_img, 0, game.bgScrollTime, bg_img_width, bg_img_height);
}
game.plains = [];
game.plainsNum = 0;
game.addPlain = function () {
if (game.bgScrollTime % 60 != 0) {
return;
}
if (game.plainsNum == 25) {
game.plainsNum = 0;
}
game.plainsNum++;
switch (true) {
case game.plainsNum % 13 == 0:
game.plains.push(new plain(3, 0.3));
break;
case game.plainsNum % 6 == 0:
game.plains.push(new plain(2, 0.3));
break;
default:
game.plains.push(new plain(1, 0.3));
break;
}
}
game.plainsScroll = function () {
game.plains.map(function (plain, plainIndex) {
if (!plain.status) {
game.plains.splice(plainIndex, 1);
return;
}
cxt.drawImage(plain.model, plain.x, plain.y, plain.width, plain.height);
if (isCollide(plain)) {
game.me.die();
}
if (plain.y > c_height) {
game.plains.splice(plainIndex, 1);
}
plain.y = plain.y + plain.speed;
});
//推断是否和玩家的飞机碰撞
function isCollide(plain) {
var plainTopLeft = [plain.x, plain.y];
var plainBottomRight = [plain.x + plain.width, plain.y + plain.height];
var meTopLeft = [game.me.x + game.me.width / 3, game.me.y];
var meBottomRight = [game.me.x + (game.me.width * 2 / 3), game.me.y + (game.me.height * 2 / 3)];
var collideTopLeft = [Math.max(plainTopLeft[0], meTopLeft[0]), Math.max(plainTopLeft[1], meTopLeft[1])];
var collideBottomRight = [Math.min(plainBottomRight[0], meBottomRight[0]), Math.min(plainBottomRight[1], meBottomRight[1])];
if (collideTopLeft[0] < collideBottomRight[0] && collideTopLeft[1] < collideBottomRight[1]) {
return true;
}
return false;
}
}
game.plainsDies = [];
game.plainsDying = function () {
game.plainsDies.map(function (plainDie, plainDieIndex) {
if (plainDie.animationTime == 0) {
game.plainsDies.splice(plainDieIndex, 1);
return;
}
plainDie.call();
});
}
game.over = function () {
$(c).removeClass("playing");
clearInterval(game.clock);
modal.show(game.score);
}
game.clear = function () {
game.me.x = ($(c).width() - game.me.width) / 2;
game.me.y = $(c).height() - game.me.height;
game.plains = [];
game.plainsDies = [];
game.plainsNum = 0;
game.time = 0;
game.bgScrollTime = 0;
game.score = 0;
game.me.status = true;
}
game.start = function () {
$(c).addClass("playing");
$(c).on("mousemove", function (e) {
var x = e.clientX - $(this).offset().left;
var y = e.clientY;
me.move(x, y);
});
c.addEventListener("touchmove", function (e) {
e.preventDefault();
var touch = e.targetTouches[0];
me.move(touch.pageX, touch.pageY);
});
modal.hide();
game.clear();
game.clock = setInterval(function () {
game.refresh();
}, 7);
}
game.start();
//飞机类
function plain(type) {
this.type = type;
this.hp; //飞机生命值
this.height;
this.width;
this.maxSpeed;
this.dieTime;
this.status = true; //飞机死了没
var dieSpeed = 25; //死亡动画播放速度
switch (type) {
case 1:
this.hp = 1;
this.score = 1000;
this.modelimg = "plain1.png";
this.maxSpeed = 5;
this.dieTime = dieSpeed * 3;
break;
case 2:
this.hp = 6;
this.score = 8000;
this.modelimg = "plain2.png";
this.maxSpeed = 2;
this.dieTime = dieSpeed * 4;
break;
case 3:
this.hp = 15;
this.score = 30000;
this.modelimg = "plain3.png";
this.maxSpeed = 1.5;
this.dieTime = dieSpeed * 6;
break;
}
this.model = creatImg(this.modelimg);
this.width = c_width / 480 * this.model.width;
this.height = this.width / this.model.width * this.model.height;
this.x = Math.random() * (c_width - this.width);
this.y = -(this.height);
var maxSpeed = game.time > 10 ? 10 : game.time;
this.speed = Math.random() * (maxSpeed - 1) + 1;
this.speed = this.speed < 0.5 ? Math.random() * 0.5 + 0.5 : this.speed;
this.speed = this.speed > this.maxSpeed ? this.maxSpeed : this.speed;
this.die = function () {
var plainType = this.type;
var plainX = this.x;
var plainY = this.y;
var plainW = this.width;
var plainH = this.height;
game.plainsDies.push((new die(this.dieTime)));
function die(dieTime) {
var dieTime = dieTime;
this.animationTime = dieTime;
this.call = function () {
if (this.animationTime <= 0) {
return;
}
var dieModel = creatImg("plain" + plainType + "_die" + (parseInt((dieTime - this.animationTime) / dieSpeed) + 1) + ".png");
cxt.drawImage(dieModel, plainX, plainY, plainW, plainH);
this.animationTime--;
}
}
}
this.byAttack = function () {
this.hp--;
if (this.hp <= 0) {
game.score += this.score;
this.status = false;
this.die();
}
}
}
$("#modal").on("click", "button", function () {
game.start();
});
}
})();
演示出处:请点击打开
Html5 の 微信飞机大战的更多相关文章
- 用DIV+Css+Jquery 实现的旧版微信飞机大战。
用jquery 实现的旧版微信飞机大战. 以前一直都是做后台和业务逻辑,前端很少去做, 现在个小游戏. 方向键控制方向,Ctrl 键 放炸弹(当然你的有炸弹,哈哈)! 主要都是用div+Css实现的, ...
- 【一】仿微信飞机大战cocos2d-x3.0rc1
參考 [偶尔e网事] 的 [cocos2d-x入门实战]微信飞机大战 cocos2dx 2.0版本号,偶尔e网事他写的很具体,面面俱到,大家很有必要看下.能够通过以下链接跳转: cocos2d-x入 ...
- Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现
看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...
- 用Javascript模拟微信飞机大战游戏
最近微信的飞机大战非常流行,下载量非常高. 利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化] [此游戏中没有使用HTML5 任何浏览器都可以运行]. 效果图: 原理:利用 ...
- 500行代码,教你用python写个微信飞机大战
这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...
- [置顶] 【cocos2d-x入门实战】微信飞机大战之六:子弹层的处理
这一篇将会处理完子弹层的其他要点. 1.子弹的初始位置 子弹的初始位置在飞机的机头位置,因为飞机在游戏的过程中会随着玩家的触摸而改变其位置,所以,子弹的初始位置只能以当前飞机位置为基准进行添加. CC ...
- [置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11730601 不过明眼人一看就知道起飞的不是飞机,是背景,相对运动引起的错觉. 1 ...
- [置顶] 【cocos2d-x入门实战】微信飞机大战之四:飞机登场咯
转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11757175 昨天收到了电子工业出版社寄过来的<cocos2d-x游戏开发之 ...
- 用Swift语言和Sprite Kit复制微信飞机大战游戏
先上GitHub链接: https://github.com/songrotek/PlaneWar.git 接下来略微解说一下! 这个程序还有点Bug,见谅! 1 说明 游戏採用了Sprite kit ...
随机推荐
- webBrower控件实现winform和webpage交互
添加WebBrowser控件 private WebBrowser webBrowser1; 引用页面的document对象 HtmlDocument doc = webBrowser1.Docume ...
- 输入n个数和输出调整后的n个数
输入n个数和输出调整后的n个数 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 148 Solved: 118 [Submit][Status][We ...
- NYOJ 14 场地安排
/* 中国标题的含义: 中国的翻译: 标题效果:寻求预定场地的最大数量,只有一个活动可以安排时间 解决问题的思路:然后使用结构数.之后再构建一个排序,排序结束时间活动.然后基于开始时间为大于一个事件的 ...
- 允许Ubuntu14.04"保存"屏幕亮度值
Ubuntu / Debian 该系统有一个共同的问题,也就是说,每个引导.系统会打开你的屏幕亮度调至最高值. 我很奇怪,为什么14.04这一问题的版本号依然不动. 但是,我们可以做一个脚本Ubunt ...
- Apache 2.4虚拟主机配置
托管它指的是多个站点的执行一台机器上 (例如 company1.example.com 和 company2.example.com) . 机能够"基于 IP",即每一个 IP 一 ...
- Eclipse+超快速的模拟器Genymotion开展Android申请书(第一步:安装和配置Genymotion)
一.安装和配置Genymotion (1)因为Eclipse自带SDK模拟器,慢启动,别说 今天给大家介绍一个更快速的模拟器Genymotion (2)第一次去Genymotion在官方网站上注册一个 ...
- 多快好省的做个app开发
从技术经理的角度算一算,如何可以多快好省的做个app [导读]前端时间,一篇“从产品经理的角度算一算,做个app需要多少钱”的文章在网上疯传,可见大家对互联网创业的热情!这次,从一名技术经理的角度再给 ...
- UVA 12538 Version Controlled IDE 解题报告
题意:给三种操作 1.在p位置插入一个字符串. 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 解法:可以用平衡树做,但是不会.后来又听说可一用一个叫ro ...
- HDU1160(LIS)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:求体重下降.速度添加的样例最多有多少个 依据体重降序排一下,然后求速度的最长上升子序列 , ...
- VC和gcc在保证功能static对线程安全的差异变量
VC和gcc不同,不能保证静态变量的线程安全性.这就给我们的程序带来了非常大的安全隐患和诸多不便.这一点应该引起我们的重视!尤其是在构造函数耗时比較长的时候.非常可能给程序带来意想不到的结果.本文从測 ...