C#winform俄罗斯方块代码
时间: 2023-08-11 10:05:57 浏览: 167
当然,下面是一个简单的 C# WinForms 俄罗斯方块游戏的示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TetrisGame
{
public partial class MainForm : Form
{
private const int BoardWidth = 10;
private const int BoardHeight = 20;
private Timer gameTimer;
private bool[,] gameBoard;
private int currentPieceX;
private int currentPieceY;
private int score;
private Piece currentPiece;
private static readonly Color[] PieceColors = { Color.Black, Color.Cyan, Color.Yellow, Color.Purple, Color.Blue, Color.Green, Color.Orange, Color.Red };
public MainForm()
{
InitializeComponent();
InitializeGame();
}
private void InitializeGame()
{
gameBoard = new bool[BoardWidth, BoardHeight];
gameTimer = new Timer { Interval = 500 };
gameTimer.Tick += GameTimer_Tick;
gameTimer.Start();
score = 0;
currentPiece = GetRandomPiece();
currentPieceX = 0;
currentPieceY = 0;
}
private void GameTimer_Tick(object sender, EventArgs e)
{
MovePieceDown();
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
MovePieceLeft();
break;
case Keys.Right:
MovePieceRight();
break;
case Keys.Down:
MovePieceDown();
break;
case Keys.Up:
RotatePiece();
break;
}
}
private void MovePieceLeft()
{
if (CanMovePiece(currentPiece, currentPieceX - 1, currentPieceY))
{
currentPieceX--;
Refresh();
}
}
private void MovePieceRight()
{
if (CanMovePiece(currentPiece, currentPieceX + 1, currentPieceY))
{
currentPieceX++;
Refresh();
}
}
private void MovePieceDown()
{
if (CanMovePiece(currentPiece, currentPieceX, currentPieceY + 1))
{
currentPieceY++;
Refresh();
}
else
{
MergePiece();
CheckLines();
currentPiece = GetRandomPiece();
currentPieceX = 0;
currentPieceY = 0;
if (!CanMovePiece(currentPiece, currentPieceX, currentPieceY))
{
gameTimer.Stop();
MessageBox.Show("Game Over! Score: " + score);
InitializeGame();
}
}
}
private void RotatePiece()
{
Piece rotatedPiece = currentPiece.Rotate();
if (CanMovePiece(rotatedPiece, currentPieceX, currentPieceY))
{
currentPiece = rotatedPiece;
Refresh();
}
}
private bool CanMovePiece(Piece piece, int x, int y)
{
for (int i = 0; i < Piece.PieceSize; i++)
{
for (int j = 0; j < Piece.PieceSize; j++)
{
if (piece.Shape[i, j])
{
int boardX = x + j;
int boardY = y + i;
if (boardX < 0 || boardX >= BoardWidth || boardY >= BoardHeight ||
(boardY >= 0 && gameBoard[boardX, boardY]))
{
return false;
}
}
}
}
return true;
}
private void MergePiece()
{
for (int i = 0; i < Piece.PieceSize; i++)
{
for (int j = 0; j < Piece.PieceSize; j++)
{
if (currentPiece.Shape[i, j])
{
gameBoard[currentPieceX + j, currentPieceY + i] = true;
}
}
}
}
private void CheckLines()
{
for (int i = BoardHeight - 1; i >= 0; i--)
{
bool lineComplete = true;
for (int j = 0; j < BoardWidth; j++)
{
if (!gameBoard[j, i])
{
lineComplete = false;
break;
}
}
if (lineComplete)
{
RemoveLine(i);
score += 100;
}
}
}
private void RemoveLine(int row)
{
for (int i = row; i > 0; i--)
{
for (int j = 0; j < BoardWidth; j++)
{
gameBoard[j, i] = gameBoard[j, i - 1];
}
}
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int blockSize = Math.Min(ClientSize.Width / BoardWidth, ClientSize.Height / BoardHeight);
for (int i = 0; i < BoardHeight; i++)
{
for (int j = 0; j < BoardWidth; j++)
{
if (gameBoard[j, i])
{
g.FillRectangle(new SolidBrush(PieceColors[0]), j * blockSize, i * blockSize, blockSize, blockSize);
}
}
}
for (int i = 0; i < Piece.PieceSize; i++)
{
for (int j = 0; j < Piece.PieceSize; j++)
{
if (currentPiece.Shape[i, j])
{
int x = (currentPieceX + j) * blockSize;
int y = (currentPieceY + i) * blockSize;
int colorIndex = Array.IndexOf(Piece.Pieces, currentPiece);
g.FillRectangle(new SolidBrush(PieceColors[colorIndex]), x, y, blockSize, blockSize);
}
}
}
}
private Piece GetRandomPiece()
{
Random random = new Random();
int index = random.Next(Piece.Pieces.Length);
return Piece.Pieces[index];
}
}
public class Piece
{
public const int PieceSize = 4;
public static readonly Piece[] Pieces = {
new Piece(new bool[,]
{
{ false, false, false, false },
{ true, true, true, true },
{ false, false, false, false },
{ false, false, false, false }
}),
// More piece shapes here...
};
public bool[,] Shape { get; }
public Piece(bool[,] shape)
{
Shape = shape;
}
public Piece Rotate()
{
bool[,] rotatedShape = new bool[PieceSize, PieceSize];
for (int i = 0; i < PieceSize; i++)
{
for (int j = 0; j < PieceSize; j++)
{
rotatedShape[i, j] = Shape[j, PieceSize - 1 - i];
}
}
return new Piece(rotatedShape);
}
}
}
```
这段代码实现了一个简单的俄罗斯方块游戏,使用 C# WinForms 编写。在 `MainForm` 类中,通过定时器 `gameTimer` 控制方块的自动下落,通过键盘事件处理用户的操作。游戏面板使用二维布尔数组 `gameBoard` 表示,其中 `true` 表示有方块,`false` 表示没有方块。方块的形状使用 `Piece` 类表示,通过旋转来改变方块的形状。
请注意,这只是一个简单的示例代码。你可以根据自己的需求进行修改和扩展。希望对你有帮助!
阅读全文