H5实现俄罗斯方块(一)

这几天一直忙于公司的项目,涉及到流程问题,(有时间会写成博客的)。。。一直没有更新。。。

为了更加巩固js的基础,自己封装类似于JQuery的操作库来对dom进行操作。

一:前度页面的绘制。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>俄罗斯方块</title>
    <link rel="stylesheet" href="css/index.css">
</head>

<body>
    <div class="start-container">
        <p><button id="btn-start">开始游戏</button></p>
        <p><button id="btn-setting">设置</button></p>
    </div>
    <div class="game-container">
        <div class="timer-panel">
            <div class="panel-header">
                使用时间
            </div>
            <div class="panel-body">
                <canvas id="timer"></canvas>
            </div>
        </div>
        <div class="help-panel">
            <div class="panel-body">
                <p>↑ 翻转方块</p>
                <p>↓ 加速下落</p>
                <p>→ 向右移动</p>
                <p>← 向左移动</p>
            </div>
        </div>
        <div class="level-panel">
            <div class="panel-header">
                当前级别
            </div>
            <div class="panel-body">
                <canvas id="level"></canvas>
            </div>
        </div>
        <div class="game-main-panel">
            <canvas id="c_game_main"></canvas>
        </div>
        <div class="next-panel">
            <div class="panel-header">
                下一方块
            </div>
            <div class="panel-body">
                <canvas id="nextshape"></canvas>
            </div>
        </div>
        <div class="setting-panel">
            <div class="panel-body">
                <button id="btn-game-pause">暂停</button><br><br><button id="btn-game-setting">设置</button>
            </div>
        </div>
        <div class="score-panel">
            <div class="panel-header">
                当前得分
            </div>
            <div class="panel-body">
                <canvas id="score"></canvas>
            </div>
        </div>
        <div class="high-score-panel">
            <div class="panel-header">
                最高得分
            </div>
            <div class="panel-body">
                <canvas id="highscore"></canvas>
            </div>
        </div>
    </div>
    <div class="modal-dialog">
        <div class="modal-body">
            <label for="ck-sound">启用声音</label>
            <input type="checkbox" id="ck-sound">
            <br>
            <br>
            <button id="btn-dialog-close">关闭</button>
        </div>
    </div>
    <script src="vendor/howler.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/ResourceManager.js"></script>
    <script src="js/Canvas.js"></script>
    <script src="js/Keyboard.js"></script>
    <script src="js/Score.js"></script>
    <script src="js/Timer.js"></script>
    <script src="js/Level.js"></script>
    <script src="js/NextShape.js"></script>
    <script src="js/HighScore.js"></script>
    <script src="js/Block.js"></script>
    <script src="js/Shape.js"></script>
    <script src="js/Board.js"></script>
    <script src="js/Tetris.js"></script>
    <script src="js/app.js"></script>
</body>

</html>

 对应的css样式:

*{
    box-sizing: border-box;
}
html,body,div{
    margin: 0;
    padding: 0;
}

body{
    background: #000;
    color: #fff;
}

.game-container, .start-container{
    position: relative;
    box-sizing: content-box;
    height: 600px;
    width: 590px;
    margin: 0 auto;
    margin-top: 50px;
    border: 1px solid green;
}

.game-container{
    display: none;
}

.start-container{
    background: url(../images/bg_start.png);
    background-size: cover;
    width: 390px;
    padding-top: 280px;
    box-sizing: border-box;
}

.start-container p{
    text-align: center;
    margin-top: 40px;
}

.start-container p button{
    width: 200px;
    height: 50px;
    line-height: 50px;
    font-size: 20px;
    font-family: 微软雅黑;
    border: 0;
    border-radius: 50%;
    outline: none;
    cursor: pointer;
    background: lightblue;
}

.start-container p button:hover{
    background: lightcoral;
}

.game-main-panel{
    position: absolute;
    left: 100px;
    top: 0;
    width: 390px;
    height: 600px;
    border-left: 1px solid green;
    border-right: 1px solid green;
}
.timer-panel{
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
}
.help-panel{
    position: absolute;
    top: 150px;
    left: 0;
    width: 100px;
}
.level-panel{
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100px;
    height: 100px;
}
.next-panel{
    position: absolute;
    right: 0;
    top: 0;
    width: 100px;
    height: 100px;
}
.setting-panel{
    position: absolute;
    top: 150px;
    right: 0;
    width: 100px;
}
.score-panel{
    position: absolute;
    right: 0;
    bottom: 120px;
    width: 100px;
    height: 100px;
}
.high-score-panel{
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100px;
    height: 100px;
}
.panel-header{
    height: 30px;
    line-height: 30px;
    padding-left: 5px;
    font-size: 18px;
    color:#fff;
    background: lightsalmon;
}
.panel-body{
    text-align: center;
    line-height: 70px;
}
.help-panel .panel-body,
.setting-panel .panel-body{
    line-height: normal;
}
.timer-panel, .next-panel, .score-panel{
    border-bottom: 1px solid green;
}

