C# winform 2048游戏完整代码
时间: 2023-07-12 15:05:47 浏览: 172
以下是一个基于C# Winform的2048游戏的完整代码,希望能对您有帮助:
```
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 _2048
{
public partial class Form1 : Form
{
private int[,] matrix = new int[4, 4];
private int score = 0;
private Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitMatrix();
UpdateMatrix();
}
private void InitMatrix()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
matrix[i, j] = 0;
}
}
AddNumber();
AddNumber();
}
private void AddNumber()
{
int x = rand.Next(4);
int y = rand.Next(4);
while (matrix[x, y] != 0)
{
x = rand.Next(4);
y = rand.Next(4);
}
matrix[x, y] = rand.Next(10) == 0 ? 4 : 2;
}
private void UpdateMatrix()
{
scoreLabel.Text = "Score: " + score;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
switch (matrix[i, j])
{
case 0:
Controls["label" + (i * 4 + j)].Text = "";
Controls["label" + (i * 4 + j)].BackColor = SystemColors.Control;
break;
case 2:
Controls["label" + (i * 4 + j)].Text = "2";
Controls["label" + (i * 4 + j)].BackColor = Color.LightYellow;
break;
case 4:
Controls["label" + (i * 4 + j)].Text = "4";
Controls["label" + (i * 4 + j)].BackColor = Color.LightGreen;
break;
case 8:
Controls["label" + (i * 4 + j)].Text = "8";
Controls["label" + (i * 4 + j)].BackColor = Color.LightBlue;
break;
case 16:
Controls["label" + (i * 4 + j)].Text = "16";
Controls["label" + (i * 4 + j)].BackColor = Color.LightSkyBlue;
break;
case 32:
Controls["label" + (i * 4 + j)].Text = "32";
Controls["label" + (i * 4 + j)].BackColor = Color.Blue;
break;
case 64:
Controls["label" + (i * 4 + j)].Text = "64";
Controls["label" + (i * 4 + j)].BackColor = Color.DarkBlue;
break;
case 128:
Controls["label" + (i * 4 + j)].Text = "128";
Controls["label" + (i * 4 + j)].BackColor = Color.Orange;
break;
case 256:
Controls["label" + (i * 4 + j)].Text = "256";
Controls["label" + (i * 4 + j)].BackColor = Color.OrangeRed;
break;
case 512:
Controls["label" + (i * 4 + j)].Text = "512";
Controls["label" + (i * 4 + j)].BackColor = Color.DarkRed;
break;
case 1024:
Controls["label" + (i * 4 + j)].Text = "1024";
Controls["label" + (i * 4 + j)].BackColor = Color.Purple;
break;
case 2048:
Controls["label" + (i * 4 + j)].Text = "2048";
Controls["label" + (i * 4 + j)].BackColor = Color.DarkMagenta;
break;
default:
Controls["label" + (i * 4 + j)].Text = "" + matrix[i, j];
Controls["label" + (i * 4 + j)].BackColor = Color.Black;
break;
}
}
}
}
private bool IsGameOver()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (matrix[i, j] == 0)
{
return false;
}
if (i < 3 && matrix[i, j] == matrix[i + 1, j])
{
return false;
}
if (j < 3 && matrix[i, j] == matrix[i, j + 1])
{
return false;
}
}
}
return true;
}
private bool MoveLeft()
{
bool isMoved = false;
for (int i = 0; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
if (matrix[i, j] != 0)
{
int k = j - 1;
while (k >= 0 && matrix[i, k] == 0)
{
k--;
}
if (k >= 0 && matrix[i, k] == matrix[i, j])
{
matrix[i, k] *= 2;
score += matrix[i, k];
matrix[i, j] = 0;
isMoved = true;
}
else if (k < j - 1)
{
matrix[i, k + 1] = matrix[i, j];
matrix[i, j] = 0;
isMoved = true;
}
}
}
}
return isMoved;
}
private bool MoveRight()
{
bool isMoved = false;
for (int i = 0; i < 4; i++)
{
for (int j = 2; j >= 0; j--)
{
if (matrix[i, j] != 0)
{
int k = j + 1;
while (k <= 3 && matrix[i, k] == 0)
{
k++;
}
if (k <= 3 && matrix[i, k] == matrix[i, j])
{
matrix[i, k] *= 2;
score += matrix[i, k];
matrix[i, j] = 0;
isMoved = true;
}
else if (k > j + 1)
{
matrix[i, k - 1] = matrix[i, j];
matrix[i, j] = 0;
isMoved = true;
}
}
}
}
return isMoved;
}
private bool MoveUp()
{
bool isMoved = false;
for (int j = 0; j < 4; j++)
{
for (int i = 1; i < 4; i++)
{
if (matrix[i, j] != 0)
{
int k = i - 1;
while (k >= 0 && matrix[k, j] == 0)
{
k--;
}
if (k >= 0 && matrix[k, j] == matrix[i, j])
{
matrix[k, j] *= 2;
score += matrix[k, j];
matrix[i, j] = 0;
isMoved = true;
}
else if (k < i - 1)
{
matrix[k + 1, j] = matrix[i, j];
matrix[i, j] = 0;
isMoved = true;
}
}
}
}
return isMoved;
}
private bool MoveDown()
{
bool isMoved = false;
for (int j = 0; j < 4; j++)
{
for (int i = 2; i >= 0; i--)
{
if (matrix[i, j] != 0)
{
int k = i + 1;
while (k <= 3 && matrix[k, j] == 0)
{
k++;
}
if (k <= 3 && matrix[k, j] == matrix[i, j])
{
matrix[k, j] *= 2;
score += matrix[k, j];
matrix[i, j] = 0;
isMoved = true;
}
else if (k > i + 1)
{
matrix[k - 1, j] = matrix[i, j];
matrix[i, j] = 0;
isMoved = true;
}
}
}
}
return isMoved;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
bool isMoved = false;
switch (e.KeyCode)
{
case Keys.Left:
isMoved = MoveLeft();
break;
case Keys.Right:
isMoved = MoveRight();
break;
case Keys.Up:
isMoved = MoveUp();
break;
case Keys.Down:
isMoved = MoveDown();
break;
}
if (isMoved)
{
AddNumber();
UpdateMatrix();
}
if (IsGameOver())
{
MessageBox.Show("Game Over!");
InitMatrix();
UpdateMatrix();
}
}
}
}
```
该代码中包含了2048游戏的核心逻辑,包括初始化矩阵、添加数字、更新矩阵、判断游戏是否结束、向左/右/上/下移动等。在移动时,如果合并了两个相同的方块,则分数会增加。如果无法再添加新的数字,即游戏结束,则会弹出提示框并重新开始。
阅读全文