整体展示:

一、全局变量

  1. /*===================玩家参数==========================*/
  2. var myPlane; //英雄对象
  3. var leftbtn = false; //左移动标志
  4. var rightbtn = false; //右移动标志
  5. var topbtn = false; //上移动标志
  6. var bottombtn = false; //下移动标志
  7. var shot = false; //射击标志
  8. //var bulletStatus = false; //子弹
  9. var score = 0; //得分
  10. var killNum = 0; //杀敌数
  11. var shotboom = false; //必杀标志
  12. var pass = 1;//关卡
  13. var mainobj = document.getElementById("main"); //获取游戏div元素
  14. /*===================集合==========================*/
  15. var bulletList = new Array(); //英雄子弹集合
  16. var boomList = new Array(); //必杀集合
  17. var boosList = new Array(); //boos集合
  18. var enemyList = new Array(); //敌机集合
  19. var boosBullet = new Array(); //boos子弹集合
  20. var toolList = new Array(); //道具集合
  21. /*====================计时器=========================*/
  22. var ctrlmove; //英雄移动计时器
  23. var ctrlshot; //英雄射击计时器
  24. var bulletfly; //子弹移动计时器
  25. var createenemy; //敌机生成计时器
  26. var enemymove; //敌机移动计时器
  27. var hitplane; //击中判断计时器
  28. var exist; //判断对象存在与否计时器

二、飞机对象

  1. /**
  2. * 飞机对象
  3. * @param {Object} src 飞机图片路径
  4. * @param {Object} x x坐标
  5. * @param {Object} y y坐标
  6. * @param {Object} speed 飞机移动速度
  7. * @param {Object} blood 飞机血量
  8. */
  9. function createPlane(src, x, y, speed, blood) {
  10. this.planeNode = document.createElement("img"); //飞机图片节点,用于主界面中显示
  11. this.src = src;
  12. this.x = x;
  13. this.y = y;
  14. this.speed = speed;
  15. this.blood = blood;
  16. this.level = 1; //等级
  17. this.boom = 5; //必杀数量
  18. //飞机左移动,需要判断是否飞出主界面左侧,如果移出,则从右边出现
  19. this.leftMove = function() {
  20. if (this.planeNode.style.left == "-60px")
  21. this.planeNode.style.left = "440px";
  22. else {
  23. this.planeNode.style.left=parseInt(this.planeNode.style.left) - this.speed +"px";
  24. }
  25. };
  26. //飞机右移动,需要判断是否飞出界面,飞出则从左边出现
  27. this.rightMove = function() {
  28. if (this.planeNode.style.left == "440px"){
  29. this.planeNode.style.left = "-60px";
  30. }
  31. else {
  32. this.planeNode.style.left=parseInt(this.planeNode.style.left) + this.speed +"px";
  33. }
  34. };
  35. //飞机上移动,当移动到一定值时则不能移动
  36. this.topMove = function() {
  37. if (this.planeNode.style.top == "-10px"){
  38.  
  39. }
  40. else {
  41. this.planeNode.style.top=parseInt(this.planeNode.style.top) - this.speed +"px";
  42. }
  43. };
  44. //飞机下移动,当移动到一定值时,则不能移动
  45. this.botoomMove = function() {
  46. if (this.planeNode.style.top == "600px"){
  47.  
  48. }
  49. else {
  50. this.planeNode.style.top=parseInt(this.planeNode.style.top) + this.speed +"px";
  51. }
  52. };
  53.  
  54. //在页面上创建飞机,设置css属性
  55. this.create = function() {
  56. this.planeNode.src = this.src;
  57. this.planeNode.style.position = "absolute";
  58. this.planeNode.style.left = this.x + "px";
  59. this.planeNode.style.top = this.y + "px";
  60. this.planeNode.style.zIndex = 6;
  61. mainobj.appendChild(this.planeNode);//在游戏界面中添加飞机节点并显示
  62. };
  63. //发射子弹
  64. this.shot = function(){
  65. var bullet1 = new createBullet("img/img_bullet.png",parseInt(this.planeNode.style.left),parseInt(this.planeNode.style.top),this.speed);
  66. bulletList.push(bullet1);
  67. var bullet2 = new createBullet("img/img_bullet.png",parseInt(this.planeNode.style.left)+50,parseInt(this.planeNode.style.top),this.speed);
  68. bulletList.push(bullet2);
  69. }
  70. //释放必杀
  71. this.shotBoom=function(){
  72. if(this.boom>0){
  73. var boom1 = new createBullet("img/daodan1.png",parseInt(this.planeNode.style.left)+20,parseInt(this.planeNode.style.top),this.speed);
  74. boomList.push(boom1);
  75. this.boom--;
  76. }
  77. }
  78.  
  79. this.create();
  80. }

