C# 全屏label控件实现的贪吃蛇。
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
namespace snake
{
public partial class Form1 : Form
{
// 定义常量
private const int BlockSize = 20; // 方块的大小
private const int Width = 40; // 游戏界面的宽度
private const int Height = 30; // 游戏界面的高度
// 记录分数
private int Score = 0;
// 地图的二维数组
private Label[][] Blocks = new Label[Width][];
// 存储地图上每个方块的引用
Label[,] map = new Label[Height, Width];
// 贪吃蛇的移动方向
private char moveDirection;
// 食物的位置
private int food_i, food_j;
// 游戏是否失败
private bool isFailure;
// 初始化游戏
private void InitializeGame()
{
// 将地图上的方块标记为0
for (int i = 0; i < Height; i++)
for (int j = 0; j < Width; j++)
Blocks[i][j].Tag = 0;
// 设置初始移动方向为向右
moveDirection = 'D';
// 设置蛇的初始位置和长度
Blocks[10][10].Tag = 1;
for (int i = 1; i <= 5; i++)
{
Blocks[10][10 - i].Tag = i + 1;
}
// 在随机位置生成食物
Random random = new Random();
food_i = random.Next(2, Height - 5);
food_j = random.Next(2, Width - 5);
// 设置窗体标题
Text = "贪吃小蛇蛇 上下左右键盘操作方向";
}
// 定时器的回调函数,控制游戏逻辑
private void Timer_Tick(object sender, EventArgs e)
{
// 如果游戏失败,则退出
if (isFailure)
{
return;
}
// 移动贪吃蛇
MoveSnake();
// 更新界面显示
Show1();
}
// 更新界面显示
private void Show1()
{
// 遍历地图上的每个方块
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
int value = (int)Blocks[i][j].Tag;
// 根据方块的值设置背景颜色
Color color = value > 0 ? Color.FromArgb(111, 111, 111) : Color.FromArgb(222, 222, 222);
Blocks[i][j].BackColor = color;
// 设置方块的位置
Blocks[i][j].Top = i * BlockSize;
Blocks[i][j].Left = j * BlockSize;
}
}
// 将食物方块的背景颜色设置为红色
Blocks[food_i][food_j].BackColor = Color.Red;
Blocks[food_i][food_j].Top = food_i * BlockSize;
Blocks[food_i][food_j].Left = food_j * BlockSize;
// 如果游戏失败,停止定时器,弹出提示框
if (isFailure)
{
timer1.Enabled = false;
MessageBox.Show("Game Over~!!");
button1.Enabled = true;
}
}
// 移动贪吃蛇
private void MoveSnake()
{
// 将蛇身体上的方块值加一
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
if ((int)Blocks[i][j].Tag > 0)
{
Blocks[i][j].Tag = (int)Blocks[i][j].Tag + 1;
}
}
}
// 找到蛇尾和蛇头的位置
int oldTail_i = 0, oldTail_j = 0, oldHead_i = 0, oldHead_j = 0;
int max_value = 0;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
int value = (int)Blocks[i][j].Tag;
if (max_value < value)
{
max_value = value;
oldTail_i = i;
oldTail_j = j;
}
if (value == 2)
{
oldHead_i = i;
oldHead_j = j;
}
}
}
// 计算蛇头新的位置
int newHead_i = oldHead_i;
int newHead_j = oldHead_j;
if (moveDirection == 'W')
{
newHead_i = oldHead_i - 1;
}
else if (moveDirection == 'S')
{
newHead_i = oldHead_i + 1;
}
else if (moveDirection == 'A')
{
newHead_j = oldHead_j - 1;
}
else if (moveDirection == 'D')
{
newHead_j = oldHead_j + 1;
}
// 判断游戏是否失败
if (newHead_i >= Height || newHead_i < 0 || newHead_j >= Width || newHead_j < 0 || (int)Blocks[newHead_i][newHead_j].Tag > 0)
{
isFailure = true;
return;
}
// 更新蛇头的位置
Blocks[newHead_i][newHead_j].Tag = 1;
// 判断是否吃到食物
if (newHead_i == food_i && newHead_j == food_j)
{
// 在随机位置生成新的食物
Random random = new Random();
food_i = random.Next(2, Height - 5);
food_j = random.Next(2, Width - 5);
// 增加分数
Score += 1;
txtScore.Text = "" + Score;
}
else
{
// 更新蛇尾的位置
Blocks[oldTail_i][oldTail_j].Tag = 0;
}
}
public Form1()
{
InitializeComponent();
// 创建地图上的方块并添加到窗体中
for (int i = 0; i < Height; i++)
{
Blocks[i] = new Label[Width];
for (int j = 0; j < Width; j++)
{
Blocks[i][j] = new Label();
Blocks[i][j].Height = BlockSize;
Blocks[i][j].Width = BlockSize;
Blocks[i][j].Tag = 0;
Controls.Add(Blocks[i][j]);
}
}
}
// 开始按钮的点击事件
private void button1_Click(object sender, EventArgs e)
{
// 初始化游戏
InitializeGame();
// 启动定时器,控制游戏逻辑
timer1.Start();
// 重置分数
Score = 0;
txtScore.Text = "" + Score;
// 禁用开始按钮
button1.Enabled = false;
}
// 键盘按下事件
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// 根据按键设置移动方向
switch (e.KeyCode)
{
case Keys.Up:
if (moveDirection != 'S')
{
moveDirection = 'W';
MoveSnake();
}
break;
case Keys.Left:
if (moveDirection != 'D')
{
moveDirection = 'A';
MoveSnake();
}
break;
case Keys.Down:
if (moveDirection != 'W')
{
moveDirection = 'S';
MoveSnake();
}
break;
case Keys.Right:
if (moveDirection != 'A')
{
moveDirection = 'D';
MoveSnake();
}
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
namespace snake
{
public partial class Form1 : Form
{
private const int BlockSize = 20;
private const int Width = 40;
private const int Height = 30;
private int Score = 0;
private Label[][] Blocks= new Label[Width][];//地图的二维数组
Label[,] map = new Label[Height, Width];
private char moveDirection;
private int food_i, food_j;
private bool isFailure;
private void InitializeGame()
{
for (int i = 0; i < Height; i++)
for (int j = 0; j<Width; j++)
Blocks[i][j].Tag = 0;
moveDirection = 'D';
food_i = 0;
food_j = 0;
isFailure = false;
Blocks[10][10].Tag =1;
for (int i = 1; i <= 5; i++)
{
Blocks[10][10 - i].Tag =i + 1;
}
Random random = new Random();
food_i = random.Next(2, Height - 5);
food_j = random.Next(2, Width - 5);
Text = "贪吃小蛇蛇 上下左右键盘操作方向";
}
private void Timer_Tick(object sender, EventArgs e)
{
if (isFailure)
{
return;
}
MoveSnake();
Show1();
}
private void Show1() {
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
int value = (int)Blocks[i][j].Tag;
Color color = value > 0 ? Color.FromArgb(111, 111, 111) : Color.FromArgb(222, 222, 222);
Blocks[i][j].BackColor = color;
Blocks[i][j].Top=i * BlockSize;
Blocks[i][j].Left= j * BlockSize;
}
}
Blocks[food_i][food_j].BackColor = Color.Red;
Blocks[food_i][food_j ].Top=food_i * BlockSize;
Blocks[food_i][food_j ].Left= food_j * BlockSize;
if (isFailure) {
timer1.Enabled = false;
MessageBox.Show("Game Over~!!");
button1.Enabled = true;
}
}
private void MoveSnake()
{
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
if ((int)Blocks[i][j].Tag > 0)
{
Blocks[i][j].Tag = (int)Blocks[i][j].Tag+1;
}
}
}
int oldTail_i = 0, oldTail_j = 0, oldHead_i = 0, oldHead_j = 0;
int max_value = 0;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
int value = (int)Blocks[i][j].Tag;
if (max_value < value)
{
max_value = value;
oldTail_i = i;
oldTail_j = j;
}
if (value == 2)
{
oldHead_i = i;
oldHead_j = j;
}
}
}
int newHead_i = oldHead_i;
int newHead_j = oldHead_j;
if (moveDirection == 'W')
{
newHead_i = oldHead_i - 1;
}
else if (moveDirection == 'S')
{
newHead_i = oldHead_i + 1;
}
else if (moveDirection == 'A')
{
newHead_j = oldHead_j - 1;
}
else if (moveDirection == 'D')
{
newHead_j = oldHead_j + 1;
}
if (newHead_i >= Height || newHead_i < 0 || newHead_j >= Width || newHead_j < 0 || (int)Blocks[newHead_i][newHead_j].Tag > 0)
{
isFailure = true;
return;
}
Blocks[newHead_i][newHead_j].Tag = 1;
if (newHead_i == food_i && newHead_j == food_j)
{
Random random = new Random();
food_i = random.Next(2, Height - 5);
food_j = random.Next(2, Width - 5);
Score +=1;
txtScore.Text=""+Score;
}
else
{
Blocks[oldTail_i][oldTail_j].Tag = 0;
}
}
public Form1()
{
InitializeComponent();
for (int i = 0; i < Height; i++)
{
Blocks[i] = new Label[Width];
for (int j = 0; j<Width; j++)
{
Blocks[i][j] = new Label();
Blocks[i][j].Height=BlockSize;
Blocks[i][j].Width =BlockSize;
Blocks[i][j].Tag = 0;
Controls.Add(Blocks[i][j]);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
InitializeGame();
timer1.Start();
Score = 0;
txtScore.Text = ""+Score;
button1.Enabled = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
if (moveDirection != 'S')
{
moveDirection = 'W';
MoveSnake();
}
break;
case Keys.Left:
if (moveDirection != 'D')
{
moveDirection = 'A';
MoveSnake();
}
break;
case Keys.Down:
if (moveDirection != 'W')
{
moveDirection = 'S';
MoveSnake();
}
break;
case Keys.Right:
if (moveDirection != 'A')
{
moveDirection = 'D';
MoveSnake();
}
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}