.modal-dialog{
    display: none;
}

.modal-dialog .modal-body{
    position: fixed;
    height: 300px;
    width: 390px;
    top: 50%;
    margin-top: -150px;
    left: 50%;
    margin-left: -195px;
    background: #000;
    z-index: 100;
    text-align: center;
}

.modal-dialog::after{
    content: '';
    position: fixed;
    top:0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(255,255,255,0.3);
}

 



 

转载于:https://www.cnblogs.com/sunliyuan/p/6158445.html

好的,以下是一个简单的HTML5俄罗斯方块游戏的代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>俄罗斯方块</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="canvas" width="300" height="600"></canvas> <script> // 获取画布和绘图上下文 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); // 定义方块大小和数量 var blockSize = 30; var blockCountX = canvas.width / blockSize; var blockCountY = canvas.height / blockSize; // 定义方块数组并初始化为空 var blocks = []; for (var i = 0; i < blockCountY; i++) { blocks[i] = []; for (var j = 0; j < blockCountX; j++) { blocks[i][j] = 0; } } // 定义当前方块的位置和形状 var curX = 0; var curY = 0; var curShape = [ [1, 1], [1, 1] ]; // 绘制方块 function drawBlock(x, y, color) { ctx.fillStyle = color; ctx.fillRect(x * blockSize, y * blockSize, blockSize, blockSize); ctx.strokeRect(x * blockSize, y * blockSize, blockSize, blockSize); } // 绘制所有方块 function drawBlocks() { for (var i = 0; i < blockCountY; i++) { for (var j = 0; j < blockCountX; j++) { if (blocks[i][j] != 0) { drawBlock(j, i, 'blue'); } } } } // 绘制当前方块 function drawCurBlock() { for (var i = 0; i < curShape.length; i++) { for (var j = 0; j < curShape[i].length; j++) { if (curShape[i][j] != 0) { drawBlock(curX + j, curY + i, 'red'); } } } } // 清空画布 function clearCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); } // 更新方块数组 function updateBlocks() { for (var i = 0; i < curShape.length; i++) { for (var j = 0; j < curShape[i].length; j++) { if (curShape[i][j] != 0) { blocks[curY + i][curX + j] = 1; } } } } // 检查方块是否与其他方块重叠 function checkOverlap() { for (var i = 0; i < curShape.length; i++) { for (var j = 0; j < curShape[i].length; j++) { if (curShape[i][j] != 0 && blocks[curY + i][curX + j] != 0) { return true; } } } return false; } // 检查方块是否到达底部 function checkBottom() { for (var i = 0; i < curShape.length; i++) { for (var j = 0; j < curShape[i].length; j++) { if (curShape[i][j] != 0 && (curY + i == blockCountY - 1 || blocks[curY + i + 1][curX + j] != 0)) { return true; } } } return false; } // 消除满行 function clearFullLines() { for (var i = blockCountY - 1; i >= 0; i--) { var isFull = true; for (var j = 0; j < blockCountX; j++) { if (blocks[i][j] == 0) { isFull = false; break; } } if (isFull) { blocks.splice(i, 1); blocks.unshift(new Array(blockCountX).fill(0)); } } } // 开始游戏 function startGame() { curX = Math.floor(blockCountX / 2) - 1; curY = 0; curShape = [ [1, 1], [1, 1] ]; if (checkOverlap()) { alert('游戏结束!'); return; } clearCanvas(); drawBlocks(); drawCurBlock(); setTimeout(function() { if (checkBottom()) { updateBlocks(); clearFullLines(); startGame(); } else { curY++; clearCanvas(); drawBlocks(); drawCurBlock(); startGame(); } }, 500); } startGame(); </script> </body> </html> ``` 此代码实现一个简单的俄罗斯方块游戏,使用 HTML5 的 canvas 元素进行绘图。其中,方块大小、数量和形状都可以自由定义。游戏中包含了方块的绘制、移动、更新、消除等基本功能。该代码仅供参考,具体实现可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值