用javascript实现一个俄罗斯方块游戏
时间: 2023-12-31 17:07:10 浏览: 159
好的,以下是用 JavaScript 实现俄罗斯方块游戏的代码:
HTML 代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Tetris</title>
<style>
canvas {
border: 1px solid #000;
margin: 0 auto;
display: block;
}
</style>
</head>
<body>
<canvas id="canvas" width="240" height="400"></canvas>
<script src="tetris.js"></script>
</body>
</html>
```
JavaScript 代码:
```
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var score = 0;
var level = 1;
var speed = 1000;
var interval = setInterval(moveDown, speed);
var board = [];
var currentPiece = null;
var nextPiece = null;
var pieces = [
{
shape: [[1, 1], [1, 1]],
color: "#00FFFF"
},
{
shape: [[1, 1, 1], [0, 1, 0]],
color: "#FF00FF"
},
{
shape: [[1, 1, 1], [1, 0, 0]],
color: "#FFFF00"
},
{
shape: [[1, 1, 0], [0, 1, 1]],
color: "#00FF00"
},
{
shape: [[0, 1, 1], [1, 1, 0]],
color: "#FF0000"
},
{
shape: [[1, 1, 1, 1]],
color: "#0000FF"
},
{
shape: [[1, 1, 1], [0, 0, 1]],
color: "#FFA500"
}
];
var pieceColors = [
"#00FFFF",
"#FF00FF",
"#FFFF00",
"#00FF00",
"#FF0000",
"#0000FF",
"#FFA500"
];
function createBoard() {
for (var i = 0; i < 20; i++) {
board[i] = [];
for (var j = 0; j < 10; j++) {
board[i][j] = null;
}
}
}
function createPiece() {
var index = Math.floor(Math.random() * pieces.length);
var piece = pieces[index];
var x = Math.floor(Math.random() * (10 - piece.shape[0].length));
currentPiece = {
shape: piece.shape,
color: piece.color,
x: x,
y: 0
};
nextPiece = {
shape: pieces[Math.floor(Math.random() * pieces.length)].shape,
color: pieceColors[Math.floor(Math.random() * pieceColors.length)]
};
}
function drawPiece(piece, x, y) {
ctx.fillStyle = piece.color;
for (var i = 0; i < piece.shape.length; i++) {
for (var j = 0; j < piece.shape[i].length; j++) {
if (piece.shape[i][j]) {
ctx.fillRect((x + j) * 20, (y + i) * 20, 20, 20);
ctx.strokeRect((x + j) * 20, (y + i) * 20, 20, 20);
}
}
}
}
function drawNextPiece() {
ctx.clearRect(240, 0, 100, 400);
ctx.fillStyle = nextPiece.color;
for (var i = 0; i < nextPiece.shape.length; i++) {
for (var j = 0; j < nextPiece.shape[i].length; j++) {
if (nextPiece.shape[i][j]) {
ctx.fillRect((j + 11) * 10, (i + 6) * 10, 10, 10);
ctx.strokeRect((j + 11) * 10, (i + 6) * 10, 10, 10);
}
}
}
}
function drawBoard() {
ctx.clearRect(0, 0, 240, 400);
for (var i = 0; i < 20; i++) {
for (var j = 0; j < 10; j++) {
if (board[i][j]) {
ctx.fillStyle = board[i][j];
ctx.fillRect(j * 20, i * 20, 20, 20);
ctx.strokeRect(j * 20, i * 20, 20, 20);
}
}
}
}
function moveDown() {
if (canMoveDown(currentPiece)) {
currentPiece.y++;
draw();
} else {
placePiece();
checkRows();
createPiece();
if (!canMoveDown(currentPiece)) {
gameOver();
}
}
}
function canMoveDown(piece) {
for (var i = 0; i < piece.shape.length; i++) {
for (var j = 0; j < piece.shape[i].length; j++) {
if (piece.shape[i][j]) {
if (piece.y + i + 1 >= 20 ||
board[piece.y + i + 1][piece.x + j]) {
return false;
}
}
}
}
return true;
}
function canMoveLeft(piece) {
for (var i = 0; i < piece.shape.length; i++) {
for (var j = 0; j < piece.shape[i].length; j++) {
if (piece.shape[i][j]) {
if (piece.x + j - 1 < 0 || board[piece.y + i][piece.x + j - 1]) {
return false;
}
}
}
}
return true;
}
function canMoveRight(piece) {
for (var i = 0; i < piece.shape.length; i++) {
for (var j = 0; j < piece.shape[i].length; j++) {
if (piece.shape[i][j]) {
if (piece.x + j + 1 >= 10 || board[piece.y + i][piece.x + j + 1]) {
return false;
}
}
}
}
return true;
}
function placePiece() {
for (var i = 0; i < currentPiece.shape.length; i++) {
for (var j = 0; j < currentPiece.shape[i].length; j++) {
if (currentPiece.shape[i][j]) {
board[currentPiece.y + i][currentPiece.x + j] = currentPiece.color;
}
}
}
}
function checkRows() {
var rowsCleared = 0;
for (var i = 0; i < 20; i++) {
var full = true;
for (var j = 0; j < 10; j++) {
if (!board[i][j]) {
full = false;
break;
}
}
if (full) {
rowsCleared++;
board.splice(i, 1);
board.unshift(new Array(10).fill(null));
}
}
if (rowsCleared > 0) {
score += rowsCleared * 100;
level = Math.floor(score / 1000) + 1;
speed = 1000 - (level - 1) * 100;
clearInterval(interval);
interval = setInterval(moveDown, speed);
}
}
function gameOver() {
clearInterval(interval);
ctx.font = "20px Arial";
ctx.fillStyle = "#000";
ctx.fillText("Game Over", 70, 200);
}
function draw() {
drawBoard();
drawPiece(currentPiece, currentPiece.x, currentPiece.y);
drawNextPiece();
ctx.font = "20px Arial";
ctx.fillStyle = "#000";
ctx.fillText("Score: " + score, 250, 100);
ctx.fillText("Level: " + level, 250, 150);
}
createBoard();
createPiece();
draw();
document.addEventListener("keydown", function(event) {
if (event.keyCode == 37 && canMoveLeft(currentPiece)) {
currentPiece.x--;
draw();
} else if (event.keyCode == 39 && canMoveRight(currentPiece)) {
currentPiece.x++;
draw();
} else if (event.keyCode == 40) {
moveDown();
} else if (event.keyCode == 38) {
var rotatedPiece = {
shape: [],
color: currentPiece.color,
x: currentPiece.x,
y: currentPiece.y
};
for (var i = 0; i < currentPiece.shape[0].length; i++) {
var row = [];
for (var j = currentPiece.shape.length - 1; j >= 0; j--) {
row.push(currentPiece.shape[j][i]);
}
rotatedPiece.shape.push(row);
}
if (canRotate(rotatedPiece)) {
currentPiece = rotatedPiece;
draw();
}
}
});
function canRotate(piece) {
for (var i = 0; i < piece.shape.length; i++) {
for (var j = 0; j < piece.shape[i].length; j++) {
if (piece.shape[i][j]) {
if (piece.y + i >= 20 ||
piece.x + j < 0 ||
piece.x + j >= 10 ||
board[piece.y + i][piece.x + j]) {
return false;
}
}
}
}
return true;
}
```
这是一个比较完整的俄罗斯方块游戏的实现,包括了游戏的基本逻辑和界面展示。你可以将这些代码复制到你的 HTML 文件中,并在浏览器中运行以查看效果。
阅读全文