js实现俄罗斯方块代码
时间: 2023-12-09 13:03:50 浏览: 121
js实现俄罗斯方块
好的,以下是一个简单的 JavaScript 实现俄罗斯方块的代码,供您参考:
```html
<!DOCTYPE html>
<html>
<head>
<title>Tetris Game</title>
<style>
#game-board {
width: 250px;
height: 500px;
border: 2px solid black;
margin: 0 auto;
position: relative;
}
.block {
width: 25px;
height: 25px;
position: absolute;
}
#block1 {
background-color: red;
}
#block2 {
background-color: blue;
}
#block3 {
background-color: yellow;
}
#block4 {
background-color: green;
}
</style>
</head>
<body>
<div id="game-board"></div>
<script>
// define blocks
var blocks = [
[
[1, 1],
[1, 1]
],
[
[1, 0],
[1, 0],
[1, 1]
],
[
[0, 1],
[0, 1],
[1, 1]
],
[
[1, 1, 0],
[0, 1, 1]
]
];
// define variables
var board = [];
var currentBlock = null;
var currentBlockX = 0;
var currentBlockY = 0;
var currentBlockRotation = 0;
var intervalId = null;
// initialize board
for (var i = 0; i < 20; i++) {
board[i] = [];
for (var j = 0; j < 10; j++) {
board[i][j] = 0;
}
}
// create new block
function newBlock() {
var randomIndex = Math.floor(Math.random() * blocks.length);
currentBlock = blocks[randomIndex];
currentBlockX = 3;
currentBlockY = 0;
currentBlockRotation = 0;
}
// draw block
function drawBlock() {
var blockId = "block" + (Math.floor(Math.random() * 4) + 1);
for (var i = 0; i < currentBlock.length; i++) {
for (var j = 0; j < currentBlock[i].length; j++) {
if (currentBlock[i][j] === 1) {
var div = document.createElement("div");
div.className = "block";
div.id = blockId;
div.style.top = (currentBlockY + i) * 25 + "px";
div.style.left = (currentBlockX + j) * 25 + "px";
document.getElementById("game-board").appendChild(div);
}
}
}
}
// remove block
function removeBlock() {
for (var i = 0; i < currentBlock.length; i++) {
for (var j = 0; j < currentBlock[i].length; j++) {
if (currentBlock[i][j] === 1) {
var div = document.getElementById("block" + (Math.floor(Math.random() * 4) + 1));
div.parentNode.removeChild(div);
}
}
}
}
// move block down
function moveDown() {
if (canMoveDown()) {
removeBlock();
currentBlockY += 1;
drawBlock();
} else {
// add block to board
for (var i = 0; i < currentBlock.length; i++) {
for (var j = 0; j < currentBlock[i].length; j++) {
if (currentBlock[i][j] === 1) {
board[currentBlockY + i][currentBlockX + j] = 1;
}
}
}
// check for completed rows
for (var i = 0; i < 20; i++) {
var rowCompleted = true;
for (var j = 0; j < 10; j++) {
if (board[i][j] === 0) {
rowCompleted = false;
break;
}
}
if (rowCompleted) {
board.splice(i, 1);
board.unshift([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
}
}
newBlock();
}
}
// check if block can move down
function canMoveDown() {
for (var i = 0; i < currentBlock.length; i++) {
for (var j = 0; j < currentBlock[i].length; j++) {
if (currentBlock[i][j] === 1) {
if (currentBlockY + i === 19) {
return false;
}
if (board[currentBlockY + i + 1][currentBlockX + j] === 1) {
return false;
}
}
}
}
return true;
}
// move block left
function moveLeft() {
if (canMoveLeft()) {
removeBlock();
currentBlockX -= 1;
drawBlock();
}
}
// check if block can move left
function canMoveLeft() {
for (var i = 0; i < currentBlock.length; i++) {
for (var j = 0; j < currentBlock[i].length; j++) {
if (currentBlock[i][j] === 1) {
if (currentBlockX + j === 0) {
return false;
}
if (board[currentBlockY + i][currentBlockX + j - 1] === 1) {
return false;
}
}
}
}
return true;
}
// move block right
function moveRight() {
if (canMoveRight()) {
removeBlock();
currentBlockX += 1;
drawBlock();
}
}
// check if block can move right
function canMoveRight() {
for (var i = 0; i < currentBlock.length; i++) {
for (var j = 0; j < currentBlock[i].length; j++) {
if (currentBlock[i][j] === 1) {
if (currentBlockX + j === 9) {
return false;
}
if (board[currentBlockY + i][currentBlockX + j + 1] === 1) {
return false;
}
}
}
}
return true;
}
// rotate block
function rotate() {
var rotatedBlock = [];
for (var i = 0; i < currentBlock[0].length; i++) {
rotatedBlock[i] = [];
for (var j = 0; j < currentBlock.length; j++) {
rotatedBlock[i][j] = currentBlock[currentBlock.length - 1 - j][i];
}
}
if (canRotate(rotatedBlock)) {
removeBlock();
currentBlock = rotatedBlock;
currentBlockRotation = (currentBlockRotation + 1) % 4;
drawBlock();
}
}
// check if block can rotate
function canRotate(rotatedBlock) {
for (var i = 0; i < rotatedBlock.length; i++) {
for (var j = 0; j < rotatedBlock[i].length; j++) {
if (rotatedBlock[i][j] === 1) {
if (currentBlockY + i > 19 || currentBlockX + j < 0 || currentBlockX + j > 9 || board[currentBlockY + i][currentBlockX + j] === 1) {
return false;
}
}
}
}
return true;
}
// start game
function startGame() {
newBlock();
drawBlock();
intervalId = setInterval(moveDown, 500);
}
// listen for keyboard input
document.addEventListener("keydown", function(event) {
if (event.keyCode === 37) { // left arrow
moveLeft();
} else if (event.keyCode === 38) { // up arrow
rotate();
} else if (event.keyCode === 39) { // right arrow
moveRight();
} else if (event.keyCode === 40) { // down arrow
clearInterval(intervalId);
moveDown();
intervalId = setInterval(moveDown, 500);
}
});
// start game
startGame();
</script>
</body>
</html>
```
这是一个简单的实现,还有许多可以优化的地方。
阅读全文