用HTML和JavaScript生成一个俄罗斯方块游戏
时间: 2023-11-28 16:50:50 浏览: 142
好的,下面是一个简单的俄罗斯方块游戏实现的HTML和JavaScript代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Tetris Game</title>
<style>
#game-board {
width: 200px;
height: 400px;
border: 1px solid black;
margin: 0 auto;
position: relative;
}
.block {
width: 20px;
height: 20px;
position: absolute;
border: 1px solid black;
}
.row {
display: flex;
flex-direction: row;
}
</style>
</head>
<body>
<div id="game-board">
</div>
<script>
// 定义方块的类型和颜色
const BLOCK_TYPES = [
{
color: "red",
shape: [
[1, 1],
[1, 1]
]
},
{
color: "blue",
shape: [
[0, 1, 0],
[0, 1, 0],
[0, 1, 1]
]
},
{
color: "green",
shape: [
[0, 1, 1],
[1, 1, 0],
[0, 0, 0]
]
},
{
color: "yellow",
shape: [
[1, 1, 0],
[0, 1, 1],
[0, 0, 0]
]
},
{
color: "purple",
shape: [
[0, 1, 0],
[1, 1, 1],
[0, 0, 0]
]
},
{
color: "orange",
shape: [
[1, 1, 0],
[1, 0, 0],
[1, 0, 0]
]
},
{
color: "pink",
shape: [
[0, 1, 0],
[1, 1, 0],
[1, 0, 0]
]
}
];
// 游戏区域
const ROWS = 20;
const COLS = 10;
// 初始化游戏区域
let gameBoard = [];
for (let i = 0; i < ROWS; i++) {
let row = [];
for (let j = 0; j < COLS; j++) {
row.push(0);
}
gameBoard.push(row);
}
// 显示游戏区域
function drawGameBoard() {
let board = document.getElementById("game-board");
board.innerHTML = "";
for (let i = 0; i < ROWS; i++) {
let row = document.createElement("div");
row.classList.add("row");
for (let j = 0; j < COLS; j++) {
let block = document.createElement("div");
block.classList.add("block");
if (gameBoard[i][j] === 1) {
block.style.backgroundColor = "black";
}
row.appendChild(block);
}
board.appendChild(row);
}
}
// 在游戏区域中添加方块
function addBlock(block) {
for (let i = 0; i < block.shape.length; i++) {
for (let j = 0; j < block.shape[i].length; j++) {
if (block.shape[i][j] === 1) {
gameBoard[block.y + i][block.x + j] = 1;
}
}
}
}
// 移动方块
function moveBlock(block, direction) {
switch (direction) {
case "down":
if (canMove(block, 0, 1)) {
block.y++;
} else {
addBlock(block);
checkRows();
newBlock();
}
break;
case "left":
if (canMove(block, -1, 0)) {
block.x--;
}
break;
case "right":
if (canMove(block, 1, 0)) {
block.x++;
}
break;
case "rotate":
let rotatedBlock = rotateBlock(block);
if (canMove(rotatedBlock, 0, 0)) {
block.shape = rotatedBlock.shape;
}
break;
}
}
// 判断方块是否可以移动
function canMove(block, dx, dy) {
for (let i = 0; i < block.shape.length; i++) {
for (let j = 0; j < block.shape[i].length; j++) {
if (block.shape[i][j] === 1) {
let x = block.x + j + dx;
let y = block.y + i + dy;
if (x < 0 || x >= COLS || y >= ROWS) {
return false;
}
if (y >= 0 && gameBoard[y][x] === 1) {
return false;
}
}
}
}
return true;
}
// 旋转方块
function rotateBlock(block) {
let rotated = [];
for (let i = 0; i < block.shape.length; i++) {
let row = [];
for (let j = 0; j < block.shape[i].length; j++) {
row.push(block.shape[block.shape.length - j - 1][i]);
}
rotated.push(row);
}
return {
x: block.x,
y: block.y,
shape: rotated
};
}
// 检查整行是否被填满
function checkRows() {
for (let i = ROWS - 1; i >= 0; i--) {
if (gameBoard[i].every((value) => value === 1)) {
gameBoard.splice(i, 1);
gameBoard.unshift(new Array(COLS).fill(0));
}
}
}
// 随机生成一个新的方块
function newBlock() {
let block = BLOCK_TYPES[Math.floor(Math.random() * BLOCK_TYPES.length)];
block.x = Math.floor((COLS - block.shape[0].length) / 2);
block.y = 0;
currentBlock = block;
}
let currentBlock;
newBlock();
// 游戏循环
setInterval(() => {
moveBlock(currentBlock, "down");
drawGameBoard();
}, 500);
// 监听键盘事件
document.addEventListener("keydown", (event) => {
switch (event.code) {
case "ArrowLeft":
moveBlock(currentBlock, "left");
break;
case "ArrowRight":
moveBlock(currentBlock, "right");
break;
case "ArrowDown":
moveBlock(currentBlock, "down");
break;
case "ArrowUp":
moveBlock(currentBlock, "rotate");
break;
}
drawGameBoard();
});
</script>
</body>
</html>
```
这个实现可能不是非常完善,但是可以作为一个起点来开始学习如何用HTML和JavaScript实现俄罗斯方块游戏。
阅读全文