C#貪吃蛇(窗體版本)
阿新 • • 發佈:2019-01-02
新建
在工具箱中找到 Timer,拖拽使用,將Name改為dt,Interval(單位:毫秒)改為500
然後編寫程式碼
string key_name = "start"; //記錄鍵盤狀態
Label[] l_b = new Label[30]; //貪吃蛇身體陣列
Random r = new Random(); //隨機數
int a = 0, b = 0; //變數,記錄座標
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//設定初始介面狀態
this.Top = 120;
this.Left = 120;
this.Width = 800;
this.Height = 600;
this.BackColor = Color.Black;
//造 蛇 身體,長度為5
for (int i = 0; i < 5; i++)
{
Label lb = new Label();
lb.Width = lb.Height = 20;
lb.Top = 400;
lb.Left = 400 - i * 20;
lb.BackColor = Color.Red;
lb.Text = "O";
lb.Font = new System.Drawing.Font("宋體", 18);
lb.Tag = i;
l_b[i] = lb;
this .Controls.Add(lb);
}
//控制元件Timer
dt.Tick += new EventHandler(dt_Tick);
//鍵盤敲擊事件
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
snake_food(); //造 蛇 的食物函式
dt.Start(); //Timer 開始
}
void dt_Tick(object sender, EventArgs e)
{
int x_1, y_1;
x_1 = l_b[0].Left;
y_1 = l_b[0].Top;
if (key_name == "start") //鍵盤狀態處於初始狀態
{
l_b[0].Left = x_1 + 20;
snake_move(x_1, y_1);
}
if (key_name == "Right") //鍵盤狀態處於 向右 狀態
{
l_b[0].Left = x_1 + 20;
snake_move(x_1, y_1);
}
if (key_name == "Up") //鍵盤狀態處於 向上 狀態
{
l_b[0].Top = y_1 - 20;
snake_move(x_1, y_1);
}
if (key_name == "Down") //鍵盤狀態處於 向下 狀態
{
l_b[0].Top = y_1 + 20;
snake_move(x_1, y_1);
}
if (key_name == "Left") //鍵盤狀態處於 向左 狀態
{
l_b[0].Left = x_1 - 20;
snake_move(x_1, y_1);
}
// 穿牆設定
if (x_1 > 800)
{
l_b[0].Left = 0; ;
}
if (x_1 < 0)
{
l_b[0].Left = 800;
}
if (y_1 > 600)
{
l_b[0].Top = 0;
}
if (y_1 < 0)
{
l_b[0].Left = 600;
}
//每動一次,判斷是否與食物重合
eat_time();
}
//敲擊鍵盤響應
void Form1_KeyDown(object sender, KeyEventArgs e)
{
//記錄蛇頭座標
int x_1, y_1;
x_1 = l_b[0].Left;
y_1 =l_b[0].Top;
//獲取按了什麼鍵
key_name = e.KeyCode.ToString();
if (e.KeyCode.ToString() == "Right") //向右
{
l_b[0].Left= x_1 + 20;
snake_move(x_1, y_1);
}
if (e.KeyCode.ToString() == "Up") //向上
{
l_b[0].Top= y_1 - 20;
snake_move(x_1, y_1);
}
if (e.KeyCode.ToString() == "Down") //向下
{
l_b[0].Top= y_1 + 20;
snake_move(x_1, y_1);
}
if (e.KeyCode.ToString() == "Left") //向左
{
l_b[0].Left= x_1 - 20;
snake_move(x_1, y_1);
}
//每按一次,判斷是否與食物重合
eat_time();
}
//吃的一瞬間
void eat_time()
{
double x1 = 20, y1 = 20, x2 = 20, y2 = 20;
foreach (Label lb in this.Controls)
{
if (lb.Tag.ToString() == "food".ToString())
{
x2 = lb.Left;
y2 = lb.Top;
}
if (lb.Tag.ToString() == "0".ToString())
{
x1 = lb.Left; //左
y1 = lb.Top; //上
}
}
if (x2==x1&&y2==y1)
{
snake_eat();
foreach (Label lb in this.Controls)
{
if (lb.Tag.ToString() == "food".ToString())
{
lb.Top = r.Next(1, 30) * 20;
lb.Left = r.Next(1, 30) * 20;
}
}
}
}
//蛇移動函式
/*主要思路:保證 蛇 的每一塊,移動的下次方向為這塊(第N塊)的前一塊(N-1)*/
void snake_move(int x_1, int y_1)
{
int xx = 0;
int yy = 0;
for (int i = 1; l_b[i] != null; i++)
{
if (i >= 3)
{
xx = a;
yy = b;
}
if (i == 1)
{
xx =l_b[i].Left;
yy = l_b[i].Top;
l_b[i].Left= x_1;
l_b[i].Top= y_1;
}
else
{
a =l_b[i].Left;
b = l_b[i].Top;
l_b[i].Left= xx;
l_b[i].Top= yy;
}
}
}
//製造食物
void snake_food()
{
double xx = l_b[0].Left;
double yy = l_b[0].Top;
Label lb = new Label();
lb.Width = 20;
lb.Height = 20;
lb.Top= r.Next(1, 30) * 20;
lb.Left= r.Next(1, 30) * 20;
lb.Tag = "food";
lb.BackColor = Color.Yellow;
this.Controls.Add(lb);
}
//吃過的食物,變為 蛇 的一部分
void snake_eat()
{
int i = 0;
for (; l_b[i] != null; i++)
;
Label lb = new Label();
lb.Width = lb.Height = 20;
lb.Top = b;
lb.Left = a;
lb.BackColor = Color.Red;
lb.Text = "O";
lb.Font = new System.Drawing.Font("宋體", 18);
lb.Tag = i;
l_b[i] = lb;
this.Controls.Add(lb);
}