目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
public static bool ifStart = false;
public static int career =;
Snake snake = new Snake();
int snake_W =;
int snake_H =;
public static bool pause = false;
public void ProtractTable(Graphics g)
{
for (int i =; i <= panel1.Width / snake_W; i++)
{
g.DrawLine(new Pen(Color.Black,), new Point(i * snake_W, 0), new Point(i * snake_W, panel1.Height));
}
for (int i =; i <= panel1.Height / snake_H; i++)
{
g.DrawLine(new Pen(Color.Black,), new Point(0, i * snake_H), new Point(panel1.Width, i * snake_H));
}
}
private void panel_Paint(object sender, PaintEventArgs e)
{
Graphics g = panel.CreateGraphics();
ProtractTable(g);
if (!ifStart)
{
Snake.timer = timer;
Snake.label = label;
snake.Ophidian(panel, snake_W);
}
else
{
for (int i =; i < Snake.List.Count; i++)
{
e.Graphics.FillRectangle(Snake.SolidB, ((Point)Snake.List[i]).X +, ((Point)Snake.List[i]).Y + 1, snake_W - 1, snake_H - 1);
}
e.Graphics.FillRectangle(Snake.SolidF, Snake.Food.X +, Snake.Food.Y + 1, snake_W - 1, snake_H - 1);
if (Snake.ifGame)
e.Graphics.DrawString("Game Over", new Font("宋体",, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130));
}
}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
NoviceCortrol(Convert.ToInt(((ToolStripMenuItem)sender).Tag.ToString()));
snake.BuildFood();
textBox.Focus();
}
public void NoviceCortrol(int n)
{
switch (n)
{
case:
{
ifStart = false;
Graphics g = panel.CreateGraphics();
g.FillRectangle(Snake.SolidD,, 0, panel1.Width, panel1.Height);
ProtractTable(g);
ifStart = true;
snake.Ophidian(panel, snake_W);
timer.Interval = career;
timer.Start();
pause = true;
label.Text = "0";
break;
}
case:
{
if (pause)
{
ifStart = true;
timer.Stop();
pause = false;
}
else
{
ifStart = true;
timer.Start();
pause = true;
}
break;
}
case:
{
timer.Stop();
Application.Exit();
break;
}
}
}
private void 初级ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (timer.Enabled == false)
{
初级ToolStripMenuItem.Checked = false;
中级ToolStripMenuItem.Checked = false;
高级ToolStripMenuItem.Checked = false;
((ToolStripMenuItem)sender).Checked = true;
switch (Convert.ToInt(((ToolStripMenuItem)sender).Tag.ToString()))
{
case:
{
career =;
break;
}
case:
{
career =;
break;
}
case:
{
career =;
break;
}
}
}
textBox.Focus();
}
private void Form_KeyDown(object sender, KeyEventArgs e)
{
int tem_n = -;
if (e.KeyCode == Keys.Right)
tem_n =;
if (e.KeyCode == Keys.Left)
tem_n =;
if (e.KeyCode == Keys.Up)
tem_n =;
if (e.KeyCode == Keys.Down)
tem_n =;
if (tem_n != - && tem_n != Snake.Aspect)
{
if (Snake.ifGame == false)
{
if (!((tem_n == && Snake.Aspect == 1 || tem_n == 1 && Snake.Aspect == 0) || (tem_n == 2 && Snake.Aspect == 3 || tem_n == 3 && Snake.Aspect == 2)))
{
Snake.Aspect = tem_n;
snake.SnakeMove(tem_n);
}
}
}
int tem_p = -;
if (e.KeyCode == Keys.F)
tem_p =;
if (e.KeyCode == Keys.F)
tem_p =;
if (e.KeyCode == Keys.F)
tem_p =;
if (tem_p != -)
NoviceCortrol(tem_p);
}
private void timer_Tick(object sender, EventArgs e)
{
snake.SnakeMove(Snake.Aspect);
}
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.初级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.中级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.高级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.控制ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.开始ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.暂停FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.退出FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.panel = new System.Windows.Forms.Panel();
this.timer = new System.Windows.Forms.Timer(this.components);
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.设置ToolStripMenuItem,
this.控制ToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip1";
this.menuStrip.Size = new System.Drawing.Size(525, 24);
this.menuStrip.TabIndex = 0;
this.menuStrip.Text = "menuStrip1";
this.设置ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.初级ToolStripMenuItem,
this.中级ToolStripMenuItem,
this.高级ToolStripMenuItem});
this.设置ToolStripMenuItem.Name = "设置ToolStripMenuItem";
this.设置ToolStripMenuItem.Size = new System.Drawing.Size(, 20);
this.设置ToolStripMenuItem.Text = "设置";
this.初级ToolStripMenuItem.Checked = true;
this.初级ToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
this.初级ToolStripMenuItem.Name = "初级ToolStripMenuItem";
this.初级ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
this.初级ToolStripMenuItem.Tag = "";
this.初级ToolStripMenuItem.Text = "初级";
this.初级ToolStripMenuItem.Click += new System.EventHandler(this.初级ToolStripMenuItem_Click);
this.中级ToolStripMenuItem.Name = "中级ToolStripMenuItem";
this.中级ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
this.中级ToolStripMenuItem.Tag = "";
this.中级ToolStripMenuItem.Text = "中级";
this.中级ToolStripMenuItem.Click += new System.EventHandler(this.初级ToolStripMenuItem_Click);
this.高级ToolStripMenuItem.Name = "高级ToolStripMenuItem";
this.高级ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
this.高级ToolStripMenuItem.Tag = "";
this.高级ToolStripMenuItem.Text = "高级";
this.高级ToolStripMenuItem.Click += new System.EventHandler(this.初级ToolStripMenuItem_Click);
this.控制ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.开始ToolStripMenuItem,
this.暂停FToolStripMenuItem,
this.退出FToolStripMenuItem});
this.控制ToolStripMenuItem.Name = "控制ToolStripMenuItem";
this.控制ToolStripMenuItem.Size = new System.Drawing.Size(, 20);
this.控制ToolStripMenuItem.Text = "控制";
this.开始ToolStripMenuItem.Name = "开始ToolStripMenuItem";
this.开始ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
this.开始ToolStripMenuItem.Tag = "";
this.开始ToolStripMenuItem.Text = "开始 &F";
this.开始ToolStripMenuItem.Click += new System.EventHandler(this.开始ToolStripMenuItem_Click);
this.暂停FToolStripMenuItem.Name = "暂停F3ToolStripMenuItem";
this.暂停FToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
this.暂停FToolStripMenuItem.Tag = "2";
this.暂停FToolStripMenuItem.Text = "暂停 &F3";
this.暂停FToolStripMenuItem.Click += new System.EventHandler(this.开始ToolStripMenuItem_Click);
this.退出FToolStripMenuItem.Name = "退出F4ToolStripMenuItem";
this.退出FToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
this.退出FToolStripMenuItem.Tag = "3";
this.退出FToolStripMenuItem.Text = "退出 &F4";
this.退出FToolStripMenuItem.Click += new System.EventHandler(this.开始ToolStripMenuItem_Click);
this.panel.Location = new System.Drawing.Point(12, 37);
this.panel.Name = "panel1";
this.panel.Size = new System.Drawing.Size(501, 401);
this.panel.TabIndex = 1;
this.panel.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
this.timer.Interval = 400;
this.timer.Tick += new System.EventHandler(this.timer1_Tick);
this.textBox.Location = new System.Drawing.Point(245, 513);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 2;
this.textBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(12, 448);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(41, 12);
this.label.TabIndex = 3;
this.label.Text = "分数:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(59, 448);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(11, 12);
this.label.TabIndex = 4;
this.label.Text = "0";
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 469);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.panel);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "Form";
this.Text = "贪吃蛇";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip;
private System.Windows.Forms.ToolStripMenuItem 设置ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 初级ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 中级ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 高级ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 控制ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 开始ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 暂停FToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 退出FToolStripMenuItem;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
}
class Snake
{
public static int Condyle =;
public static int Aspect =;
public static Point[] Place = { new Point(-, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1) };
public static Point Food = new Point(-, -1);
public static bool ifFood = false;
public static bool ifGame = false;
public static int Field_width =;
public static int Field_Height =;
public static Control control;
public static Timer timer;
public static SolidBrush SolidB = new SolidBrush(Color.Red);
public static SolidBrush SolidD = new SolidBrush(Color.LightCoral);
public static SolidBrush SolidF = new SolidBrush(Color.Blue);
public static Label label;
public static ArrayList List = new ArrayList();
Graphics g;
public void Ophidian(Control Con, int condyle)
{
Field_width = Con.Width;
Field_Height = Con.Height;
Condyle = condyle;
control = Con;
g = control.CreateGraphics();
SolidD = new SolidBrush(Con.BackColor);
for (int i =; i < Place.Length; i++)
{
Place[i].X = (Place.Length - i -) * Condyle;
Place[i].Y = (Field_Height /) - Condyle;
g.FillRectangle(SolidB, Place[i].X +, Place[i].Y + 1, Condyle - 1, Condyle - 1);
}
List = new ArrayList(Place);
ifGame = false;
Aspect =;
}
public void SnakeMove(int n)
{
Point tem_Point = new Point(-, -1);
switch (n)
{
case:
{
tem_Point.X = ((Point)List[]).X + Condyle;
tem_Point.Y = ((Point)List[]).Y;
break;
}
case:
{
tem_Point.X = ((Point)List[]).X - Condyle;
tem_Point.Y = ((Point)List[]).Y;
break;
}
case:
{
tem_Point.Y = ((Point)List[]).Y - Condyle;
tem_Point.X = ((Point)List[]).X;
break;
}
case:
{
tem_Point.Y = ((Point)List[]).Y + Condyle;
tem_Point.X = ((Point)List[]).X;
break;
}
}
BuildFood();
if (!EstimateMove(tem_Point))
{
Aspect = n;
if (!GameAborted(tem_Point))
{
ProtractSnake(tem_Point);
EatFood();
}
g.FillRectangle(SolidF, Food.X +, Food.Y + 1, Condyle - 1, Condyle - 1);
}
}
public void EatFood()
{
if (((Point)List[]) == Food)
{
List.Add(List[List.Count -]);
ifFood = false;
BuildFood();
label.Text = Convert.ToString(Convert.ToInt(label.Text) + 5);
}
}
public bool GameAborted(Point GameP)
{
bool tem_b = false;
bool tem_body = false;
for (int i =; i < List.Count; i++)
{
if (((Point)List[]) == ((Point)List[i]))
tem_body = true;
}
if (GameP.X <= - || GameP.X >= control.Width - 1 || GameP.Y <= -20 || GameP.Y >= control.Height - 1 || tem_body)
{
g.DrawString("Game Over", new Font("宋体",, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130));
ifGame = true;
timer.Stop();
tem_b = true;
}
return tem_b;
}
public bool EstimateMove(Point Ep)
{
bool tem_bool = false;
if (Ep.X == ((Point)List[]).X && Ep.Y == ((Point)List[0]).Y)
tem_bool = true;
return tem_bool;
}
public void ProtractSnake(Point Ep)
{
bool tem_bool = false;
List.Insert(, Ep);
Point tem_point = ((Point)List[List.Count-]);
List.RemoveAt(List.Count -);
for (int i =; i < List.Count - 1; i++)
{
if (tem_point == ((Point)List[i]))
tem_bool = true;
}
if (!tem_bool)
g.FillRectangle(SolidD, tem_point.X +, tem_point.Y + 1, Condyle - 1, Condyle - 1);
for (int i =; i < List.Count; i++)
{
g.FillRectangle(SolidB, ((Point)List[]).X + 1, ((Point)List[0]).Y + 1, Condyle - 1, Condyle - 1);
}
}
public void BuildFood()
{
if (ifFood == false)
{
Point tem_p = new Point(-, -1);
bool tem_bool = true;
bool tem_b = false;
while (tem_bool)
{
tem_b = false;
tem_p = RectFood();
for (int i =; i < List.Count; i++)
{
if (((Point)List[i]) == tem_p)
{
tem_b = true;
break;
}
}
if (tem_b == false)
tem_bool = false;
}
Food = tem_p;
}
ifFood = true;
}
public Point RectFood()
{
int tem_W = Field_width /;
int tem_H = Field_Height /;
Random RandW = new Random();
tem_W=RandW.Next(, tem_W - 1);
Random RandH = new Random();
tem_H = RandH.Next(, tem_H - 1);
Point tme_P = new Point(tem_W * Condyle, tem_H * Condyle);
return tme_P;
}
}