【VC++开源代码栏目提醒】:网学会员,鉴于大家对VC++开源代码十分关注,论文会员在此为大家搜集整理了“C#.net__贪吃蛇源码 - 其它资料”一文,供大家参考学习!
贪吃蛇源码界面:MainDialog所用到的控件:menuStrip timer在这的 Timer 是用来控制蛇的速度OptionDialog:这个 Gameover 的说一下,这个文本框是用来输入姓名,当分数大于前此的分数时记录,也就是保存最高分。
好了,下面是
代码:MainDialog:using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOnamespace Snake_Class_ public partial class Form1 : Form public Form1 InitializeComponent g pictureBox1.CreateGraphics region 变量 public QueueltPointgt queue new QueueltPointgt private int snakeLongprivate int snakeSpeedpublic Graphics gprivate int snakeScore0private Keys keyKeys.Leftprivate int tempkeykeya37Snake snake new SnakeFood food new FoodWall wall new Wallendregionregion 属性public int SnakeLong get return snakeLong set snakeLong value public int SnakeSpeed get return snakeSpeed set snakeSpeed value public int SnakeScore get return snakeScore set snakeScore value endregionprotected override bool ProcessDialogKeyKeys keyData switch keyData case Keys.Return: timer1.Start break case Keys.Space: timer1.Stop break key keyData return falseprivate void 退出ToolStripMenuItem_Clickobject sender EventArgs e this.Closeprivate void 选项ToolStripMenuItem_Clickobject sender EventArgs e OptionDialog opDia new OptionDialog opDia.ShowDialog SnakeLong OptionDialog.SnakeLong SnakeSpeed OptionDialog.SnakeSpeedprivate void 关于ToolStripMenuItem_Clickobject sender EventArgs e AboutDialog abd new AboutDialog abd.ShowDialogthisprivate void 查看帮助ToolStripMenuItem_Clickobject sender EventArgs e HelpDialog helD new HelpDialog helD.ShowDialogthispublic void NewGame Bitmap b new BitmapDirectory.GetCurrentDirectory quotRed.jpgquot SolidBrush sb new SolidBrushBackColor queue.Clear food.FoodFlog true Pen p new PenColor.Blue 2 g.FillRectanglesb 0 0 420 420 for int i 0 i lt 28 i g.DrawImageb i 15 01515 g.DrawImageb 0 i 151515 g.DrawImagebi154051515 g.DrawImageb405i151515 Point temp for int i 0 i lt SnakeLong 1 i temp new Pointi 6 15 90 queue.Enqueuetemp private void 新游戏ToolStripMenuItem_Clickobject sender EventArgs e timer1.Stop try StreamReader sr File.OpenTextOptionDialog.Path SnakeLong int.Parsesr.ReadLine SnakeSpeed int.Parsesr.ReadLine sr.Close catch Exception MessageBox.Showquot发生未知错误quot quot提示quot MessageBoxButtons.OKMessageBoxIcon.Asterisk NewGame food.FoodFlogtrue snake.CreateSnakequeueBackColorthisfood snakeScore 0 wall.WallQueue.Clear wall.CreateWallgfoodthis timer1.Start private void timer1_Tickobject sender EventArgs e this.label1.Text snakeScoreSnakeSpeed100.ToString if SnakeSpeed 10 GameOverquot恭喜通关!quot switch key case Keys.Up: tempkey 38 break case Keys.Left: tempkey 37 break case Keys.Right: tempkey 39 break case Keys.Down: tempkey 40 break if Math.Abstempkey - keya 1 Math.Abstempkey - keya 3 keya tempkey this.timer1.Interval 1000 - SnakeSpeed 100 snake.MoveSnakekeyaqueueBackColorthisfoodwall public void GameOverstring text this.timer1.Stop GameOverDialog god new GameOverDialogtextthis.label1.Text god.ShowDialog private void 统计信息ToolStripMenuItem_Clickobject sender EventArgs e StatMessDialog smd new StatMessDialog smd.ShowDialog Gameover 窗口:using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOnamespace Snake_Class_ public partial class GameOverDialog : Form public GameOverDialogstring textstring scroetext InitializeComponent this.Text text scroetxt scroetext private string scorePath Directory.GetCurrentDirectoryquotScore.snaquot private int scroe 0 private string scroetxtnull private string name class NUllException : Exception public NUllException : base private void button1_Clickobject sender EventArgs e try StreamReader sr File.OpenTextscorePath scroe int.Parsesr.ReadLine name sr.ReadLine sr.Close catch Exception MessageBox.Showquot发生未知错误quot quot提示quot MessageBoxButtons.OKMessageBoxIcon.Asterisk return if scroe lt int.Parsescroetxt this.textBox1.Visible true this.label1.Text quot请输入s.label1.Location new Point1224 else this.label1.Location new Point76 59 this.label1.Text quot游戏结束!quot this.Close StreamWriter sw File.CreateTextscorePath try if scroe lt int.Parsescroetxt sw.WriteLinescroetxt sw.WriteLinethis.textBox1.Text else sw.WriteLinescroe sw.WriteLinename catch Exception MessageBox.Showquot发生未知错误quotquot提示quotMessageBoxButtons.OKMessageBoxIcon.Asterisk finally sw.Close try if textBox1.Visible true if textBox1.Text quotquot throw new NUllException else this. Close catch Exception MessageBox.Showquot姓名不能为空quot quot提示quot MessageBoxButtons.OKMessageBoxIcon.Asterisk OptionDialog窗口:using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOnamespace Snake_Class_ public partial class OptionDialog : Form public OptionDialog InitializeComponent private static int snakeLong4 private static int snakeSpeed1 private static string path Directory.GetCurrentDirectory quotOption.snaquot public static string Path get return path set path value public static int SnakeLong get return snakeLong set snakeLong value public static int SnakeSpeed get return snakeSpeed set snakeSpeed value private void button1_Clickobject sender EventArgs e try SnakeLongint.Parsethis.numericUpDown2.Value.ToString SnakeSpeedint.Parsethis.numericUpDown1.Value.ToString catch Exception MessageBox.Showquot提示quotquot输入错误!quotMessageBoxButtons.OKMessageBoxIcon.Error try StreamWriter sw File.CreateTextpath sw.WriteLinethis.numericUpDown2.Value.ToString sw.WriteLinethis.numericUpDown1.Value.ToString sw.Close catch Exception MessageBox.Showquot发生未知错误quot quot提示quot MessageBoxButtons.OKMessageBoxIcon.Asterisk Close private void button2_Clickobject sender EventArgs e Close private void OptionDialog_Loadobject sender EventArgs e try StreamReader sr File.OpenTextpath this.numericUpDown2.Value int.Parsesr.ReadLine this.numericUpDown1.Value int.Parsesr.ReadLine sr.Close catch Exception MessageBox.Showquot发生未知错误quot quot提示quot MessageBoxButtons.OKMessageBoxIcon.Asterisk this.Close StatMessDialog.cs 窗口using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOnamespace Snake_Class_ public partial class StatMessDialog : Form public StatMessDialog InitializeComponent string path Directory.GetCurrentDirectory quotScore.snaquot private void button1_Clickobject sender EventArgs e Close private void button2_Clickobject sender EventArgs e StreamWriter sw File.CreateTextpath sw.WriteLinequot0quot sw.WriteLinequotDefaultPlayquot sw.Close this.label4.Text quotDefaultPlayquot this.label5.Text quot0quot private void StatMessDialog_Loadobject sender EventArgs e try StreamReader sr File.OpenTextpath this.label5.Text sr.ReadLine this.label4.Text sr.ReadLine sr.Close catch Exception MessageBox.Showquot发生未知错误quot quot提示quot MessageBoxButtons.OKMessageBoxIcon.Asterisk Food.csFood 类using Systemusing System.Collections.Genericusing System.Linqusing System.Textusing System.Drawingusing System.IOnamespace Snake_Class_ class Food region 变量 private bool foodFlog private Point foodP endregion region 属性 public bool FoodFlog get return foodFlog set foodFlog value public Point FoodP get return foodP set foodP value endregionpublic Food FoodPnew Point 00public void CreatFoodForm1 form//食物创建 bool flog true Random rD new Random int rdX 0 rdY 0 int fpath rD.Next13 string pathquotYellow.gifquot if fpath 1 path quotYellow.gifquot else path quotBlue.gifquot Bitmap b new BitmapDirectory.GetCurrentDirectory path while foodFlog rdX rD.Next1 20 15 rdY rD.Next1 20 15 for int i 0 i lt form.SnakeLong i if form.queue.ElementAti.X rdX ampamp form.queue.ElementAti.Y rdY flog false if flog form.g.DrawImageb rdX 1 rdY 1 15 15 foodFlog false foodP.X rdX foodP.Y rdY public void HaveFoodPoint pForm1 form//吃食物事件 if p foodP form.SnakeScore 1 foodFlog true form.queue.Enqueuep form.SnakeLong 1 if form.SnakeScore 5 0 ampamp form.SnakeScore 0 form.SnakeSpeed Snake.csSnake 类using Systemusing System.Collections.Genericusing System.Linqusing System.Textusing System.Drawingusing System.Windows.Formsusing System.Drawing.Drawing2Dusing System.IOnamespace Snake_Class_ public interface ISGameover void GameOverPoint snakePoint Form1 form class Snake:ISGameover public delegate void EatfoodPoint p Form1 form static event Eatfood eatfood public void MoveSnakeint keyQueueltPointgt queueColor BackColorForm1 formFoodfoodWall wall Point temp Point temp2 // eatfood - new Eatfoodfood.HaveFood eatfood new Eatfoodfood.HaveFood SolidBrush sb new SolidBrushBackColor temp2 temp queue.Dequeue switch key case 37: temp.X - 15 break case 38: temp.Y - 15 break case 39: temp.X 15 break case 40: temp.Y 15 break GameOvertemp form wall.GameOvertemp form queue.Enqueuetemp form.g.FillRectanglesb temp.X 1 temp.Y 1 16 14 Bitmap b new BitmapDirectory.GetCurrentDirectory quotBlue.jpgquot form.g.DrawImageb temp.X 1 temp.Y 1 15 15 if temp food.FoodP eatfood temp form b new BitmapDirectory.GetCurrentDirectory quotYellow.jpgquot for int i 0 i lt form.SnakeLong-1 i temp queue.Dequeue queue.Enqueuetemp2 form.g.DrawImageb temp2.X 1 temp2.Y 1 15 15 temp2 temp temp queue.Dequeue queue.Enqueuetemp2 form.g.FillRectanglesb temp2.X 1 temp2.Y 1 15 15 temp2 temp if food.FoodFlog food.CreatFoodformpublic void CreateSnake QueueltPointgt queue Color BackColor Form1 form Food food SolidBrush sb new SolidBrushBackColor Bitmap b new BitmapDirectory.GetCurrentDirectory quotBlue.jpgquot form.g.DrawImageb queue.ElementAt0.X 1 queue.ElementAt0.Y 1 15 15 b new BitmapDirectory.GetCurrentDirectory quotYellow.jpgquot for int i 1 i lt form.SnakeLong i form.g.DrawImageb queue.ElementAti.X 1queue.ElementAti.Y 1 15 15 .