基于phaser引擎的JS打飞机闯关答题小游戏

基于Phaser的游戏设计了一款结合党史知识的打飞机小游戏,玩家可通过积分解锁不同难度关卡,答错可复活答题。游戏包含基本逻辑设计、碰撞检测和资源加载,旨在寓教于乐。

游戏设计逻辑

主要是基于phaser游戏引擎做的打飞机游戏为主的闯关答题小游戏。通过打飞机获得积分,根据积分的多少设置了不同关卡,关卡的敌人难度会不断上升,同时一旦达到一定的积分就会触发必答题弹出,玩家必须答对必答题才能继续闯关。

而主要目的是为了寓教于乐传播党史知识,所以对于被敌机击杀后,我们提供答题复活的机会,而且打错了可以继续答,直到答对为止。

其中在游戏设计上,主要根据常规的phaser小游戏格式,先设计一个基本的游戏环境,然后将各个组件设计成类的形式,设置初始值,然后设计碰撞检测等交互功能,设计各个加载的页面和跳转关系,加载图片,bgm等资源文件,然后在主逻辑段中不断更新数据即可。

游戏截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

开源项目下载

https://download.youkuaiyun.com/download/david2000999/19427597

下载后直接解压,使用浏览器打开index.html即可直接运行

源代码

index.html

<!DOCTYPE html> 
<head> 
	<meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
	<title>互联网侦察-飞机大战</title>
    <script src="js/phaser.min.js"></script>
    <script src="js/main.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      canvas {
        margin: 0 auto;
      }
      .logo {
        position: absolute;
        left: -1000px;
        top: -1000px;
      }
      .share {
        position: absolute;
        width: 100%;
        height: 100%;
        display: none;
        z-index: 2;
      }
      .share img {
        position: absolute;
      }
      .share .sharepng {
        width: 100%;
        height: 100%;
        top: 0px;
        left: 0px;
      }
      .game {
        position: absolute;
        width: 100%;
        height: 100%;
      }
      .iframe {
         height: 400;
         width: 240;
      }
    </style>
</head>
<body>
  <div class="logo">
    <img src="http://game.webxinxin.com/plane/assets/logo.jpg" alt="" />
  </div>
  <div id="share" class="share">
    <img class="sharepng" src="http://game.webxinxin.com/plane/assets/share.png" alt="" />
    <img class="closebtn" src="http://game.webxinxin.com/plane/assets/close.png" alt="" onclick="onCloseShare();" />
  </div>
  <div id="game" class="game"></div>
  
</body>

main.js

/***********************/

function Enemy(config) {
  this.init = function() {
    this.enemys = game.add.group();
    this.enemys.enableBody = true;
    this.enemys.createMultiple(config.selfPool, config.selfPic);
    this.enemys.setAll('outOfBoundsKill', true);
    this.enemys.setAll('checkWorldBounds', true);
    // 敌人的子弹
    this.enemyBullets = game.add.group();
    this.enemyBullets.enableBody = true;
    this.enemyBullets.createMultiple(config.bulletsPool, config.bulletPic);
    this.enemyBullets.setAll('outOfBoundsKill', true);
    this.enemyBullets.setAll('checkWorldBounds', true);
    // 敌人的随机位置范围
    this.maxWidth = game.width - game.cache.getImage(config.selfPic).width;
    // 产生敌人的定时器
    game.time.events.loop(Phaser.Timer.SECOND * config.selfTimeInterval, this.generateEnemy, this);
    // 敌人的爆炸效果
    this.explosions = game.add.group();
    this.explosions.createMultiple(config.explodePool, config.explodePic);
    this.explosions.forEach(function(explosion) {
      explosion.animations.add(config.explodePic);
    }, this);
  }
  // 产生敌人
  this.generateEnemy = function() {
    var e = this.enemys.getFirstExists(false);
    if(e) {
      e.reset(game.rnd.integerInRange(0, this.maxWidth), -game.cache.getImage(config.selfPic).height);
      e.life = config.life;
      e.body.velocity.y = config.velocity;
    }
  }
  // 敌人开火
  this.enemyFire = function() {
    this.enemys.forEachExists(function(enemy) {
			var bullet = this.enemyBullets.getFirstExists(false);
      if(bullet) {
        if(game.time.now > (enemy.bulletTime || 0)) {
          bullet.reset(enemy.x + config.bulletX, enemy.y + config.bulletY);
          bullet.body.velocity.y = config.bulletVelocity;
          enemy.bulletTime = game.time.now + config.bulletTimeInterval;
        }
      }
		}, this);
  };
  // 打中了敌人
  this.hitEnemy = function(myBullet, enemy) {
    try {
      config.firesound.play();
    } catch(e) {}
    myBullet.kill();
    enemy.life--;
    if(enemy.life <= 0) {
      try {
        config.crashsound.play();
      } catch(e) {}
      enemy.kill();
      var explosion = this.explosions.getFirstExists(false);
      explosion.reset(enemy.body.x, enemy.body.y);
      explosion.play(config.explodePic, 30, false, true);
      score += config.score;
      config.game.updateText();
    }
  };
}

