添加完成响应函数之后是没有实现的,需要自己实现函数体,代码都在 Form1.cs中修改。

下面就是我实现的代码:格式很乱!!没办法(看不清的话我会将源代码放到网上,下载链接在下面)

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Snake{        public partial class Form1 : Form    {        Snake snake;        Point food;                int[,] data;        int unitWidth, unitHeight;        int xNum, yNum;        Pen bgPen;        Pen greenPen;        Pen whitePen;        Pen yellowPen;        Pen redPen;        public Form1()        {            InitializeComponent();            food = new Point();            xNum = 10;            yNum = 10;            data = new int[xNum, yNum];            bgPen = new Pen(Color.Black);            whitePen = new Pen(Color.White);            greenPen = new Pen(Color.Green);            yellowPen = new Pen(Color.Yellow);            redPen = new Pen(Color.Red);            snake = new Snake() ;            this.pBox.Image = new System.Drawing.Bitmap(this.pBox.Size.Width, this.pBox.Size.Height);            this.unitWidth = this.pBox.Image.Width / this.yNum - 1;            this.unitHeight = this.pBox.Image.Height / this.xNum - 1;            reDrawPBox();            this.timer1.Enabled = false;            initFace();        }        void initFace()        {            snake.addHead();        }                void gameOver()        {            Graphics g = Graphics.FromImage(this.pBox.Image);            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 20);            g.DrawString("GAME OVER", font, this.redPen.Brush, this.pBox.Image.Width / 2 - 80, this.pBox.Image.Height / 3);            this.pBox.Refresh();            font.Dispose();            g.Dispose();        }        void drawMemory()        {            snake.initArr(data, xNum, yNum);            for (int i = 0; i < snake.SQ.Count; i++)            {                data[snake.SQ.ElementAt(i).X, snake.SQ.ElementAt(i).Y] = 2;            }            data[snake.Head.X, snake.Head.Y] = 1 ;            data[snake.Food.X, snake.Food.Y] = 3; // 食物        }                void reDrawPBox()        {            Graphics g = Graphics.FromImage(pBox.Image);            Pen otPen = null;            g.FillRectangle(bgPen.Brush, 0, 0, this.pBox.Image.Width, this.pBox.Image.Height);            for (int i = 0; i < this.xNum; i++)            {                for (int j = 0; j < this.yNum; j++)                {                    if (data[i, j] == 0) otPen = whitePen;                    else if(data[i, j] == 1)otPen = redPen ;                    else if (data[i, j] == 2) otPen = greenPen;                    else if (data[i, j] == 3) otPen = yellowPen;                    g.FillRectangle(otPen.Brush,j * (unitWidth + 1), i * (unitHeight + 1), unitWidth, unitHeight);                }            }            pBox.Refresh();            g.Dispose();        }        private void onStartMenuClick(object sender, EventArgs e)        {            snake.SQ.Clear();            snake.addHead();            snake.initArr(data, xNum, yNum);            this.timer1.Enabled = true;        }        private void timer1_Tick(object sender, EventArgs e)        {            if (snake.isOver())            {                gameOver();                this.timer1.Enabled = false;                return;            }            snake.next();            drawMemory();            reDrawPBox();        }        private void onKeyUp(object sender, KeyEventArgs e)        {            if(e.KeyCode == Keys.Up){                snake.up();            }else if(e.KeyCode == Keys.Left){                snake.left();            }else if(e.KeyCode == Keys.Down){                snake.down();            }            else if (e.KeyCode == Keys.Right)            {                snake.right();            }                  }        private void onStopMenuClick(object sender, EventArgs e)        {                        if (this.stopToolStripMenuItem.Text == "继续")            {                this.timer1.Enabled = true;                this.stopToolStripMenuItem.Text = "暂停";            }            else            {                this.timer1.Enabled = false;                this.stopToolStripMenuItem.Text = "继续";            }        }        private void onExitClick(object sender, EventArgs e)        {            Environment.Exit(0);        }                private void onKeyDown(object sender, KeyEventArgs e)        {            if (e.KeyCode == Keys.Up)            {                snake.up();            }            else if (e.KeyCode == Keys.Left)            {                snake.left();            }            else if (e.KeyCode == Keys.Down)            {                snake.down();            }            else if (e.KeyCode == Keys.Right)            {                snake.right();            }            if (snake.isOver())            {                gameOver();                this.timer1.Enabled = false;                return;            }            snake.next();            drawMemory();            reDrawPBox();        }    }    }

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