整体展示:

一、全局变量

/*===================玩家参数==========================*/
var myPlane; //英雄对象
var leftbtn = false; //左移动标志
var rightbtn = false; //右移动标志
var topbtn = false; //上移动标志
var bottombtn = false; //下移动标志
var shot = false; //射击标志
//var bulletStatus = false; //子弹
var score = 0; //得分
var killNum = 0; //杀敌数
var shotboom = false; //必杀标志
var pass = 1;//关卡
var mainobj = document.getElementById("main"); //获取游戏div元素
/*===================集合==========================*/
var bulletList = new Array(); //英雄子弹集合
var boomList = new Array(); //必杀集合
var boosList = new Array(); //boos集合
var enemyList = new Array(); //敌机集合
var boosBullet = new Array(); //boos子弹集合
var toolList = new Array(); //道具集合
/*====================计时器=========================*/
var ctrlmove; //英雄移动计时器
var ctrlshot; //英雄射击计时器
var bulletfly; //子弹移动计时器
var createenemy; //敌机生成计时器
var enemymove; //敌机移动计时器
var hitplane; //击中判断计时器
var exist; //判断对象存在与否计时器

二、飞机对象

/**
* 飞机对象
* @param {Object} src 飞机图片路径
* @param {Object} x x坐标
* @param {Object} y y坐标
* @param {Object} speed 飞机移动速度
* @param {Object} blood 飞机血量
*/
function createPlane(src, x, y, speed, blood) {
this.planeNode = document.createElement("img"); //飞机图片节点,用于主界面中显示
this.src = src;
this.x = x;
this.y = y;
this.speed = speed;
this.blood = blood;
this.level = 1; //等级
this.boom = 5; //必杀数量
//飞机左移动,需要判断是否飞出主界面左侧,如果移出,则从右边出现
this.leftMove = function() {
if (this.planeNode.style.left == "-60px")
this.planeNode.style.left = "440px";
else {
this.planeNode.style.left=parseInt(this.planeNode.style.left) - this.speed +"px";
}
};
//飞机右移动,需要判断是否飞出界面,飞出则从左边出现
this.rightMove = function() {
if (this.planeNode.style.left == "440px"){
this.planeNode.style.left = "-60px";
}
else {
this.planeNode.style.left=parseInt(this.planeNode.style.left) + this.speed +"px";
}
};
//飞机上移动,当移动到一定值时则不能移动
this.topMove = function() {
if (this.planeNode.style.top == "-10px"){ }
else {
this.planeNode.style.top=parseInt(this.planeNode.style.top) - this.speed +"px";
}
};
//飞机下移动,当移动到一定值时,则不能移动
this.botoomMove = function() {
if (this.planeNode.style.top == "600px"){ }
else {
this.planeNode.style.top=parseInt(this.planeNode.style.top) + this.speed +"px";
}
}; //在页面上创建飞机,设置css属性
this.create = function() {
this.planeNode.src = this.src;
this.planeNode.style.position = "absolute";
this.planeNode.style.left = this.x + "px";
this.planeNode.style.top = this.y + "px";
this.planeNode.style.zIndex = 6;
mainobj.appendChild(this.planeNode);//在游戏界面中添加飞机节点并显示
};
//发射子弹
this.shot = function(){
var bullet1 = new createBullet("img/img_bullet.png",parseInt(this.planeNode.style.left),parseInt(this.planeNode.style.top),this.speed);
bulletList.push(bullet1);
var bullet2 = new createBullet("img/img_bullet.png",parseInt(this.planeNode.style.left)+50,parseInt(this.planeNode.style.top),this.speed);
bulletList.push(bullet2);
}
//释放必杀
this.shotBoom=function(){
if(this.boom>0){
var boom1 = new createBullet("img/daodan1.png",parseInt(this.planeNode.style.left)+20,parseInt(this.planeNode.style.top),this.speed);
boomList.push(boom1);
this.boom--;
}
} this.create();
}

三、敌机对象

