C#实现贪食蛇小游戏

JerryXia 发表于 , 阅读 (931)

Food类:

namespace Snake
{
    public class Food
    {
        private Point f_point;

        public Point F_point
        {
            get { return f_point; }
            set { f_point = value; }
        }

        public void drawfood(Graphics g)
        {
            SolidBrush b = new SolidBrush(Color.White);
            Rectangle rtg = new Rectangle(f_point.X, f_point.Y, Form1.SNAKEREC, Form1.SNAKEREC);
            g.FillRectangle(b, rtg);
        }

        public Point getpoint()
        {
            Random rdm = new Random(DateTime.Now.Millisecond);
            int i = rdm.Next(0, Form1.WIDTH / Form1.SNAKEREC);
            int j = rdm.Next(0, Form1.HEIGHT / Form1.SNAKEREC);
            Point newp = new Point(i * Form1.SNAKEREC, j * Form1.SNAKEREC);
            return newp;
        }
    }
}

节点类:

namespace Snake
{
    public class Segment
    {
        private int number;
        private Point orign;

        public int Number
        {
            get { return number; }
            set { number = value; }
        }
 
        public Point Orign
        {
            get { return orign; }
            set { orign = value; }
        }

        public void drawpart(Graphics g)
        {
            Pen p = new Pen(Color.White);
            g.DrawRectangle(p, orign.X, orign.Y, Form1.SNAKEREC, Form1.SNAKEREC);
        }
    }
}

蛇类:

namespace Snake
{
    public class Snake
    {
        //Segment list
        ArrayList al_list;
        //snake head
        public Point headpoint;
        //snake direcion
        private int way = 1;

        public int Way
        {
            get { return way; }
            set { way = value; }
        }

        public Snake(int count)
        {
            Segment apart;
            Point apoint = new Point(Form1.INIT_X, Form1.INIT_y);

            al_list = new ArrayList(count);
            for (int i = 1; i <= count; i++)
            {
                apoint.X = apoint.X + Form1.SNAKEREC;//init point[0].X + 10
                apart = new Segment();
                apart.Number = i;
                apart.Orign = apoint;
                al_list.Add(apart);
                if (i == count)
                    headpoint = apoint;//last point is headpoint
            }
        }

        public void drawsnake(Graphics g)
        {
            for (int i = 0; i < al_list.Count; i++)
            {
                Segment seg = (Segment)al_list[i];
                seg.drawpart(g);//draw every point
            }
        }

        public void extendsnake()
        {
            Segment seg = new Segment();
            seg.Number = al_list.Count + 1;
            Point p;
            if (way == 0)
                //Up
                p = new Point(headpoint.X, headpoint.Y - Form1.SNAKEREC);
            else if (way == 2)
                //Down
                p = new Point(headpoint.X, headpoint.Y + Form1.SNAKEREC);
            else if (way == 3)
                //Left
                p = new Point(headpoint.X - Form1.SNAKEREC, headpoint.Y);
            else
                //Right
                p = new Point(headpoint.X + Form1.SNAKEREC, headpoint.Y);
            seg.Orign = p;
            al_list.Add(seg);//add new Segment
            headpoint = seg.Orign;//head is the new Segment
        }

        public void contractsnake()
        {
            al_list.Remove(al_list[0]);//remove: al_list[0] the last point
        }

        public bool deadsnake()
        {
            if (headpoint.X < 0 || headpoint.Y < 0 || headpoint.X >= Form1.WIDTH || headpoint.Y >= Form1.HEIGHT)
                return true;
            for (int i = 0; i < al_list.Count - 1; i++)
            {
                Segment seg = (Segment)al_list[i];
                if (seg.Orign == headpoint)
                    return true;
            }
            return false;
        }
    }
}

Fom1.cs:

namespace Snake
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /***************
         * init args
         * *************/
        public static int WIDTH = 400;
        public static int HEIGHT = 300;
        public static int SNAKEREC = 10;
        //start location
        public static int INIT_X = 30;
        public static int INIT_y = 30;

        private int TIMERINTERVAL = 70;
        private bool isStart = false;
        
        private bool i = false;

        Snake a_snake = new Snake(5);
        Food afood = new Food();

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (i)
            {
                a_snake.drawsnake(g);
                afood.drawfood(g);
            }
            if (a_snake.deadsnake())
            {
                timer1.Enabled = false;
                if (DialogResult.Yes == MessageBox.Show("Do you want to restart?", "GAME OVER", MessageBoxButtons.YesNo))
                {
                    i = false;
                    a_snake = new Snake(5);
                    afood = new Food();
                    afood.F_point = afood.getpoint();
                    g.Clear(pictureBox1.BackColor);
                }
                else
                    Application.Exit();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
            a_snake.extendsnake();
            if (a_snake.headpoint == afood.F_point)
                afood.F_point = afood.getpoint();
            else
                a_snake.contractsnake();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(i)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    i = false;
                    timer1.Enabled = false;
                }
                else 
                {
                    if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up) && a_snake.Way != 2)
                    {
                        a_snake.Way = 0;
                    }
                    if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && a_snake.Way != 3)
                    {
                        a_snake.Way = 1;
                    }
                    if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) && a_snake.Way != 0)
                    {
                        a_snake.Way = 2;
                    }
                    if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && a_snake.Way != 1)
                    {
                        a_snake.Way = 3;
                    }
                }
            }
            else
            {
                if (isStart)
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        i = true;
                        timer1.Enabled = true;
                    }
                }
                else
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        isStart = true;
                        i = true;
                        afood.F_point = afood.getpoint();
                        pictureBox1.Refresh();
                        timer1.Enabled = true;
                        timer1.Interval = TIMERINTERVAL;
                    }
                }
            }
        }
    }
}

Form1.designer.cs:

namespace Snake
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.Color.Black;
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(400, 300);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(400, 300);
            this.Controls.Add(this.pictureBox1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Auto Snake";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Timer timer1;
    }
}

添加新评论