用jQuery仿写出马里奥小游戏

CSS页面代码:

   *{
      margin: 0;
      padding: 0;
    }
    .games{
      width: 1500px;
      height: 800px;
      margin: 50px auto;
      background-color: #ccc;
      position: relative;
      overflow: hidden;
    }
    .role{
      width: 100px;
      height: 200px;
      position: absolute;
      bottom: 0;
    }
    .role img{
      width: 100%;
      height: 100%;
    }
    .barrier{
      width: 100px;
      height: 100px;
      position: absolute;
      left: 1300px;
      bottom: 0;
    }
    .barrier img{
      width: 100%;
      height: 100%;
    }
    @keyframes move {
      0%{
        left: 1500px;
      }
      100%{
        left: -100px;
      }  
    }
    .move{
      animation: move 2s linear infinite;
    }
    .animate-pause{
      animation-play-state: paused;
    }
    .btn-mask{
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, .8);
      position: absolute;
      left: 0;
      top: 0;
      z-index: 9;
    }
    .btn-mask .start,.games .pause{
      width: 100px;
      height: 100px;
      font-size: 18px;
      color: #fff;
      text-align: center;
      line-height: 100px;
      background-color:rgb(0, 102, 255);
      position: absolute;
      border-radius: 50%;
      cursor: pointer;
    }
    .btn-mask .start{
      transform: translate(-50%,-50%);
      position: absolute;
      left: 50%;
      top: 50%;
    }
    .games .pause{
      right: 40px;
      top: 40px;
    }

HTML页面代码:

 <!-- 游戏界面 -->
  <div class="games">
    <!-- 游戏角色 -->
    <div class="role">
      <img src="https://img1.baidu.com/it/u=4071404386,3974115926&fm=253&fmt=auto&app=138&f=PNG?w=268&h=331" alt="">
    </div>
    <!-- 障碍物 -->
    <div class="barrier">
      <img src="https://img0.baidu.com/it/u=3949025014,4235362944&fm=253&fmt=auto&app=138&f=PNG?w=256&h=256" alt="">
    </div>
  </div>

JS页面详细代码:

 <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script>
    $(function(){
      let _role=$('.role'),
      _barrier=$('.barrier'),
      _games=$('.games'),
      _games_h=_games.height(),
      _role_h=_role.height(),
      _diff_h=_games_h-_role_h;
      _timer=null;
      _games.append($('<div class="btn-mask"><div class="start jq-start">开始游戏</div></div>'));
      $('.jq-start').click(function(){
        startGames();
      });
      // 判断游戏是否结束
      function isOver(){
        // 判断角色是否与障碍物碰撞
        let _role_bottom=_diff_h-_role.position().top;
        let  _barrier_left=_barrier.position().left;
        if(_role_bottom<40 &&_barrier_left>-40 &&_barrier_left<40){
          restartGames();
        }   
      }
      // 重新开始
      function restartGames(){
        clearInterval(_timer);
        $('.pause').remove();
        $(window).off('keydown');
        _barrier.removeClass('move');
        $('.jq-start').html('重新开始');
        $('.btn-mask').show();
      }
      // 继续游戏
      function continueGames(){
        _timer=setInterval(isOver,10);
        $(window).on('keydown',jump);
        _barrier.removeClass('animate-pause');
        $('.jq-pause').html('暂停游戏');
        $('.jq-pause').click(function(){
          pasuseGames();
        });
      }
       // 暂停游戏
       function pasuseGames(){
         clearInterval(_timer);
         $(window).off('keydown');
         _barrier.addClass('animate-pause');
         $('.jq-pause').html('继续游戏');
         $('.jq-pause').click(function(){
           continueGames();
         })
       }
       // 角色跳跃
       function jump(event){
         if(event.keyCode==32){
           _role.animate({
             bottom:'300px'
           },'fast','swing').delay(150).animate({
             bottom:0
           },'fast','swing');
         }
       }
        // 开始游戏
        function startGames(){
          $('.btn-mask').hide();
          _games.append($('<div class="pause jq-pause">暂停游戏</div>'));
          $('.jq-pause').click(function(){
            pasuseGames();
          });
          _barrier.addClass('move');
          _timer=setInterval(isOver,10);
          $(window).on('keydown',jump);
        }
    })
  </script>

实现效果如图:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值