/***********************/

var game = new Phaser.Game(240, 400, Phaser.CANVAS, 'game');

game.States = {};

// 分数
//var score = 0;

// 生成Title
var makeTitle = function(score) {
  if(score < 1000) {
    return "简版飞机大战,还挺难的,我才" + score + "分,你能得多少分呢?";
  } else {
    return "简版飞机大战,我是天才,得了" + score + "分,你能得多少分呢?";
  }
}

// 关闭分享
var onCloseShare = function() {
  document.getElementById('share').style.display = 'none';
};

game.States.boot = function() {
  this.preload = function() {
    if(typeof(GAME) !== "undefined") {
      this.load.baseURL = GAME + "/";
    }
    if(!game.device.desktop){
      this.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT;
      this.scale.forcePortrait = true;
      this.scale.refresh();
    }
    game.load.image('loading', 'assets/preloader.gif');
  };
  this.create = function() {
    game.state.start('preload');
  };
};

game.States.preload = function() {
  this.preload = function() {
    var preloadSprite = game.add.sprite(10, game.height/2, 'iloadng');
    game.load.setPreloadSprite(preloadSprite);
    game.load.image('background', 'assets/bg.jpg');
    game.load.image('background1', 'assets/dang.png');
    
    
    //game.load.image('background1', 'assets/t01.png');
    game.load.image('background2', 'assets/rule.png');
    game.load.image('background3', 'assets/bg03.png');
    game.load.image('last', 'assets/last.png');

    game.load.image('t01', 'assets/t01.png');
    game.load.image('t02', 'assets/t02.png');
    game.load.image('t03', 'assets/t03.png');
    game.load.image('t04', 'assets/t04.png');
    game.load.image('t05', 'assets/t05.png');
    game.load.image('t06', 'assets/t06.png');
    game.load.image('t07', 'assets/t07.png');
    game.load.image('t08', 'assets/t08.png');
    game.load.image('t09', 'assets/t09.png');
    game.load.image('t10', 'assets/t10.png');
    game.load.image('A', 'assets/A.png');
    game.load.image('B', 'assets/B.png');
    game.load.image('C', 'assets/C.png');
    game.load.image('D', 'assets/D.png');

    game.load.image('restart', 'assets/restart.png');
    game.load.image('start', 'assets/start.png');
    game.load.image('relieve', 'assets/relieve.png');

    game.load.image('copyright', 'assets/copyright.png');
    game.load.spritesheet('myplane', 'assets/myplane.png', 40, 40, 4);
    game.load.spritesheet('startbutton', 'assets/startbutton.png', 100, 40, 2);
    //game.load.spritesheet('startbutton', 'assets/start.png', 100, 40, 2);

    game.load.spritesheet('replaybutton', 'assets/replaybutton.png', 80, 30, 2);
    game.load.spritesheet('sharebutton', 'assets/sharebutton.png', 80, 30, 2);
    game.load.image('mybullet', 'assets/mybullet.png');
    game.load.image('bullet', 'assets/bullet.png');
    game.load.image('enemy1', 'assets/enemy1.png');
    game.load.image('enemy2', 'assets/enemy2.png');
    game.load.image('enemy3', 'assets/enemy3.png');
    game.load.image('enemy4', 'assets/enemy4.png');
    game.load.image('enemy5', 'assets/enemy5.png');
    game.load.spritesheet('explode1', 'assets/explode1.png', 20, 20, 3);
    game.load.spritesheet('explode2', 'assets/explode2.png', 30, 30, 3);
    game.load.spritesheet('explode3', 'assets/explode3.png', 50, 50, 3);
    game.load.spritesheet('myexplode', 'assets/myexplode.png', 40, 40, 3);
    game.load.image('award', 'assets/award.png');
    game.load.audio('normalback', 'assets/normalback.mp3');
    game.load.audio('playback', 'assets/playback.mp3');
    game.load.audio('fashe', 'assets/fashe.mp3');
    game.load.audio('crash1', 'assets/crash1.mp3');
    game.load.audio('crash2', 'assets/crash2.mp3');
    game.load.audio('crash3', 'assets/crash3.mp3');
    game.load.audio('error', 'assets/error.mp3');
    game.load.audio('ao', 'assets/ao.mp3');
    game.load.audio('pi', 'assets/pi.mp3');
    game.load.audio('deng', 'assets/deng.mp3');
  };
  this.create = function() {
    game.state.start('first');
  };
};

