以下是使用jQuery编写俄罗斯方块的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Tetris Game</title>
<style type="text/css">
#game-board {
width: 300px;
height: 600px;
border: 1px solid black;
margin: 0 auto;
position: relative;
}
.block {
width: 30px;
height: 30px;
border: 1px solid black;
position: absolute;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var gameBoard = $('#game-board');
var blockWidth = parseInt(gameBoard.css('width')) / 10;
var blockHeight = parseInt(gameBoard.css('height')) / 20;
var currentBlock = null;
var currentBlockType = null;
var currentBlockOrientation = null;
var currentBlockPosition = null;
var currentBlockTimer = null;
var score = 0;
var scoreBoard = $('#score-board');
var highScore = localStorage.getItem('tetris-high-score') || 0;
var highScoreBoard = $('#high-score-board');
highScoreBoard.text(highScore);
function createBlock(type, orientation, position) {
var block = $('<div class="block"></div>');
block.css({
'width': blockWidth + 'px',
'height': blockHeight + 'px',
'top': position[0] * blockHeight + 'px',
'left': position[1] * blockWidth + 'px'
});
block.addClass(type + '-' + orientation);
gameBoard.append(block);
return block;
}
function moveBlockDown() {
var newPosition = [currentBlockPosition[0] + 1, currentBlockPosition[1]];
if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) {
currentBlock.css('top', newPosition[0] * blockHeight + 'px');
currentBlockPosition = newPosition;
} else {
clearInterval(currentBlockTimer);
currentBlockTimer = null;
checkForCompletedRows();
createNewBlock();
}
}
function isValidPosition(type, orientation, position) {
var blocks = getBlocksForTypeAndOrientation(type, orientation);
for (var i = 0; i < blocks.length; i++) {
var row = position[0] + blocks[i][0];
var col = position[1] + blocks[i][1];
if (row < 0 || row >= 20 || col < 0 || col >= 10) {
return false;
}
if ($('#game-board .block[row="' + row + '"][col="' + col + '"]').length > 0) {
return false;
}
}
return true;
}
function getBlocksForTypeAndOrientation(type, orientation) {
var blocks = [];
switch (type) {
case 'i':
switch (orientation) {
case 0:
case 2:
blocks = [[0, 0], [1, 0], [2, 0], [3, 0]];
break;
case 1:
case 3:
blocks = [[0, 0], [0, 1], [0, 2], [0, 3]];
break;
}
break;
case 'j':
switch (orientation) {
case 0:
blocks = [[0, 0], [1, 0], [1, 1], [1, 2]];
break;
case 1:
blocks = [[0, 2], [1, 2], [2, 2], [2, 1]];
break;
case 2:
blocks = [[2, 0], [2, 1], [1, 1], [0, 1]];
break;
case 3:
blocks = [[0, 0], [0, 1], [1, 1], [2, 1]];
break;
}
break;
case 'l':
switch (orientation) {
case 0:
blocks = [[0, 2], [1, 0], [1, 1], [1, 2]];
break;
case 1:
blocks = [[0, 1], [1, 1], [2, 1], [2, 2]];
break;
case 2:
blocks = [[2, 0], [1, 0], [1, 1], [1, 2]];
break;
case 3:
blocks = [[0, 1], [1, 1], [2, 1], [0, 0]];
break;
}
break;
case 'o':
blocks = [[0, 0], [0, 1], [1, 0], [1, 1]];
break;
case 's':
switch (orientation) {
case 0:
case 2:
blocks = [[0, 1], [0, 2], [1, 0], [1, 1]];
break;
case 1:
case 3:
blocks = [[0, 1], [1, 1], [1, 2], [2, 2]];
break;
}
break;
case 't':
switch (orientation) {
case 0:
blocks = [[0, 1], [1, 0], [1, 1], [1, 2]];
break;
case 1:
blocks = [[0, 1], [1, 1], [2, 1], [1, 2]];
break;
case 2:
blocks = [[1, 0], [0, 1], [1, 1], [2, 1]];
break;
case 3:
blocks = [[0, 1], [1, 0], [1, 1], [2, 1]];
break;
}
break;
case 'z':
switch (orientation) {
case 0:
case 2:
blocks = [[0, 0], [0, 1], [1, 1], [1, 2]];
break;
case 1:
case 3:
blocks = [[0, 2], [1, 1], [1, 2], [2, 1]];
break;
}
break;
}
return blocks;
}
function createNewBlock() {
var types = ['i', 'j', 'l', 'o', 's', 't', 'z'];
var type = types[Math.floor(Math.random() * types.length)];
var orientation = Math.floor(Math.random() * 4);
var position = [0, Math.floor(Math.random() * 7)];
currentBlock = createBlock(type, orientation, position);
currentBlockType = type;
currentBlockOrientation = orientation;
currentBlockPosition = position;
currentBlockTimer = setInterval(moveBlockDown, 500);
}
function checkForCompletedRows() {
for (var row = 0; row < 20; row++) {
var blocksInRow = $('#game-board .block[row="' + row + '"]');
if (blocksInRow.length === 10) {
blocksInRow.remove();
score += 10;
scoreBoard.text(score);
if (score > highScore) {
highScore = score;
highScoreBoard.text(highScore);
localStorage.setItem('tetris-high-score', highScore);
}
$('#game-board .block').each(function() {
var blockRow = parseInt($(this).attr('row'));
var blockCol = parseInt($(this).attr('col'));
if (blockRow < row) {
$(this).css('top', (blockRow + 1) * blockHeight + 'px');
$(this).attr('row', blockRow + 1);
}
});
row--;
}
}
}
$(document).keydown(function(event) {
if (currentBlockTimer !== null) {
switch (event.which) {
case 37: // left arrow
var newPosition = [currentBlockPosition[0], currentBlockPosition[1] - 1];
if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) {
currentBlock.css('left', newPosition[1] * blockWidth + 'px');
currentBlockPosition = newPosition;
}
break;
case 38: // up arrow
var newOrientation = (currentBlockOrientation + 1) % 4;
if (isValidPosition(currentBlockType, newOrientation, currentBlockPosition)) {
currentBlock.removeClass(currentBlockType + '-' + currentBlockOrientation);
currentBlock.addClass(currentBlockType + '-' + newOrientation);
currentBlockOrientation = newOrientation;
}
break;
case 39: // right arrow
var newPosition = [currentBlockPosition[0], currentBlockPosition[1] + 1];
if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) {
currentBlock.css('left', newPosition[1] * blockWidth + 'px');
currentBlockPosition = newPosition;
}
break;
case 40: // down arrow
moveBlockDown();
break;
case 32: // space bar
var newOrientation = (currentBlockOrientation + 1) % 4;
if (isValidPosition(currentBlockType, newOrientation, currentBlockPosition)) {
currentBlock.removeClass(currentBlockType + '-' + currentBlockOrientation);
currentBlock.addClass(currentBlockType + '-' + newOrientation);
currentBlockOrientation = newOrientation;
}
break;
case 13: // enter key
if (currentBlockTimer !== null) {
clearInterval(currentBlockTimer);
currentBlockTimer = null;
} else {
currentBlockTimer = setInterval(moveBlockDown, 500);
}
break;
}
}
});
createNewBlock();
});
</script>
</head>
<body>
<div id="game-board"></div>
<div id="score-board">0</div>
<div id="high-score-board"></div>
</body>
</html>
```