protected override void OnPaint(PaintEventArgs e) { // 获取当前窗体的坐标 Rectangle rect = this.Bounds; // 网格的列数 col = rect.Width / xs; // 网格的行数 row = rect.Height / xs; //this.Height = col * xs; //this.Width = row * xs + 6; int drawRow = 0; int drawCol = 0; // 实例化画笔。 Pen p = new Pen(Brushes.LightBlue); // 设置画笔的样式(虚线)。 p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; // 画水平线 for (int i = 0; i <= row; i++) { e.Graphics.DrawLine(p, 0, drawCol, rect.Width, drawCol); drawCol += xs; } // 画垂直线 for (int j = 0; j <= col; j++) { e.Graphics.DrawLine(p, drawRow, 0, drawRow, drawCol); drawRow += xs; } if (load == 1) { all = new int[row, col]; point = new Point[row, col]; direct = new int[row, col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { all[i, j] = 0; direct[i, j] = 3; point[i, j] = new Point(i * xs, j * xs); } } all[0, 0] = 1; // means snake self load = 0; list.Add(new Point(0, 0)); set_direct(); } else { int ret = produce_food(); if (ret == 1) { food_status = 1; } check_food(); label1.Text = food_cur.ToString(); show(e.Graphics); move(); } }