game.States.first = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background1');
    //bg.scale.set(0.5);
   
    // 开始按钮
    this.startbutton = game.add.button(70, 320, 'start', this.onStartClick, this, 1, 1, 0);
    //this.startbutton = game.add.button(70, 320, 'startbutton', this.onStartClick);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
  };
  this.onStartClick = function() {
    game.state.start('second');
    this.normalback.stop();
  };
};

game.States.second = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background2');
   
   
    // 开始按钮
    this.startbutton = game.add.button(70, 250, 'start', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
  };
  this.onStartClick = function() {
    game.state.start('main');
    this.normalback.stop();
  };
};

game.States.main = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background');
    
    // 我的飞机
    this.myplane = game.add.sprite(100, 100, 'myplane');
    this.myplane.animations.add('fly');
    this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.startbutton = game.add.button(70, 250, 'startbutton', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    //属性设置
    score=0;
    flag1=0;flag2=0;flag3=0;flag4=0;flag5=0;
    flag6=0;flag7=0;flag8=0;flag9=0;flag10=0;
    flag11=0;flag12=0;g1=0;g2=0;g3=0;
    b1=0;b2=1;b3=1;
  };
  this.onStartClick = function() {
    game.state.start('start');
    this.normalback.stop();
  };
};


game.States.start = function() {
  this.create = function() {
    // 物理系统
    game.physics.startSystem(Phaser.Physics.ARCADE);
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background');
    bg.autoScroll(0, 20);
    // 我的飞机
    this.myplane = game.add.sprite(100, 100, 'myplane');
    this.myplane.animations.add('fly');
    this.myplane.animations.play('fly', 12, true);
    game.physics.arcade.enable(this.myplane);
    this.myplane.body.collideWorldBounds = true;
    this.myplane.level = 2;
    // 动画
    var tween = game.add.tween(this.myplane).to({y: game.height - 40}, 1000, Phaser.Easing.Sinusoidal.InOut, true);
    tween.onComplete.add(this.onStart, this);
    // 背景音乐
    this.playback = game.add.audio('playback', 0.2, true);
    this.playback.play();
    // 开火音乐
    this.pi = game.add.audio('pi', 1, false);
    // 打中敌人音乐
    this.firesound = game.add.audio('fashe', 5, false);
    // 爆炸音乐
    this.crash1 = game.add.audio('crash1', 10, false);
    this.crash2 = game.add.audio('crash2', 10, false);
    this.crash3 = game.add.audio('crash3', 20, false);
    // 挂了音乐
    this.ao = game.add.audio('ao', 10, false);
    // 接到了奖音乐
    this.deng = game.add.audio('deng', 10, false);
  };
  this.onStart = function() {
    // 我的子弹
    this.mybullets = game.add.group();
    this.mybullets.enableBody = true;
    this.mybullets.createMultiple(50, 'mybullet');
    this.mybullets.setAll('outOfBoundsKill', true);
    this.mybullets.setAll('checkWorldBounds', true);
    this.myStartFire = true;
    this.bulletTime = 0;
    // 我的飞机允许拖拽
    this.myplane.inputEnabled = true;
    this.myplane.input.enableDrag(false);
    // 奖
    this.awards = game.add.group();
    this.awards.enableBody = true;
    this.awards.createMultiple(1, 'award');
    this.awards.setAll('outOfBoundsKill', true);
    this.awards.setAll('checkWorldBounds', true);
    this.awardMaxWidth = game.width - game.cache.getImage('award').width;
    game.time.events.loop(Phaser.Timer.SECOND * 30, this.generateAward, this);
    // 分数
    var style = {font: "16px Arial", fill: "#ff0000"};
    if(b1==0){this.text = game.add.text(0, 0, "第一关Score: "+score, style);}
    else if(b2==0){this.text = game.add.text(0, 0, "第二关Score: "+score, style);}
    else if(b3==0){this.text = game.add.text(0, 0, "第三关Score: "+score, style);}
    else {this.text = game.add.text(0, 0, "第四关Score: "+score, style);}
    
    //score = 0;
    // 敌机
    var enemyTeam = {
      enemy1: {
        game: this,
        selfPic: 'enemy1',
        bulletPic: 'bullet',
        explodePic: 'explode1',
        selfPool: 10,
        bulletsPool: 50,
        explodePool: 10,
        life: 2,
        velocity: 60,
        bulletX: 9,
        bulletY: 20,
        bulletVelocity: 200,
        selfTimeInterval: 2,
        bulletTimeInterval: 1000,
        score: 10,
        firesound: this.firesound,
        crashsound: this.crash1
      },
      enemy2: {
        game: this,
        selfPic: 'enemy2',
        bulletPic: 'bullet',
        explodePic: 'explode2',
        selfPool: 10,
        bulletsPool: 50,
        explodePool: 10,
        life: 3,
        velocity: 40,
        bulletX: 13,
        bulletY: 30,
        bulletVelocity: 250,
        selfTimeInterval: 3,
        bulletTimeInterval: 1200,
        score: 20,
        firesound: this.firesound,
        crashsound: this.crash2
      },
      enemy3: {
        game: this,
        selfPic: 'enemy3',
        bulletPic: 'bullet',
        explodePic: 'explode3',
        selfPool: 5,
        bulletsPool: 25,
        explodePool: 5,
        life: 15,
        velocity: 30,
        bulletX: 22,
        bulletY: 50,
        bulletVelocity: 300,
        selfTimeInterval: 10,
        bulletTimeInterval: 1500,
        score: 50,
        firesound: this.firesound,
        crashsound: this.crash3
      },

      enemy4: {
        game: this,
        selfPic: 'enemy4',
        bulletPic: 'bullet',
        explodePic: 'explode3',
        selfPool: 10,
        bulletsPool: 25,
        explodePool: 5,
        life: 5,
        velocity: 45,
        bulletX: 22,
        bulletY: 50,
        bulletVelocity: 300,
        selfTimeInterval: 10,
        bulletTimeInterval: 1500,
        score: 40,
        firesound: this.firesound,
        crashsound: this.crash3
      },

      enemy5: {
        game: this,
        selfPic: 'enemy5',
        bulletPic: 'bullet',
        explodePic: 'explode3',
        selfPool: 15,
        bulletsPool: 25,
        explodePool: 5,
        life: 5,
        velocity: 70,
        bulletX: 22,
        bulletY: 50,
        bulletVelocity: 300,
        selfTimeInterval: 10,
        bulletTimeInterval: 1500,
        score: 40,
        firesound: this.firesound,
        crashsound: this.crash3
      }
    }
    this.enemy1 = new Enemy(enemyTeam.enemy1);
    this.enemy1.init();
    this.enemy2 = new Enemy(enemyTeam.enemy2);
    this.enemy2.init();
    this.enemy3 = new Enemy(enemyTeam.enemy3);
    this.enemy3.init();
    this.enemy4 = new Enemy(enemyTeam.enemy4);
    this.enemy4.init();
    this.enemy5 = new Enemy(enemyTeam.enemy5);
    this.enemy5.init();
  };
  // 产生一个奖
  this.generateAward = function() {
    var award = this.awards.getFirstExists(false);
    if(award) {
      award.reset(game.rnd.integerInRange(0, this.awardMaxWidth), -game.cache.getImage('award').height);
      award.body.velocity.y = 500;
    }
  };
  // 自己开火
  this.myFireBullet = function() {
    if(this.myplane.alive && game.time.now > this.bulletTime) {
      try {
        this.pi.play();
      } catch(e) {}
      var bullet;
      bullet = this.mybullets.getFirstExists(false);
      if(bullet) {
        bullet.reset(this.myplane.x + 16, this.myplane.y - 15);
        bullet.body.velocity.y = -400;
        this.bulletTime = game.time.now + 200;
      }
      if(this.myplane.level >= 2) {
        bullet = this.mybullets.getFirstExists(false);
        if(bullet) {
          bullet.reset(this.myplane.x + 16, this.myplane.y - 15);
          bullet.body.velocity.y = -400;
          bullet.body.velocity.x = -40;
          this.bulletTime = game.time.now + 200;
        }
        bullet = this.mybullets.getFirstExists(false);
        if(bullet) {
          bullet.reset(this.myplane.x + 16, this.myplane.y - 15);
          bullet.body.velocity.y = -400;
          bullet.body.velocity.x = 40;
          this.bulletTime = game.time.now + 200;
        }
      }
      if(this.myplane.level >= 3) {
        bullet = this.mybullets.getFirstExists(false);
        if(bullet) {
          bullet.reset(this.myplane.x + 16, this.myplane.y - 15);
          bullet.body.velocity.y = -400;
          bullet.body.velocity.x = -80;
          this.bulletTime = game.time.now + 200;
        }
        bullet = this.mybullets.getFirstExists(false);
        if(bullet) {
          bullet.reset(this.myplane.x + 16, this.myplane.y - 15);
          bullet.body.velocity.y = -400;
          bullet.body.velocity.x = 80;
          this.bulletTime = game.time.now + 200;
        }
      }
    }
  };
  // 被敌机打中
  this.hitMyplane = function(myplane, bullet) {
    bullet.kill();
    if(myplane.level > 1) {
      myplane.level--;
    } else {
      myplane.kill();
      this.dead();
    }
  };
  // 与敌机撞击
  this.crashMyplane = function(myplane, enemy) {
    myplane.kill();
    this.dead();
  }
  // 得奖了
  this.getAward = function(myplane, award) {
    award.kill();
    try {
      this.deng.play();
    } catch(e) {}
    if(myplane.level < 3) {
      myplane.level++;
    }
  };
  // 更新分数
  this.updateText = function() {
    if(b1==0){this.text.setText("第一关Score: " + score);}
    else if(b2==0){this.text.setText("第四关Score: " + score);}
    else if(b3==0){this.text.setText("第三关Score: " + score);}
    else {this.text.setText("第四关Score: " + score);}
    //this.text.setText("Score: " + score);
    if(score>3500){game.state.start('win');}
    else if(b1==0&&score>800){game.state.start('revive04');}
    else if(b2==0&&score>1600){game.state.start('revive08');}
    else if(b3==0&&score>2400){game.state.start('revive11');}
  };
  // 挂了
  this.dead = function() {
    try {
      this.ao.play();
    } catch(e) {}
    var myexplode = game.add.sprite(this.myplane.x, this.myplane.y, 'myexplode');
    var anim = myexplode.animations.add('myexplode');
    myexplode.animations.play('myexplode', 30, false, true);
    anim.onComplete.add(this.gotoOver, this);
  };
  // 跳转到Over场景
  this.gotoOver = function() {
    this.playback.stop();
  
    game.state.start('over');
  };
  // 更新函数
  this.update = function() {
    if(this.myStartFire) {
      this.myFireBullet();
      this.enemy1.enemyFire();
      this.enemy2.enemyFire();
      this.enemy3.enemyFire();
      this.enemy4.enemyFire();
      this.enemy5.enemyFire();
      // 碰撞检测
      game.physics.arcade.overlap(this.mybullets, this.enemy1.enemys, this.enemy1.hitEnemy, null, this.enemy1);
      game.physics.arcade.overlap(this.mybullets, this.enemy2.enemys, this.enemy2.hitEnemy, null, this.enemy2);
      game.physics.arcade.overlap(this.mybullets, this.enemy3.enemys, this.enemy3.hitEnemy, null, this.enemy3);
      game.physics.arcade.overlap(this.mybullets, this.enemy4.enemys, this.enemy4.hitEnemy, null, this.enemy4);
      game.physics.arcade.overlap(this.mybullets, this.enemy5.enemys, this.enemy5.hitEnemy, null, this.enemy5);
      game.physics.arcade.overlap(this.enemy1.enemyBullets, this.myplane, this.hitMyplane, null, this);
      game.physics.arcade.overlap(this.enemy2.enemyBullets, this.myplane, this.hitMyplane, null, this);
      game.physics.arcade.overlap(this.enemy3.enemyBullets, this.myplane, this.hitMyplane, null, this);
      game.physics.arcade.overlap(this.enemy4.enemyBullets, this.myplane, this.hitMyplane, null, this);
      game.physics.arcade.overlap(this.enemy5.enemyBullets, this.myplane, this.hitMyplane, null, this);
      game.physics.arcade.overlap(this.enemy1.enemys, this.myplane, this.crashMyplane, null, this);
      game.physics.arcade.overlap(this.enemy2.enemys, this.myplane, this.crashMyplane, null, this);
      game.physics.arcade.overlap(this.enemy3.enemys, this.myplane, this.crashMyplane, null, this);
      game.physics.arcade.overlap(this.enemy4.enemys, this.myplane, this.crashMyplane, null, this);
      game.physics.arcade.overlap(this.enemy5.enemys, this.myplane, this.crashMyplane, null, this);
      game.physics.arcade.overlap(this.awards, this.myplane, this.getAward, null, this);
    }
  };
};