三、敌机对象

  1. /**
  2. * 敌人飞机对象
  3. * @param {Object} src
  4. * @param {Object} x
  5. * @param {Object} y
  6. * @param {Object} speed
  7. * @param {Object} blood
  8. */
  9. function createEnemyPlane(src,x,y,speed,blood){
  10. this.src = src;
  11. this.x = x;
  12. this.y = y;
  13. this.blood = blood;
  14. this.speed = speed;
  15. this.isdead = false; //是否死亡
  16. this.deadtime = 10; //移除时间(计时器时间*10),用于爆炸效果
  17. this.enemyNode = document.createElement("img");
  18.  
  19. this.create = function(){
  20. this.enemyNode.src = this.src;
  21. this.enemyNode.style.position = "absolute";
  22. this.enemyNode.style.left = this.x +"px";
  23. this.enemyNode.style.top = this.y+"px";
  24. this.enemyNode.style.zIndex = 6;
  25. mainobj.appendChild(this.enemyNode);
  26. };
  27.  
  28. this.move = function(){
  29. this.enemyNode.style.top=parseInt(this.enemyNode.style.top) + this.speed +"px";
  30. };
  31.  
  32. this.rightMove=function(){
  33. this.enemyNode.style.left=parseInt(this.enemyNode.style.left) + this.speed +"px";
  34. };
  35.  
  36. this.leftMove=function(){
  37. this.enemyNode.style.left=parseInt(this.enemyNode.style.left) - this.speed +"px";
  38. };
  39.  
  40. this.shot=function(){
  41.  
  42. };
  43. /**
  44. * boos射击
  45. * 很据英雄的level不同,boos的子弹不同
  46. */
  47. this.boosShot=function(){
  48. if(myPlane.level==1){
  49. var bullet = new createBullet("img/wp2.png",parseInt(this.enemyNode.style.left)+22,parseInt(this.enemyNode.style.top),this.speed);
  50. boosBullet.push(bullet);
  51. }
  52. if(myPlane.level==3){
  53. var bullet = new createBullet("img/wp3.png",parseInt(this.enemyNode.style.left)+26,parseInt(this.enemyNode.style.top),this.speed);
  54. boosBullet.push(bullet);
  55. }
  56. };
  57. this.create();
  58.  
  59. }

四、子弹对象

  1. /**
  2. * 子弹对象
  3. * @param {Object} src
  4. * @param {Object} x
  5. * @param {Object} y
  6. * @param {Object} speed
  7. */
  8. function createBullet(src,x,y,speed){
  9. this.src = src;
  10. this.x = x;
  11. this.y = y;
  12. this.speed = speed;
  13. this.ishit = false; //子弹是否击中
  14. this.boomStop = 50; //必杀移动时间
  15. this.boomTime = 100; //必杀移除时间
  16. this.bulletNode = document.createElement("img");
  17.  
  18. this.create = function(){
  19. this.bulletNode.src = src;
  20. this.bulletNode.style.position = "absolute";
  21. this.bulletNode.style.left = this.x+"px";
  22. this.bulletNode.style.top = this.y + "px";
  23. this.bulletNode.style.zIndex = 6;
  24. mainobj.appendChild(this.bulletNode);
  25. };
  26.  
  27. this.move = function(){
  28. this.bulletNode.style.top = parseInt(this.bulletNode.style.top)-this.speed +"px";
  29. };
  30.  
  31. this.downMove=function(){
  32. this.bulletNode.style.top = parseInt(this.bulletNode.style.top)+this.speed +"px";
  33. }
  34. this.create();
  35. }

