C#制作贪吃蛇游戏(下)
来源:网络收集 点击: 时间:2024-09-02双击窗体(注意不是双击控件),加入代码:
public class Di
{
public static int D;
public static int s = 2;
}
void Add_food()
{
Label label = new Label();
label.Name = Lab + Di.s;
label.BackColor = System.Drawing.Color.Red;
label.Size = new System.Drawing.Size(10, 10);
label.Margin = new System.Windows.Forms.Padding(0);
label.ForeColor = System.Drawing.Color.Red;
label.AutoSize = false;
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Random rd = new Random();
tag1:
label.Location = new System.Drawing.Point(rd.Next(0, 30) * 10, rd.Next(0, 30) * 10);
for (int i = 1; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); if (label.Location == lo.Location) goto tag1;
}
this.Controls.Add(label);
label.SendToBack();
panel1.SendToBack();
}
如图所示:

双击开始按钮,加入代码:
Add_food();
button1.Enabled = false;
如图所示:

双击timer1控件,加入代码:
if (Di.D == 1) { System.Windows.Forms.SendKeys.Send({DOWN}); return; };
if (Di.D == 2) { System.Windows.Forms.SendKeys.Send({UP}); return; };
if (Di.D == 3) { System.Windows.Forms.SendKeys.Send({LEFT}); return; };
if (Di.D == 4) { System.Windows.Forms.SendKeys.Send({RIGHT}); return; };
如图所示:

单击Form1,在属性中单击事件,找到KeyDown,双击,加入代码:
if (e.KeyCode == Keys.Down Di.D != 2)
{
timer1.Enabled = false;
Di.D = 1;
Label lb = (Label)this.Controls.Find(Lab + Di.s, true);
Label lbl = (Label)this.Controls.Find(Lab + (Di.s - 1), true);
Point Lo = new Point;
for (int i = 1; i = Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); Lo = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.Down Di.s 2 lbl.BackColor != System.Drawing.Color.Red Lab1.Location.Y != 290)
{
for (int i = 2; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); lo.Location = Lo;
}
}
if (lb.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y + 10))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);
Lab1.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y + 10);
lb.Location = le.Location;
this.Controls.Remove(le);
Di.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); if (lo.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y + 10)) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; }; ;
}
if (e.KeyCode == Keys.Down Lab1.Location.Y == 290) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; };
Lab1.Top = Lab1.Top + 10;
timer1.Enabled = true;
}
if (e.KeyCode == Keys.Up Di.D != 1)
{
timer1.Enabled = false;
Di.D = 2;
Label lb = (Label)this.Controls.Find(Lab + Di.s, true);
Label lbl = (Label)this.Controls.Find(Lab + (Di.s - 1), true);
Point Lo = new Point;
for (int i = 1; i = Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); Lo = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.Up Di.s 2 lbl.BackColor != System.Drawing.Color.Red Lab1.Location.Y != 0)
{
for (int i = 2; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); lo.Location = Lo;
}
}
if (lb.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y - 10))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);
Lab1.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y - 10);
lb.Location = le.Location;
this.Controls.Remove(le);
Di.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); if (lo.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y - 10)) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; }; ;
}
if (e.KeyCode == Keys.Up Lab1.Location.Y == 0) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; };
Lab1.Top = Lab1.Top - 10;
timer1.Enabled = true;
}
if (e.KeyCode == Keys.Left Di.D != 4)
{
timer1.Enabled = false;
Di.D = 3;
Label lb = (Label)this.Controls.Find(Lab + Di.s, true);
Label lbl = (Label)this.Controls.Find(Lab + (Di.s - 1), true);
Point Lo = new Point;
for (int i = 1; i = Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); Lo = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.Left Di.s 2 lbl.BackColor != System.Drawing.Color.Red Lab1.Location.X != 0)
{
for (int i = 2; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); lo.Location = Lo;
}
}
if (lb.Location == new System.Drawing.Point(Lab1.Location.X - 10, Lab1.Location.Y))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);
Lab1.Location = new System.Drawing.Point(Lab1.Location.X - 10, Lab1.Location.Y);
lb.Location = le.Location;
this.Controls.Remove(le);
Di.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); if (lo.Location == new System.Drawing.Point(Lab1.Location.X - 10, Lab1.Location.Y)) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; }; ;
}
if (e.KeyCode == Keys.Left Lab1.Location.X == 0) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; };
Lab1.Left = Lab1.Left - 10;
timer1.Enabled = true;
}
if (e.KeyCode == Keys.Right Di.D != 3)
{
timer1.Enabled = false;
Di.D = 4;
Label lb = (Label)this.Controls.Find(Lab + Di.s, true);
Label lbl = (Label)this.Controls.Find(Lab + (Di.s - 1), true);
Point Lo = new Point;
for (int i = 1; i = Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); Lo = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.Right Di.s 2 lbl.BackColor != System.Drawing.Color.Red Lab1.Location.X != 290)
{
for (int i = 2; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); lo.Location = Lo;
}
}
if (lb.Location == new System.Drawing.Point(Lab1.Location.X + 10, Lab1.Location.Y))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);
Lab1.Location = new System.Drawing.Point(Lab1.Location.X + 10, Lab1.Location.Y);
lb.Location = le.Location;
this.Controls.Remove(le);
Di.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i Di.s; i++)
{
Label lo = (Label)this.Controls.Find(Lab + i, true); if (lo.Location == new System.Drawing.Point(Lab1.Location.X + 10, Lab1.Location.Y)) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; }; ;
}
if (e.KeyCode == Keys.Right Lab1.Location.X == 290) { timer1.Enabled = false; MessageBox.Show(游戏结束!, 提示); return; };
Lab1.Left = Lab1.Left + 10;
timer1.Enabled = true;
}
如图所示:

至此,所有工作完成,按F5即可查看效果,如下图所示:

版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_1188877.html