game.States.over = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background');
    
    // 我的飞机
    this.myplane = game.add.sprite(100, 100, 'myplane');
    this.myplane.animations.add('fly');
    this.myplane.animations.play('fly', 12, true);
    // 分数
    var style = {font: "bold 32px Arial", fill: "#ff0000", boundsAlignH: "center", boundsAlignV: "middle"};
    this.text = game.add.text(0, 0, "Score: " + score, style);
    this.text.setTextBounds(0, 0, game.width, game.height);
    // 重来按钮
    this.replaybutton = game.add.button(12, 300, 'restart', this.onReplayClick, this, 0, 0, 1);
    // 复活按钮
    this.sharebutton = game.add.button(127, 300, 'relieve', this.onReliveClick, this, 0, 0, 1);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
  };
  // 重来
  this.onReplayClick = function() {
    this.normalback.stop();
    game.state.start('main');
  };
  //答题复活
  this.onReliveClick = function() {
    this.normalback.stop();
    if(b1==0){
      if(g1!=3){
        game.state.start('revive01');
      }else{
        game.state.start('last');}
    }
    else if(b2==0){
      if(g2!=3){
        game.state.start('revive05');
      }else{
        game.state.start('last');}
    }
    else if(b3==0){
      if(g3!=2){
        game.state.start('revive09');
      }else{
        game.state.start('last');}
    }
    else{
      game.state.start('revive12');
    }
  };
  // 分享
  // this.onShareClick = function() {
  //   document.title = makeTitle(score);
  //   document.getElementById('share').style.display = 'block';
  // };
}