五、道具对象

  1. function createTool(src,x,y,speed,tooltype){
  2. this.src = src;
  3. this.x = x;
  4. this.y = y;
  5. this.speed = speed;
  6. this.getme = false;
  7. this.tooltype = tooltype;//道具类型 1为加boom 2为加血,
  8. this.toolNode = document.createElement("img");
  9.  
  10. this.create=function(){
  11. this.toolNode.src=this.src;
  12. this.toolNode.style.top=this.y+"px";
  13. this.toolNode.style.left=this.x+"px";
  14. this.toolNode.style.position="absolute";
  15. this.toolNode.style.zIndex=6;
  16. mainobj.appendChild(this.toolNode);
  17. }
  18.  
  19. this.move=function(){
  20. this.toolNode.style.top=parseInt(this.toolNode.style.top) + this.speed +"px";
  21. }
  22. this.create();
  23. }

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

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

以上为今天的第一讲,如需了解更加深入的知识,请大家进入知了堂社区: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. Java多线程缓存器简单实现

    package com.charles.utils; import java.util.HashMap; import java.util.Map; import java.util.concurre ...

  2. oracle-plsql序列问题

    场景:用来汇总工作中数据库的异常问题! 1 问题 序列在会话中未定义 解决: 创建Sequence后直接查询它的当前值(CURRVAL)会出错,要先调用Sequence对象.NEXTVAL,才能查询当 ...

  3. MyBatis 详解(一对一,一对多,多对多)

    1.什么是MyBatis? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且 ...

  4. 【HTML】dl dt dd

    摘要 看到没怎么使用过的html 标签,记录下 定义 dl 类似于 ul ,无任何样式,自定义列表容器, ul 为无序列表容器,ol 为有序列表容器 dt dd 类似于 li ,无任何样式,为帮助实现 ...

  5. YARN笔记——技术点汇总

    目录 · 概况 · 原理 · 资源调度器分类 · YARN架构 · ResourceManager · NodeManager · ApplicationMaster · Container · YA ...

  6. POJ3268 Silver Cow Party Dijkstra最短路

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  7. ida和idr机制分析(盘符分配机制)

    # ida和idr机制分析 ida和idr的机制在我个人看来,是内核管理整数资源的一种方法.在内核中,许多地方都用到了该结构(例如class的id,disk的id),更直观的说,硬盘的sda到sdz的 ...

  8. Python模块----linecache

    Python标准库提供了一个有趣的模块:linecache模块.该模块用来从文件中读取任何的行,并且将这些lines使用缓存进行优化,常见的情况是从个大文件中读取指定的行.不过由于此模块使用内存进行缓 ...

  9. 【NO.1】Jmeter-安装JDK- 配置Jmeter运行的环境 - 是使用Jmeter的前提

    本篇文档是描述:当我们遇到没有安装JDK的Windows系统的电脑.Linux系统的电脑的时候,该怎么来安装JDK. 如果你不安装JDK,那么就没办法开始使用Jmeter. JDK下载地址 http: ...

  10. 【HOSTS相关】前端提供测试模板地址

    在测试接口的时候,首先需要绑定HOSTS,这个文件的位置在这个目录的下面:C:\WINDOWS\system32\drivers\etc,打开hosts文件的方式选择"记事本"就可 ...