别碰钉子^^

欢迎来到程序小院

别碰钉子

玩法:鼠标点击左键飞行的小鸟就会上升,不点击降落,不同的关卡左右两侧不同的钉子数,快去闯关吧^^。

开始游戏icon-default.png?t=N7T8https://www.ormcc.com/play/gameStart/224

html

  <div id="game"></div>  

css

*{margin:0;padding:0}
html, body
{
    -webkit-text-size-adjust:none;
    overflow: hidden;
    height:100%;
}

js

MyGame.Game = function(game) {
 game.direction = 1;
 this.nailNumber = 1;
 this.score = 0;
 this.hitWall = false;
 game.isRun = false;
};
MyGame.Game.prototype = {
    create: function() {
        game.stage.backgroundColor = '#d4d8d3';
        GameUI.Game_element()
     //添加屏幕中间的圆
     this.circle = game.add.sprite(game.world.centerX, game.world.centerY, 
      game.circleGraphics.generateTexture());
     this.circle.anchor.set(0.5);
     this.scoreText = this.add.text(this.world.centerX, this.world.centerY+20, 
      '0',{font: "bold 238px Microsoft YaHei", fill: '#d4d8d3',align:'center'});
        this.scoreText.anchor.set(0.5);
        
        //添加上下矩形
  this.rectTop = game.add.sprite(0, 0, game.rectGraphics.generateTexture());
  this.rectBottom = game.add.sprite(0, game.world.height - 80, 
    game.rectGraphics.generateTexture());
     //添加小鸟
     this.bird = this.add.sprite(game.world.centerX,game.world.centerY,'bird');
     this.bird.anchor.set(0.5);
     //开启物理引擎
     game.physics.enable([this.bird],Phaser.Physics.ARCADE);
     //开启鸟的边界碰撞,系数 0.8
     this.bird.body.collideWorldBounds = true;
     this.bird.body.bounce.set(0.8)
     //添加上下钉子组(带物理属性)
     this.upbottomNailGroup = game.add.physicsGroup();
     //添加左右钉子组(带物理属性)
     this.leftrigthNailGroup = game.add.physicsGroup();
     //循环添加 上下钉子,并加入到上下钉子组里面
     for(var i = 0 ;i<16;i++)
     {
      if(i<8)
      {
       this.Nail = game.add.sprite(96*i, 80+20, game.NailGraphics.generateTexture());
       this.upbottomNailGroup.add(this.Nail);
      }
      else
      {
       this.Nail = game.add.sprite(96*i - game.world.width - 19, 
          game.world.height - 80 - 20, game.NailGraphics.generateTexture());
       this.Nail.scale.y = -1
       this.upbottomNailGroup.add(this.Nail);
      }
     }
     //让上下钉子组固定不动
     this.upbottomNailGroup.forEach(function(i){
      i.anchor.set(0,0.5);
      i.body.immovable = true;
     })
     //利用自定义createNail方法创建钉子
     this.createNail(2)
     this.GameStart();
     //添加屏幕点击事件
     game.input.onTap.add(this.birdJump, this);
     //销毁 图形
     game.circleGraphics.destroy();
     game.NailGraphics.destroy();
     game.rectGraphics.destroy();
    },
    GameStart : function(){
     game.isRun = true;
        this.bird.body.gravity.y = 1000;
        this.bird.body.velocity.y = -350;
     this.bird.body.velocity.x = 200
    },
    GameOver : function(){
     game.isRun = false;
     this.bird.body.velocity.x = 2000;
        this.bird.body.velocity.y = 2000;
        this.add.tween(this.bird).to( { alpha: 0 }, 1000, "Linear", true).onComplete.add(function(){
            this.bird.kill();
        }, this);
    },
    birdJump : function(){
     if(game.isRun)
     {
      this.bird.body.velocity.y = -350;
      this.bird.body.velocity.x = 200 * game.direction;
     }
    },
    createNail : function(num){
     for(var i=0;i<num;i++)
        {
         var nailSpace = 120 + 60 * this.RandomNum(0,15)         
         this.NailA = game.add.sprite(game.world.width - 15, nailSpace , game.NailGraphicsA.generateTexture());
      this.NailA .anchor.set(0.5,0);
      this.leftrigthNailGroup.add(this.NailA);
      this.NailA.body.immovable = true;
        }
    },
    RandomNum : function(Min,Max){
     var Range = Max - Min;
  var Rand = Math.random();   
  var num = Min + Math.round(Rand * Range);
  return num;
    },
    update: function() {
     if(!game.isRun) return;
     game.physics.arcade.collide(this.upbottomNailGroup, 
      this.bird,this.GameOver, null, this);
     game.physics.arcade.collide(this.leftrigthNailGroup, 
      this.bird,this.GameOver, null, this);
     if(this.bird.x>=game.world.width - this.bird.width/2)
     {
      game.direction = this.bird.scale.x =  -1;
      this.hitWall = true;
     }
     else if(this.bird.x<=0 - this.bird.width/2){
      game.direction = this.bird.scale.x =  1;
      this.hitWall = true;
     }
     if(this.hitWall)
     {
      this.score++;
      this.scoreText.setText(this.score);
      if(this.score % 10 == 0)
      {
       this.createNail(1)
      }
      this.leftrigthNailGroup.forEach(function(i){
       i.body.immovable = true;
       if(game.direction == -1)
       {
        i.x = 0 + 15;
        i.scale.x = -1;
       }
       else
       {
        i.x = game.world.width - 15;
        i.scale.x = 1
       }
    i.y = 162 + 100 * game.rnd.integerInRange(0,8)
      });
      this.hitWall = false;
     }
    },
    render : function() {
  /*game.debug.body(this.bird);
     this.leftrigthNailGroup.forEach(function(i){
       game.debug.body(i);
     })*/
 }
};

源码

需要源码请关注添加好友哦^ ^

转载:欢迎来到本站,转载请注明文章出处https://ormcc.com/

相关推荐

  1. 数值类型==和equals用错

    2023-12-21 17:12:03       41 阅读
  2. 让猴子跳回背上摘抄

    2023-12-21 17:12:03       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 17:12:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 17:12:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 17:12:03       82 阅读
  4. Python语言-面向对象

    2023-12-21 17:12:03       91 阅读

热门阅读

  1. facebook广告投放有哪些需要注意的

    2023-12-21 17:12:03       56 阅读
  2. 在 Docker 上部署 Nacos 并连接到 MySQL

    2023-12-21 17:12:03       47 阅读
  3. 数据分析的基本步骤有哪些?

    2023-12-21 17:12:03       55 阅读
  4. 【密码学引论】密码协议

    2023-12-21 17:12:03       47 阅读
  5. 在Idea中创建基于工件的本地服务

    2023-12-21 17:12:03       64 阅读
  6. Codeforces Round 916 (Div. 3)(A~E2)

    2023-12-21 17:12:03       53 阅读
  7. 深度学习嵌入头embedding head解释

    2023-12-21 17:12:03       57 阅读
  8. 7-1 冰雹猜想

    2023-12-21 17:12:03       60 阅读
  9. minio 整合springboot

    2023-12-21 17:12:03       52 阅读
  10. Springboot Async 引起的循环依赖

    2023-12-21 17:12:03       55 阅读