game.States.last = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'last');
    
    // 我的飞机
    //this.myplane = game.add.sprite(100, 100, 'myplane');
    //this.myplane.animations.add('fly');
    //this.myplane.animations.play('fly', 12, true);
    // 分数
    var style = {font: "bold 24px Arial", fill: "#ff0000", boundsAlignH: "center", boundsAlignV: "middle"};
   // var style2 = {font: "bold 24px Arial", fill: "#ff0000", boundsAlignH: 240, boundsAlignV: 200};
    var word="闯关成功";
    this.text = game.add.text(0, 0, "Score: " + score, style);
    //this.text2 = game.add.text(0, 0, "闯关成功!Score: " + score, style2);
    this.text.setTextBounds(0, 0, game.width, game.height);
    //this.text2.setTextBounds(0, 0, game.width, game.height);
    // 重来按钮
    this.replaybutton = game.add.button(70, 300, 'restart', this.onReplayClick, this, 0, 0, 1);
    // 复活按钮
    //this.sharebutton = game.add.button(127, 300, 'relieve', this.onReliveClick, this, 0, 0, 1);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
  };
  // 重来
  this.onReplayClick = function() {
    this.normalback.stop();
    game.state.start('main');
  };
  //答题复活
  this.onReliveClick = function() {
    this.normalback.stop();
    
    game.state.start('revive01');
    
  };
  // 分享
  // this.onShareClick = function() {
  //   document.title = makeTitle(score);
  //   document.getElementById('share').style.display = 'block';
  // };
}

