c# winform 2048游戏的代码,要写明用了什么控件
时间: 2023-12-14 20:39:38 浏览: 162
以下是一个简单的C# WinForm 2048游戏的代码,使用了Label、Panel、Button、PictureBox等控件:
```csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm2048
{
public partial class Form1 : Form
{
private int[,] board = new int[4, 4];
private Label[,] labels = new Label[4, 4];
private Random random = new Random();
public Form1()
{
InitializeComponent();
InitBoard();
}
private void InitBoard()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
board[i, j] = 0;
labels[i, j] = new Label();
labels[i, j].Size = new Size(60, 60);
labels[i, j].Location = new Point(80 * j + 20, 80 * i + 20);
labels[i, j].BackColor = Color.LightGray;
labels[i, j].Font = new Font("Arial", 24, FontStyle.Bold);
labels[i, j].TextAlign = ContentAlignment.MiddleCenter;
panel1.Controls.Add(labels[i, j]);
}
}
AddNumber();
AddNumber();
}
private void AddNumber()
{
List<int[]> emptyCells = new List<int[]>();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (board[i, j] == 0)
{
emptyCells.Add(new int[] { i, j });
}
}
}
if (emptyCells.Count > 0)
{
int[] cell = emptyCells[random.Next(emptyCells.Count)];
board[cell[0], cell[1]] = random.Next(1, 3) * 2;
UpdateBoard();
}
}
private void UpdateBoard()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
labels[i, j].Text = board[i, j] > 0 ? board[i, j].ToString() : "";
labels[i, j].BackColor = GetColor(board[i, j]);
}
}
}
private Color GetColor(int value)
{
switch (value)
{
case 2: return Color.LightBlue;
case 4: return Color.Blue;
case 8: return Color.LightGreen;
case 16: return Color.Green;
case 32: return Color.Yellow;
case 64: return Color.Orange;
case 128: return Color.Pink;
case 256: return Color.Red;
case 512: return Color.Purple;
case 1024: return Color.Violet;
case 2048: return Color.Black;
default: return Color.LightGray;
}
}
private bool CanMoveLeft()
{
for (int i = 0; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
if (board[i, j] > 0 && (board[i, j - 1] == 0 || board[i, j - 1] == board[i, j]))
{
return true;
}
}
}
return false;
}
private bool CanMoveRight()
{
for (int i = 0; i < 4; i++)
{
for (int j = 2; j >= 0; j--)
{
if (board[i, j] > 0 && (board[i, j + 1] == 0 || board[i, j + 1] == board[i, j]))
{
return true;
}
}
}
return false;
}
private bool CanMoveUp()
{
for (int j = 0; j < 4; j++)
{
for (int i = 1; i < 4; i++)
{
if (board[i, j] > 0 && (board[i - 1, j] == 0 || board[i - 1, j] == board[i, j]))
{
return true;
}
}
}
return false;
}
private bool CanMoveDown()
{
for (int j = 0; j < 4; j++)
{
for (int i = 2; i >= 0; i--)
{
if (board[i, j] > 0 && (board[i + 1, j] == 0 || board[i + 1, j] == board[i, j]))
{
return true;
}
}
}
return false;
}
private void MoveLeft()
{
for (int i = 0; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
if (board[i, j] > 0)
{
int k = j - 1;
while (k >= 0 && board[i, k] == 0)
{
k--;
}
if (k >= 0 && board[i, k] == board[i, j])
{
board[i, k] *= 2;
board[i, j] = 0;
}
else
{
board[i, k + 1] = board[i, j];
board[i, j] = 0;
}
}
}
}
AddNumber();
}
private void MoveRight()
{
for (int i = 0; i < 4; i++)
{
for (int j = 2; j >= 0; j--)
{
if (board[i, j] > 0)
{
int k = j + 1;
while (k < 4 && board[i, k] == 0)
{
k++;
}
if (k < 4 && board[i, k] == board[i, j])
{
board[i, k] *= 2;
board[i, j] = 0;
}
else
{
board[i, k - 1] = board[i, j];
board[i, j] = 0;
}
}
}
}
AddNumber();
}
private void MoveUp()
{
for (int j = 0; j < 4; j++)
{
for (int i = 1; i < 4; i++)
{
if (board[i, j] > 0)
{
int k = i - 1;
while (k >= 0 && board[k, j] == 0)
{
k--;
}
if (k >= 0 && board[k, j] == board[i, j])
{
board[k, j] *= 2;
board[i, j] = 0;
}
else
{
board[k + 1, j] = board[i, j];
board[i, j] = 0;
}
}
}
}
AddNumber();
}
private void MoveDown()
{
for (int j = 0; j < 4; j++)
{
for (int i = 2; i >= 0; i--)
{
if (board[i, j] > 0)
{
int k = i + 1;
while (k < 4 && board[k, j] == 0)
{
k++;
}
if (k < 4 && board[k, j] == board[i, j])
{
board[k, j] *= 2;
board[i, j] = 0;
}
else
{
board[k - 1, j] = board[i, j];
board[i, j] = 0;
}
}
}
}
AddNumber();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left && CanMoveLeft())
{
MoveLeft();
}
else if (e.KeyCode == Keys.Right && CanMoveRight())
{
MoveRight();
}
else if (e.KeyCode == Keys.Up && CanMoveUp())
{
MoveUp();
}
else if (e.KeyCode == Keys.Down && CanMoveDown())
{
MoveDown();
}
else if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
if (!CanMoveLeft() && !CanMoveRight() && !CanMoveUp() && !CanMoveDown())
{
MessageBox.Show("Game over!");
Application.Exit();
}
}
}
}
```
其中,Panel控件用来放置所有的Label控件,PictureBox控件可以用来显示游戏结束的图片。
阅读全文