Javascript写一个俄罗斯方块游戏
时间: 2024-01-01 21:04:20 浏览: 81
一个Java语言写的俄罗斯方块小游戏。因为作者刚接触Java,正在摸索着学习,代码并非原创,而是跟着别人学习。.zip
以下是一个使用JavaScript编写的俄罗斯方块游戏的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Tetris Game</title>
<style>
/* 游戏区域样式 */
#game-board {
width: 300px;
height: 600px;
border: 1px solid black;
position: relative;
}
/* 方块样式 */
.block {
width: 30px;
height: 30px;
position: absolute;
}
/* 方块颜色 */
.block-i {
background-color: cyan;
}
.block-j {
background-color: blue;
}
.block-l {
background-color: orange;
}
.block-o {
background-color: yellow;
}
.block-s {
background-color: green;
}
.block-t {
background-color: purple;
}
.block-z {
background-color: red;
}
</style>
</head>
<body>
<div id="game-board"></div>
<script>
// 游戏区域的宽度和高度
const BOARD_WIDTH = 10;
const BOARD_HEIGHT = 20;
// 方块的形状和颜色
const SHAPES = [
{ name: 'i', color: 'block-i', shape: [[1, 1, 1, 1]] },
{ name: 'j', color: 'block-j', shape: [[1, 0, 0], [1, 1, 1]] },
{ name: 'l', color: 'block-l', shape: [[0, 0, 1], [1, 1, 1]] },
{ name: 'o', color: 'block-o', shape: [[1, 1], [1, 1]] },
{ name: 's', color: 'block-s', shape: [[0, 1, 1], [1, 1, 0]] },
{ name: 't', color: 'block-t', shape: [[0, 1, 0], [1, 1, 1]] },
{ name: 'z', color: 'block-z', shape: [[1, 1, 0], [0, 1, 1]] }
];
// 游戏区域的二维数组,用于存储方块的位置
let board = [];
// 当前方块的位置和形状
let currentShape = {};
let currentX = 0;
let currentY = 0;
// 初始化游戏区域
function initBoard() {
for (let i = 0; i < BOARD_HEIGHT; i++) {
board[i] = [];
for (let j = 0; j < BOARD_WIDTH; j++) {
board[i][j] = '';
}
}
}
// 创建一个新的方块
function createNewShape() {
const randomIndex = Math.floor(Math.random() * SHAPES.length);
const shape = SHAPES[randomIndex];
currentShape = shape;
currentX = Math.floor((BOARD_WIDTH - shape.shape[0].length) / 2);
currentY = 0;
}
// 绘制游戏区域
function drawBoard() {
const gameBoard = document.getElementById('game-board');
gameBoard.innerHTML = '';
for (let i = 0; i < BOARD_HEIGHT; i++) {
for (let j = 0; j < BOARD_WIDTH; j++) {
if (board[i][j] !== '') {
const block = document.createElement('div');
block.className = `block ${board[i][j]}`;
block.style.top = `${i * 30}px`;
block.style.left = `${j * 30}px`;
gameBoard.appendChild(block);
}
}
}
}
// 绘制当前方块
function drawCurrentShape() {
const gameBoard = document.getElementById('game-board');
for (let i = 0; i < currentShape.shape.length; i++) {
for (let j = 0; j < currentShape.shape[i].length; j++) {
if (currentShape.shape[i][j] === 1) {
const block = document.createElement('div');
block.className = `block ${currentShape.color}`;
block.style.top = `${(currentY + i) * 30}px`;
block.style.left = `${(currentX + j) * 30}px`;
gameBoard.appendChild(block);
}
}
}
}
// 检查方块是否可以移动到指定位置
function canMoveTo(x, y, shape) {
for (let i = 0; i < shape.length; i++) {
for (let j = 0; j < shape[i].length; j++) {
if (shape[i][j] === 1) {
const newX = x + j;
const newY = y + i;
if (newX < 0 || newX >= BOARD_WIDTH || newY >= BOARD_HEIGHT || board[newY][newX] !== '') {
return false;
}
}
}
}
return true;
}
// 将当前方块固定在游戏区域中
function fixCurrentShape() {
for (let i = 0; i < currentShape.shape.length; i++) {
for (let j = 0; j < currentShape.shape[i].length; j++) {
if (currentShape.shape[i][j] === 1) {
const x = currentX + j;
const y = currentY + i;
board[y][x] = currentShape.color;
}
}
}
}
// 消除已经填满的行
function clearFullRows() {
for (let i = BOARD_HEIGHT - 1; i >= 0; i--) {
if (board[i].every(cell => cell !== '')) {
board.splice(i, 1);
board.unshift(new Array(BOARD_WIDTH).fill(''));
}
}
}
// 游戏结束
function gameOver() {
alert('Game Over');
initBoard();
drawBoard();
}
// 游戏循环
function gameLoop() {
if (canMoveTo(currentX, currentY + 1, currentShape.shape)) {
currentY++;
} else {
fixCurrentShape();
clearFullRows();
createNewShape();
if (!canMoveTo(currentX, currentY, currentShape.shape)) {
gameOver();
return;
}
}
drawBoard();
drawCurrentShape();
}
// 监听键盘事件
document.addEventListener('keydown', event => {
if (event.key === 'ArrowLeft' && canMoveTo(currentX - 1, currentY, currentShape.shape)) {
currentX--;
} else if (event.key === 'ArrowRight' && canMoveTo(currentX + 1, currentY, currentShape.shape)) {
currentX++;
} else if (event.key === 'ArrowDown' && canMoveTo(currentX, currentY + 1, currentShape.shape)) {
currentY++;
} else if (event.key === 'ArrowUp') {
const rotatedShape = rotateShape(currentShape.shape);
if (canMoveTo(currentX, currentY, rotatedShape)) {
currentShape.shape = rotatedShape;
}
}
});
// 旋转方块
function rotateShape(shape) {
const newShape = [];
const rows = shape.length;
const cols = shape[0].length;
for (let i = 0; i < cols; i++) {
newShape[i] = [];
for (let j = 0; j < rows; j++) {
newShape[i][j] = shape[rows - j - 1][i];
}
}
return newShape;
}
// 初始化游戏
function initGame() {
initBoard();
createNewShape();
setInterval(gameLoop, 500);
}
initGame();
</script>
</body>
</html>
```
阅读全文