用C#写一个俄罗斯方块游戏
时间: 2023-11-23 18:04:57 浏览: 26
以下是一个简单的 C# 俄罗斯方块游戏示例:
```csharp
using System;
using System.Collections.Generic;
namespace TetrisGame
{
public class Tetris
{
private static int BOARD_WIDTH = 10;
private static int BOARD_HEIGHT = 20;
private static int BLOCK_SIZE = 20;
private static int[,] board = new int[BOARD_WIDTH, BOARD_HEIGHT];
private static int[,] currentPiece = new int[4, 4];
private static int currentPieceX = 0;
private static int currentPieceY = 0;
private static int currentPieceType = 0;
private static int currentRotation = 0;
private static Random random = new Random();
private static List<int[,]> pieces = new List<int[,]>()
{
new int[,] { { 1, 1, 1, 1 } },
new int[,] { { 1, 1, 1 }, { 0, 1, 0 } },
new int[,] { { 1, 1, 1 }, { 1, 0, 0 } },
new int[,] { { 1, 1, 1 }, { 0, 0, 1 } },
new int[,] { { 0, 1, 1 }, { 1, 1, 0 } },
new int[,] { { 1, 1, 0 }, { 0, 1, 1 } },
new int[,] { { 0, 1, 0 }, { 1, 1, 1 } }
};
public static void Main(string[] args)
{
Console.CursorVisible = false;
while (true)
{
Console.Clear();
DrawBoard();
DrawCurrentPiece();
MoveCurrentPieceDown();
if (CheckForCollision())
{
PlaceCurrentPiece();
RemoveCompletedRows();
if (CheckForGameOver())
{
Console.Clear();
Console.WriteLine("Game Over!");
Console.ReadKey();
break;
}
SpawnNewPiece();
}
System.Threading.Thread.Sleep(500);
}
}
private static void DrawBoard()
{
for (int y = 0; y < BOARD_HEIGHT; y++)
{
for (int x = 0; x < BOARD_WIDTH; x++)
{
if (board[x, y] == 1)
{
Console.SetCursorPosition(x * BLOCK_SIZE, y);
Console.Write("█");
}
}
}
}
private static void DrawCurrentPiece()
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if (currentPiece[x, y] == 1)
{
Console.SetCursorPosition((currentPieceX + x) * BLOCK_SIZE, currentPieceY + y);
Console.Write("█");
}
}
}
}
private static void MoveCurrentPieceDown()
{
currentPieceY++;
}
private static bool CheckForCollision()
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if (currentPiece[x, y] == 1)
{
int boardX = currentPieceX + x;
int boardY = currentPieceY + y;
if (boardX < 0 || boardX >= BOARD_WIDTH || boardY >= BOARD_HEIGHT || board[boardX, boardY] == 1)
{
return true;
}
}
}
}
return false;
}
private static void PlaceCurrentPiece()
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if (currentPiece[x, y] == 1)
{
int boardX = currentPieceX + x;
int boardY = currentPieceY + y;
board[boardX, boardY] = 1;
}
}
}
}
private static void RemoveCompletedRows()
{
for (int y = BOARD_HEIGHT - 1; y >= 0; y--)
{
bool rowCompleted = true;
for (int x = 0; x < BOARD_WIDTH; x++)
{
if (board[x, y] == 0)
{
rowCompleted = false;
break;
}
}
if (rowCompleted)
{
for (int i = y; i > 0; i--)
{
for (int x = 0; x < BOARD_WIDTH; x++)
{
board[x, i] = board[x, i - 1];
}
}
y++;
}
}
}
private static bool CheckForGameOver()
{
for (int x = 0; x < BOARD_WIDTH; x++)
{
if (board[x, 0] == 1)
{
return true;
}
}
return false;
}
private static void SpawnNewPiece()
{
currentPieceType = random.Next(pieces.Count);
currentRotation = 0;
currentPieceX = BOARD_WIDTH / 2 - 2;
currentPieceY = 0;
currentPiece = (int[,])pieces[currentPieceType].Clone();
}
}
}
```
这只是一个简单的示例,可以根据需要进行修改和扩展。