(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. jsp include指令标签

    假设须要在JSP页面内某处总体嵌入一个文件,就能够考虑使用这个指令标签. 该指令标签例如以下: <%@ include file ="文件的名字"%> 该指令标签的作用 ...

  2. Android开发之Handler的用法(源码分享)

    Handler主要接受子线程发送的数据, 并用此数据配合主线程更新UI.. 当应用程序启动时.Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发. ...

  3. POJ1251 Jungle Roads 【最小生成树Prim】

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Des ...

  4. Scrum三头猪

    猪和鸡在过去进入业务合作.他们都打算开一家餐厅,它的售价火腿和鸡蛋. 这想了一下时间猪,我用他的肉,鸡只是用它生下的蛋.万一生意失败,我自己的命就没,,没有不论什么影响. 1. Scrum两类关系人 ...

  5. 《Linux Device Drivers》第十四章 Linux 设备型号

    基本介绍 2.6内核设备模型来提供的抽象叙述性描述的一般系统的结构,为了支持各种不同的任务 电源管理和系统关机 用户空间与通信 热插拔设备 设备类型 kobject.kset和子系统 kobject是 ...

  6. extjs_09_定义自己的页面组件

    1.项目截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWRhbV93enM=/font/5a6L5L2T/fontsize/400/fill/I0J ...

  7. SQLite外键

    数据库工具:SQLite Manager(V0.7.7) SQLite版本号:V3.6.19+ SQLite Manager 默认是不开启外键的. 那么怎样,使用它创建一个带有外键的表呢? 一.开启外 ...

  8. java通用抹去魔,在边界行动,擦除补偿

    java通用抹去魔 package org.rui.generics.erasure; public class HasF { public void f(){ System.out.println( ...

  9. How to install IIS 7.5 on Windows 7 using the Command Line

    原文 How to install IIS 7.5 on Windows 7 using the Command Line On Windows Vista, to install IIS 7.0 f ...

  10. 解决Gradle minifyEnabled无法找到错误

    Android studio最新版0.9支持proguard minify,shrinkResources ,尝试了下,提示找不到minifyEnabled方法 Error:(29, 0) Gradl ...