game.States.win = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background');
    
    // 我的飞机
    //this.myplane = game.add.sprite(100, 100, 'myplane');
    //this.myplane.animations.add('fly');
    //this.myplane.animations.play('fly', 12, true);
    // 分数
    var style = {font: "bold 24px Arial", fill: "#ff0000", boundsAlignH: "center", boundsAlignV: "middle"};
   // var style2 = {font: "bold 24px Arial", fill: "#ff0000", boundsAlignH: 240, boundsAlignV: 200};
    var word="闯关成功";
    this.text = game.add.text(0, 0, "闯关成功!Score: " + score, style);
    //this.text2 = game.add.text(0, 0, "闯关成功!Score: " + score, style2);
    this.text.setTextBounds(0, 0, game.width, game.height);
    //this.text2.setTextBounds(0, 0, game.width, game.height);
    // 重来按钮
    this.replaybutton = game.add.button(70, 300, 'restart', this.onReplayClick, this, 0, 0, 1);
    // 复活按钮
    //this.sharebutton = game.add.button(127, 300, 'relieve', this.onReliveClick, this, 0, 0, 1);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
  };
  // 重来
  this.onReplayClick = function() {
    this.normalback.stop();
    game.state.start('main');
  };
  //答题复活
  this.onReliveClick = function() {
    this.normalback.stop();
    game.state.start('revive01');
  };
  // 分享
  // this.onShareClick = function() {
  //   document.title = makeTitle(score);
  //   document.getElementById('share').style.display = 'block';
  // };
}
game.States.fail = function() {
  this.create = function() {
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 'background');
    
    // 我的飞机
    //this.myplane = game.add.sprite(100, 100, 'myplane');
    //this.myplane.animations.add('fly');
    //this.myplane.animations.play('fly', 12, true);
    // 分数
    var style = {font: "bold 24px Arial", fill: "#ff0000", boundsAlignH: "center", boundsAlignV: "middle"};
   // var style2 = {font: "bold 24px Arial", fill: "#ff0000", boundsAlignH: 240, boundsAlignV: 200};
    var word="闯关成功";
    this.text = game.add.text(0, 0, "闯关失败!Score: " + score, style);
    //this.text2 = game.add.text(0, 0, "闯关成功!Score: " + score, style2);
    this.text.setTextBounds(0, 0, game.width, game.height);
    //this.text2.setTextBounds(0, 0, game.width, game.height);
    // 重来按钮
    this.replaybutton = game.add.button(70, 300, 'restart', this.onReplayClick, this, 0, 0, 1);
    // 复活按钮
    //this.sharebutton = game.add.button(127, 300, 'relieve', this.onReliveClick, this, 0, 0, 1);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
  };
  // 重来
  this.onReplayClick = function() {
    this.normalback.stop();
    game.state.start('main');
  };
  //答题复活
  this.onReliveClick = function() {
    this.normalback.stop();
    game.state.start('revive01');
  };
  // 分享
  // this.onShareClick = function() {
  //   document.title = makeTitle(score);
  //   document.getElementById('share').style.display = 'block';
  // };
}
game.States.revive01 = function() {
  this.create = function() {
    if(flag1==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't01');
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive02');
    }
    
  };
  this.onStartClick = function() {
    flag1=1;
    g1++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();
    this.error.stop();
    game.state.start('revive02');
    this.normalback.stop();
  };
};

