广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    C#制作贪吃蛇游戏(下)

    来源:网络收集  点击:  时间:2024-09-02
    【导读】:
    C#贪吃蛇游戏设计(代码部分)工具/原料more以Microsoft Visual C# 2010 Express为例方法/步骤1/5分步阅读

    双击窗体(注意不是双击控件),加入代码:

    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();

    }

    如图所示:

    2/5

    双击开始按钮,加入代码:

    Add_food();

    button1.Enabled = false;

    如图所示:

    3/5

    双击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; };

    如图所示:

    4/5

    单击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;

    }

    如图所示:

    5/5

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

    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_1188877.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-04 20:52:48  耗时:0.024
    0.0242s