C# 写个 俄罗斯方块
时间: 2023-08-30 16:10:58 浏览: 151
C#写的俄罗斯方块
好的,下面是一个简单的 C# 俄罗斯方块游戏代码:
```csharp
using System;
using System.Threading;
namespace Tetris
{
class Program
{
// 游戏区域大小
const int Width = 10;
const int Height = 20;
// 方块图形数组
static readonly int[][][] Tetrominoes = new int[][][]
{
new int[][] // I
{
new int[] { 1, 1, 1, 1 }
},
new int[][] // J
{
new int[] { 1, 1, 1 },
new int[] { 0, 0, 1 }
},
new int[][] // L
{
new int[] { 1, 1, 1 },
new int[] { 1, 0, 0 }
},
new int[][] // O
{
new int[] { 1, 1 },
new int[] { 1, 1 }
},
new int[][] // S
{
new int[] { 0, 1, 1 },
new int[] { 1, 1, 0 }
},
new int[][] // T
{
new int[] { 1, 1, 1 },
new int[] { 0, 1, 0 }
},
new int[][] // Z
{
new int[] { 1, 1, 0 },
new int[] { 0, 1, 1 }
}
};
// 随机数生成器
static readonly Random Random = new Random();
// 当前方块
static int[][] CurrentTetromino;
static int CurrentX;
static int CurrentY;
// 游戏区域
static readonly int[][] Board = new int[Height][];
static int Score;
// 控制台输出锁
static readonly object ConsoleLock = new object();
static void Main(string[] args)
{
// 初始化游戏区域
for (int i = 0; i < Height; i++)
{
Board[i] = new int[Width];
}
// 开始游戏循环
while (true)
{
// 产生新的方块
CurrentTetromino = Tetrominoes[Random.Next(Tetrominoes.Length)];
CurrentX = Width / 2 - CurrentTetromino[0].Length / 2;
CurrentY = 0;
// 游戏循环
while (true)
{
// 处理用户输入
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.LeftArrow:
if (IsLegalMove(CurrentTetromino, CurrentX - 1, CurrentY))
{
CurrentX--;
}
break;
case ConsoleKey.RightArrow:
if (IsLegalMove(CurrentTetromino, CurrentX + 1, CurrentY))
{
CurrentX++;
}
break;
case ConsoleKey.DownArrow:
if (IsLegalMove(CurrentTetromino, CurrentX, CurrentY + 1))
{
CurrentY++;
}
break;
case ConsoleKey.Spacebar:
// 下落到底部
while (IsLegalMove(CurrentTetromino, CurrentX, CurrentY + 1))
{
CurrentY++;
}
break;
}
}
// 计算下一帧
var nextTetrominoY = CurrentY + 1;
if (IsLegalMove(CurrentTetromino, CurrentX, nextTetrominoY))
{
CurrentY = nextTetrominoY;
}
else
{
// 将当前方块放入游戏区域
for (int i = 0; i < CurrentTetromino.Length; i++)
{
for (int j = 0; j < CurrentTetromino[i].Length; j++)
{
if (CurrentTetromino[i][j] == 1)
{
Board[CurrentY + i][CurrentX + j] = 1;
}
}
}
// 消除满行
int linesCleared = 0;
for (int i = 0; i < Height; i++)
{
if (IsRowFull(i))
{
ClearRow(i);
linesCleared++;
}
}
// 更新分数
Score += linesCleared * linesCleared;
// 判断游戏是否结束
if (IsGameOver())
{
lock (ConsoleLock)
{
Console.SetCursorPosition(0, Height + 1);
Console.WriteLine("Game over! Your score is {0}.", Score);
}
return;
}
// 跳出当前方块的下落循环
break;
}
// 渲染游戏区域
lock (ConsoleLock)
{
Console.Clear();
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
Console.Write(Board[i][j] == 1 ? "#" : ".");
}
Console.WriteLine();
}
Console.WriteLine("Score: {0}", Score);
}
// 暂停一段时间
Thread.Sleep(100);
}
}
}
// 判断当前方块是否可以移动到指定位置
static bool IsLegalMove(int[][] tetromino, int x, int y)
{
for (int i = 0; i < tetromino.Length; i++)
{
for (int j = 0; j < tetromino[i].Length; j++)
{
if (tetromino[i][j] == 1)
{
int boardX = x + j;
int boardY = y + i;
if (boardX < 0 || boardX >= Width || boardY < 0 || boardY >= Height || Board[boardY][boardX] == 1)
{
return false;
}
}
}
}
return true;
}
// 判断指定行是否已满
static bool IsRowFull(int row)
{
for (int j = 0; j < Width; j++)
{
if (Board[row][j] == 0)
{
return false;
}
}
return true;
}
// 清除指定行
static void ClearRow(int row)
{
for (int i = row; i > 0; i--)
{
for (int j = 0; j < Width; j++)
{
Board[i][j] = Board[i - 1][j];
}
}
}
// 判断游戏是否结束
static bool IsGameOver()
{
for (int j = 0; j < Width; j++)
{
if (Board[0][j] == 1)
{
return true;
}
}
return false;
}
}
}
```
这个代码实现了一个简单的俄罗斯方块游戏,使用控制台进行输出。玩家可以使用方向键来移动方块,使用空格键加速方块下落。游戏结束时会显示分数。
阅读全文