厉害!84 行 JavaScript 代码实现塔式堆叠游戏

作者 | 李雪敬

出品 | 程序人生(coder_life)

塔式堆叠小游戏游戏玩法

想必大家对这个小游戏都挺熟悉的。这个游戏的重点是把尽可能多的盒子叠在一起。当你点击屏幕时,位于上方正在移动的盒子会往下掉,如果想要上面移动的盒子正好落在下面盒子上,就需要在合适的时机点击屏幕。如果你没在两个盒子完美对齐的时候点击,盒子突出的部分就会被切割掉下来(“碎片”),下一个盒子的落地空间就更小了,比前一个更难弹跳。

游戏代码

<html>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
<script>
let canvas = document.getElementById("myCanvas");
let context = canvas.getContext("2d");
context.font = 'bold 30px sans-serif';
let scrollCounter, cameraY, current, mode, xSpeed;
let ySpeed = 5;
let height = 50;
let boxes = [];
boxes[0] = {
  x: 300,
  y: 300,
  width: 200
};
let debris = {
  x: 0,
  width: 0
};

function newBox() {
  boxes[current] = {
    x: 0,
    y: (current + 10) * height,
    width: boxes[current - 1].width
  };
}

function gameOver() {
  mode = 'gameOver';
  context.fillText('Game over. Click to play again!', 50, 50);
}

function animate() {
  if (mode != 'gameOver') {
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.fillText('Score: ' + (current - 1).toString(), 100, 200);
    for (let n = 0; n < boxes.length; n++) {
      let box = boxes[n];
      context.fillStyle = 'rgb(' + n * 16 + ',' + n * 16 + ',' + n * 16 + ')';
      context.fillRect(box.x, 600 - box.y + cameraY, box.width, height);
    }
    context.fillStyle = 'red';
    context.fillRect(debris.x, 600 - debris.y + cameraY, debris.width, height);
    if (mode == 'bounce') {
      boxes[current].x = boxes[current].x + xSpeed;
      if (xSpeed > 0 && boxes[current].x + boxes[current].width > canvas.width)
        xSpeed = -xSpeed;
      if (xSpeed < 0 && boxes[current].x < 0)
        xSpeed = -xSpeed;
    }
    if (mode == 'fall') {
      boxes[current].y = boxes[current].y - ySpeed;
      if (boxes[current].y == boxes[current - 1].y + height) {
        mode = 'bounce';
        let difference = boxes[current].x - boxes[current - 1].x;
        if (Math.abs(difference) >= boxes[current].width) {
          gameOver();
        }
        debris = {
          y: boxes[current].y,
          width: difference
        };
        if (boxes[current].x > boxes[current - 1].x) {
          boxes[current].width = boxes[current].width - difference;
          debris.x = boxes[current].x + boxes[current].width;
        } else {
          debris.x = boxes[current].x - difference;
          boxes[current].width = boxes[current].width + difference;
          boxes[current].x = boxes[current - 1].x;
        }
        if (xSpeed > 0)
          xSpeed++;
        else
          xSpeed--;
        current++;
        scrollCounter = height;
        newBox();
      }
    }
    debris.y = debris.y - ySpeed;
    if (scrollCounter) {
      cameraY++;
      scrollCounter--;
    }
  }
  window.requestAnimationFrame(animate);
}

function restart() {
  boxes.splice(1, boxes.length - 1);
  mode = 'bounce';
  cameraY = 0;
  scrollCounter = 0;
  xSpeed = 2;
  current = 1;
  newBox();
  debris.y = 0;
}

canvas.onpointerdown = function() {
  if (mode == 'gameOver')
    restart();
  else {
    if (mode == 'bounce')
      mode = 'fall';
  }
};

restart();
animate();
</script>
</body>
</html>

参考链接:https://slicker.me/javascript/tower.htm

#欢迎来留言#

留言点赞数量最多的前两名

程序人生携手【北京大学出版社】送出

《从零开始读懂量子力学》一本

截至9月18日14:00点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
html5叠房子消消乐小游戏代码是一款非常好玩的html5叠房子游戏源码,点击颜色相同的邻接区域,可以将这些区域合成到一处进消除升级。 js代码     [removed]         // The game configuration         var qici = {};         qici.config = {             projectName: 'Subara',             gameName: '消消乐',             companyName: 'qcplay',             bundleIdentifier: 'com.qcplay',             gameInstance: 'qc_game',             frameRate: {"mobile":40,"desktop":60},             backgroundColor: 4210752,             runInBackground: true,             antialias: true,             transparent: false,             developerMode: false,             renderer: 'Canvas',             resolution : Math.min(2, devicePixelRatio),             loadingPrefab: '',             scene: {                              "SubaraCity" : "Assets/state/SubaraCity.bin"             },             entryScene : 'SubaraCity',             dirtyRectangles: true,             loading: {                 loadingText: 'Loading, please wait...',                 loadingInterval: 200,                 brightingInterval: 10,                 blinkingCount: 5,                 blinkingInterval: 70,                 fadingInInterval: 400,                 fadingOutInterval: 600             }         };   _pluginVariables_={}         qici.scripts = [             './Assets/meta/globalUrlMap.js',             './xiaoxiaole/phaser.min.js',             './xiaoxiaole/webfontloader.js',             './xiaoxiaole/qc-core.js',             './xiaoxiaole/qc-webgl.js',             // External scripts for plugins             // User scripts             './js/game-scripts-mini-1.0.js'         ]; // Asset count that need preload in boot qici.loadingAssetCount = 11;     [removed]     [removed][removed]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值