/**
* 敌人飞机对象
* @param {Object} src
* @param {Object} x
* @param {Object} y
* @param {Object} speed
* @param {Object} blood
*/
function createEnemyPlane(src,x,y,speed,blood){
this.src = src;
this.x = x;
this.y = y;
this.blood = blood;
this.speed = speed;
this.isdead = false; //是否死亡
this.deadtime = 10; //移除时间(计时器时间*10),用于爆炸效果
this.enemyNode = document.createElement("img"); this.create = function(){
this.enemyNode.src = this.src;
this.enemyNode.style.position = "absolute";
this.enemyNode.style.left = this.x +"px";
this.enemyNode.style.top = this.y+"px";
this.enemyNode.style.zIndex = 6;
mainobj.appendChild(this.enemyNode);
}; this.move = function(){
this.enemyNode.style.top=parseInt(this.enemyNode.style.top) + this.speed +"px";
}; this.rightMove=function(){
this.enemyNode.style.left=parseInt(this.enemyNode.style.left) + this.speed +"px";
}; this.leftMove=function(){
this.enemyNode.style.left=parseInt(this.enemyNode.style.left) - this.speed +"px";
}; this.shot=function(){ };
/**
* boos射击
* 很据英雄的level不同,boos的子弹不同
*/
this.boosShot=function(){
if(myPlane.level==1){
var bullet = new createBullet("img/wp2.png",parseInt(this.enemyNode.style.left)+22,parseInt(this.enemyNode.style.top),this.speed);
boosBullet.push(bullet);
}
if(myPlane.level==3){
var bullet = new createBullet("img/wp3.png",parseInt(this.enemyNode.style.left)+26,parseInt(this.enemyNode.style.top),this.speed);
boosBullet.push(bullet);
}
};
this.create(); }

四、子弹对象

/**
* 子弹对象
* @param {Object} src
* @param {Object} x
* @param {Object} y
* @param {Object} speed
*/
function createBullet(src,x,y,speed){
this.src = src;
this.x = x;
this.y = y;
this.speed = speed;
this.ishit = false; //子弹是否击中
this.boomStop = 50; //必杀移动时间
this.boomTime = 100; //必杀移除时间
this.bulletNode = document.createElement("img"); this.create = function(){
this.bulletNode.src = src;
this.bulletNode.style.position = "absolute";
this.bulletNode.style.left = this.x+"px";
this.bulletNode.style.top = this.y + "px";
this.bulletNode.style.zIndex = 6;
mainobj.appendChild(this.bulletNode);
}; this.move = function(){
this.bulletNode.style.top = parseInt(this.bulletNode.style.top)-this.speed +"px";
}; this.downMove=function(){
this.bulletNode.style.top = parseInt(this.bulletNode.style.top)+this.speed +"px";
}
this.create();
}

五、道具对象

function createTool(src,x,y,speed,tooltype){
this.src = src;
this.x = x;
this.y = y;
this.speed = speed;
this.getme = false;
this.tooltype = tooltype;//道具类型 1为加boom 2为加血,
this.toolNode = document.createElement("img"); this.create=function(){
this.toolNode.src=this.src;
this.toolNode.style.top=this.y+"px";
this.toolNode.style.left=this.x+"px";
this.toolNode.style.position="absolute";
this.toolNode.style.zIndex=6;
mainobj.appendChild(this.toolNode);
} this.move=function(){
this.toolNode.style.top=parseInt(this.toolNode.style.top) + this.speed +"px";
}
this.create();
}

其实在这些对象中,很多代码都是类似的,只要能实现一个对象,在仔细想一想其他对象的特性,结合下代码很快能实现。

 下一讲,则会实现各种处理方法了,让游戏活起来。

以上为今天的第一讲,如需了解更加深入的知识,请大家进入知了堂社区:http://www.zhiliaotang.com/portal.php

