(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 の 微信飞机大战的更多相关文章

  1. 用DIV+Css+Jquery 实现的旧版微信飞机大战。

    用jquery 实现的旧版微信飞机大战. 以前一直都是做后台和业务逻辑,前端很少去做, 现在个小游戏. 方向键控制方向,Ctrl 键 放炸弹(当然你的有炸弹,哈哈)! 主要都是用div+Css实现的, ...

  2. 【一】仿微信飞机大战cocos2d-x3.0rc1

    參考 [偶尔e网事] 的 [cocos2d-x入门实战]微信飞机大战  cocos2dx 2.0版本号,偶尔e网事他写的很具体,面面俱到,大家很有必要看下.能够通过以下链接跳转: cocos2d-x入 ...

  3. Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现

    看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...

  4. 用Javascript模拟微信飞机大战游戏

    最近微信的飞机大战非常流行,下载量非常高. 利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化] [此游戏中没有使用HTML5 任何浏览器都可以运行]. 效果图: 原理:利用 ...

  5. 500行代码,教你用python写个微信飞机大战

    这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...

  6. [置顶] 【cocos2d-x入门实战】微信飞机大战之六:子弹层的处理

    这一篇将会处理完子弹层的其他要点. 1.子弹的初始位置 子弹的初始位置在飞机的机头位置,因为飞机在游戏的过程中会随着玩家的触摸而改变其位置,所以,子弹的初始位置只能以当前飞机位置为基准进行添加. CC ...

  7. [置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11730601 不过明眼人一看就知道起飞的不是飞机,是背景,相对运动引起的错觉. 1 ...

  8. [置顶] 【cocos2d-x入门实战】微信飞机大战之四:飞机登场咯

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11757175 昨天收到了电子工业出版社寄过来的<cocos2d-x游戏开发之 ...

  9. 用Swift语言和Sprite Kit复制微信飞机大战游戏

    先上GitHub链接: https://github.com/songrotek/PlaneWar.git 接下来略微解说一下! 这个程序还有点Bug,见谅! 1 说明 游戏採用了Sprite kit ...

随机推荐

  1. 【Java 之 JVM】Java内存结构概述

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWl3dXpoaWxpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  2. OFTP说明

    OFTP (TheOdette File Transfer Protocol,RFC 2204)作为两个商业伙伴中建立EDI连接的一种协议.它由Odette-Organization于1986年创建. ...

  3. Win7设置局域网共享

    Win7设置局域网共享 1. 把win7的guest用户开启. 打开win7的控制面板=>用户账号与安全=>用户账户=>管理其他用户=>点击guest用户,进行开启. 2.解决 ...

  4. Bug记录:微博的Java SDK返回经纬度错误

    现象:美国的坐标点可能会定位到西藏地区-后发现原来负经度经解析后,均变成正的! 源码: private void getGeoInfo(String geo) { StringBuffer value ...

  5. ECharts SSH+JQueryAjax+Json+JSP在数据库中的数据来填充ECharts在

    1导入包.设定SSH框架. 进口JQuery的JS包.<script src="JS/jquery-1.7.1.js"></script> 导入EChart ...

  6. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  7. ftk学习记录(多形式的文章)

    [声明:版权全部.欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 在上周末的博客中,我们谈到了list view,今天能够看看效果图怎样. 假设大家细心一点,能够 ...

  8. WebStorm的compass配置

    在webstorm中配置compass WebStorm是功能强大的前端开发专用IDE,拥有即时编辑(chrome).自动完成.debugger.Emmet.HTML5 支持.JSLint.Less. ...

  9. Task的异步模式

    Task的异步模式 返回该系列目录<基于Task的异步模式--全面介绍> 生成方法 编译器生成 在.NET Framework 4.5中,C#编译器实现了TAP.任何标有async关键字的 ...

  10. ssis package 在调试状态中设置断点,程序 不进入断点 的解决方案

    原文:ssis package 在调试状态中设置断点,程序 不进入断点 的解决方案 针对 SSIS intergation 项目 > 属性 > Debug >Run64bITRunt ...