game.States.revive02 = function() {
  this.create = function() {
    if(flag2==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't02');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onStartClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive03');
    }
    
  };
  this.onStartClick = function() {
    flag2=1;
    g1++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive03');
    this.normalback.stop();
  };
};

game.States.revive03 = function() {
  this.create = function() {
    if(flag3==0){  
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't03');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onStartClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive01');
    }
    
  };
  this.onStartClick = function() {
    flag3=1;
    g1++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive01');
    this.normalback.stop();
  };
};

game.States.revive04 = function() {
  this.create = function() {
    if(flag4==0){  
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't04');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive05');
    }
    
  };
  this.onStartClick = function() {
    flag4=1;
    b1=1;
    b2=0;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('fail');
    this.normalback.stop();
  };
};

game.States.revive05 = function() {
  this.create = function() {
    if(flag5==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't05');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onStartClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C',this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive06');
    }
    
  };
  this.onStartClick = function() {
    flag5=1;
    g2++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive06');
    this.normalback.stop();
  };
};

game.States.revive06 = function() {
  this.create = function() {
    if(flag6==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't06');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive07');
    }
    
  };
  this.onStartClick = function() {
    flag6=1;
    g2++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive07');
    this.normalback.stop();
  };
};

game.States.revive07 = function() {
  this.create = function() {
    if(flag7==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't07');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive05');
    }
    
  };
  this.onStartClick = function() {
    flag7=1;
    g2++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive05');
    this.normalback.stop();
  };
};