[知了堂学习笔记]_纯JS制作《飞机大战》游戏_第2讲(对象的实现及全局变量的定义)的更多相关文章

  1. [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第1讲(实现思路与游戏界面的实现)

    整体效果展示: 一.实现思路 如图,这是我完成该项目的一个逻辑图,也是一个功能模块完成的顺序图. 游戏界面的完成 英雄飞机对象实现,在实现发射子弹方法过程中,又引出了子弹对象并实现.在此时,英雄飞机能 ...

  2. 【知了堂学习笔记】java 自定义异常

    java 常见异常种类(Java Exception): 算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCas ...

  3. [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第3讲(逻辑方法的实现)

    整体展示: 上一讲实现了诸多对象,这次我们就需要实现许多逻辑方法,如控制飞机移动,判断子弹击中敌机,敌机与英雄飞机相撞等等.并且我们在实现这些功能的时候需要计时器去调用这些方法.setInterval ...

  4. [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)

    一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...

  5. [知了堂学习笔记]_牵线Eclipse和Tomcat第二篇 —— 安装Tomcat&&添加Tomcat到Eclipse

    来了来了~~~~~我们的"织女"--Tomcat来了,牛郎们等急了吧!哈哈! 一.安装Tomcat 下载地址:http://tomcat.apache.org/download-7 ...

  6. [知了堂学习笔记]_牵线Eclipse和Tomcat第一篇 —— 配置Java环境变量&&安装eclipse

    一.先给他们提供一个"浪漫的"环境,比如传说中的"鹊桥"--java环境变量.哈哈! 配置java环境变量. 下载jdk,根据自己电脑的版本和操作位数选择不同的 ...

  7. [知了堂学习笔记]_ajax的两种使用方式

    一.Ajax概述 1.什么是同步,什么是异步 同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待 卡死状态 异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都 ...

  8. 【知了堂学习笔记】java web 简单的登录

    最近皮皮潇在学习java web,刚接触了简单的东西,所以今天给大家带来一个简单的登录实现. 页面: 页面代码: <%@ page language="java" conte ...

  9. [知了堂学习笔记]_JSON数据操作第1讲(初识JSON)

    一.认识JSON 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式..它基于 ECMAScript (w3c制定的js规 ...

随机推荐

  1. 64位系统下8G内存仅使用到4G问题的解决方法

    笔记本:联想E46G 当前bios版本:25CN32WW 内存:DDR3 133 4G × 2 问题:bios信息显示8G,win7和ubuntu 在64位下使用情况仅4G 准备工作1:bios版本和 ...

  2. 使用Fiddler调试手机端页面请求/抓包

    简介 Fiddler作为一个强大的抓包工具,也是非常强大的http(s)协议分析工具,我们通常用它跟踪请求,PC端使用这里暂不做介绍(这里前提是熟悉PC端的使用),使用很简单. 那么我们如何来用它来跟 ...

  3. ES6 变量、常量声明总结

    较之前ES5,新颁布在声明上有改变 一.var  对比  let 1.作用域不同 let只在命令所在的代码块 {} 里有效 ES5只有全局作用域和函数作用域,没有块级作用域,带来很多不合理的场景,比如 ...

  4. Java基础之数据类型、内存、修饰符、代码块

    Java 数据类型 基本数据类型 数值:int.short.long 字符:char 布尔:boolean 引用数据类型 class(类) interface(接口) 数组[] 所占字节数(1 byt ...

  5. 枪战Maf[POI2008]

    题目描述 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人也不同. 输入 输入n人 ...

  6. 结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程

    1.bootstrap-fileinpu的简单介绍 在前面的随笔,我介绍了Bootstrap-table表格插件的具体项目应用过程,本篇随笔介绍另外一个Bootstrap FieInput插件的使用, ...

  7. C++标准库string

    C++标准库string 定义和初始化 string s1 默认初始化,s1是一个空串 string s2(s1) s2是s1的副本 string s2 = s1 等价于s2(s1),s2是s1的副本 ...

  8. 【思维】【水】 南阳oj 喷水装置(一)

    描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1& ...

  9. 【机器学习PAI实践二】人口普查统计

    一.背景 感谢大家关注玩转数据系列文章,我们希望通过在阿里云机器学习平台上提供demo数据并搭建相关的实验流程的方式来帮助大家学习如何通过算法来挖掘数据中的价值.本系列文章包含详细的实验流程以及相关的 ...

  10. css 定位功能position

    Static 定位 HTML元素的默认值,即没有定位,元素出现在正常的流中.静态定位的元素不会受到top, bottom, left, right影响. 相对定位Relative相对定位元素的定位是相 ...