game.States.revive08 = function() {
  this.create = function() {
    if(flag8==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't08');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onStartClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive09');
    }
    
  };
  this.onStartClick = function() {
    flag8=1;
    b2=1;
    b3=0;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('fail');
    this.normalback.stop();
  };
};

game.States.revive09 = function() {
  this.create = function() {
    if(flag9==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't09');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onStartClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive10');
    }
    
  };
  this.onStartClick = function() {
    flag9=1;
    g3++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive10');
    this.normalback.stop();
  };
};

game.States.revive10 = function() {
  this.create = function() {
    if(flag10==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't10');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onStartClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('revive09');
    }
    
  };
  this.onStartClick = function() {
    flag10=1;
    g3++;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('revive09');
    this.normalback.stop();
  };
};

game.States.revive11 = function() {
  this.create = function() {
    if(flag11==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't11');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onNextClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onStartClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('last');
    }
    
  };
  this.onStartClick = function() {
    flag10=1;
    b3=1;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('fail');
    this.normalback.stop();
  };
};

game.States.revive12 = function() {
  this.create = function() {
    if(flag12==0){
    // 背景
    var bg = game.add.tileSprite(0, 0, game.width, game.height, 't120');
    
    // 我的飞机
    // this.myplane = game.add.sprite(100, 100, 'myplane');
    // this.myplane.animations.add('fly');
    // this.myplane.animations.play('fly', 12, true);
    // 开始按钮
    this.A = game.add.button(5, 300, 'A', this.onStartClick, this, 1, 1, 0);
    this.B = game.add.button(65, 300, 'B', this.onNextClick, this, 1, 1, 0);
    this.C = game.add.button(125, 300, 'C', this.onNextClick, this, 1, 1, 0);
    this.D = game.add.button(185, 300, 'D', this.onNextClick, this, 1, 1, 0);
    // 背景音乐
    this.normalback = game.add.audio('normalback', 0.2, true);
    this.normalback.play();
    }else{
      game.state.start('last');
    }
    
  };
  this.onStartClick = function() {
    flag12=1;
    game.state.start('start');
    this.normalback.stop();
  };
  this.onNextClick = function() {
    this.error = game.add.audio('error', 0.2, true);
    this.error.play();this.error.stop();
    game.state.start('fail');
    this.normalback.stop();
  };
};

game.state.add('boot', game.States.boot);
game.state.add('preload', game.States.preload);
game.state.add('first', game.States.first);
game.state.add('second', game.States.second);
game.state.add('main', game.States.main);
game.state.add('start', game.States.start);

game.state.add('revive01', game.States.revive01);
game.state.add('revive02', game.States.revive02);
game.state.add('revive03', game.States.revive03);
game.state.add('revive04', game.States.revive04);
game.state.add('revive05', game.States.revive05);
game.state.add('revive06', game.States.revive06);
game.state.add('revive07', game.States.revive07);
game.state.add('revive08', game.States.revive08);
game.state.add('revive09', game.States.revive09);
game.state.add('revive10', game.States.revive10);
game.state.add('revive11', game.States.revive11);
game.state.add('revive12', game.States.revive12);
game.state.add('over', game.States.over);
game.state.add('last', game.States.last);
game.state.add('win', game.States.win);
game.state.add('fail', game.States.fail);

game.state.start('boot');



评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值