您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 贪吃蛇C#net
  所属分类: .Net
  开发工具:
  文件大小: 81kb
  下载次数: 0
  上传时间: 2014-12-17
  提 供 者: missyou*******
 详细说明: 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; using System.Collections; namespace 贪吃蛇 { public partial class Form1 : Form { public int speed = 350; //用于设置贪吃蛇的速度 public static bool ifStart = false; //用于游戏是否开始 public static bool pause = true; //用于游戏是否暂停,游戏暂停为true Snake snake = new Snake();//实例化Snake类 Food food = new Food(); public Form1() { InitializeComponent(); } //绘制游戏场景 public void ProtractTable(Graphics G) { //绘制单元格的纵向线 for (int i = 0; i <= panel1.Width / Snake.SnakeBone; i++) { G.DrawLine(new Pen(Color.LightGreen, 1), new Point(i * Snake.SnakeBone, 0), new Point(i * Snake.SnakeBone, panel1.Height)); } //绘制单元格的横向线 for (int i = 0; i <= panel1.Height / Snake.SnakeBone; i++) { G.DrawLine(new Pen(Color.LightGreen, 1), new Point(0, i * Snake.SnakeBone), new Point(panel1.Width, i * Snake.SnakeBone)); } } private void panel1_Paint(object sender, PaintEventArgs e) { Graphics G = panel1.CreateGraphics();//创建panel1控件的Graphics类 ProtractTable(G); //绘制游戏场景 if (!ifStart) //如果没有开始游戏 { Snake.timer = timer1; Snake.label = label1; //初始化场地及贪吃蛇信息; //Control c = (Control)panel1; Snake.GamePlaceSet(panel1, Snake.SnakeBone); } else { for (int i = 0; i < Snake.List.Count; i++)//绘制蛇体 { e.Graphics.FillRectangle(Snake.SolidSnake, ((Point)Snake.List[i]).X + 1, ((Point)Snake.List[i]).Y + 1, Snake.SnakeBone - 1, Snake.SnakeBone - 1); } //绘制食物 e.Graphics.FillRectangle(Food.SolidFood, Food.FoodPoint.X + 1, Food.FoodPoint.Y + 1, Snake.SnakeBone - 1, Snake.SnakeBone - 1); if (Snake.isGame)//如果游戏结束 { e.Graphics.DrawString("游戏结束!", new Font("宋体", 50, FontStyle.Bold), new SolidBrush(Color.Yellow), new PointF(100, 150)); } } } private void 初级ToolStripMenuItem_Click(object sender, EventArgs e) {//在游戏没有开始和游戏结束时才可以选择设置 if ((ifStart == false) || Snake.isGame) {初级ToolStripMenuItem.Checked = false;//设置初级项被选中 中级ToolStripMenuItem.Checked = false;//设置中级项被选中 高级ToolStripMenuItem.Checked = false;//设置高级项目被选中 ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 speed = 350; label.Focus();//获得焦点 } } private void 中级ToolStripMenuItem_Click(object sender, EventArgs e) { //在游戏没有开始和游戏结束时才可以选择设置 if ((ifStart == false) || Snake.isGame) { 初级ToolStripMenuItem.Checked = false;//设置初级项被选中 中级ToolStripMenuItem.Checked = false;//设置中级项被选中 高级ToolStripMenuItem.Checked = false;//设置高级项目被选中 ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 speed = 250;//中级 label.Focus();//获得焦点 } } private void 高级ToolStripMenuItem_Click(object sender, EventArgs e) {//在游戏没有开始和游戏结束时才可以选择设置 if ((ifStart == false) || (Snake.isGame)) { 初级ToolStripMenuItem.Checked = false;//设置初级项被选中 中级ToolStripMenuItem.Checked = false;//设置中级项被选中 高级ToolStripMenuItem.Checked = false;//设置高级项目被选中 ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 speed = 200;//高级 label.Focus();//获得焦点 } } private void 开始sapceToolStripMenuItem_Click(object sender, EventArgs e) { //控制游戏的开始、暂停、和退出 ifStart = false; Graphics G = panel1.CreateGraphics();//创建panel1控件的Graphics类 //刷新游戏场地 G.FillRectangle(Snake.SolidBack, 0, 0, panel1.Width, panel1.Height); ProtractTable(G);//绘制游戏场地 ifStart = true;//开始游戏 //初始化场地以及贪吃蛇信息 Snake.GamePlaceSet(panel1, Snake.SnakeBone); timer1.Interval = speed;//设置贪吃蛇的移动速度 timer1.Start();//启动计时器 pause = false;//游戏初始状态,暂停取消 label.Text = "0";//显示当前分数 Food.Buildfood(Snake.hasFood, Snake.co);//生成食物 label.Focus();//获得焦点,便于键盘操作 } private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e) { Graphics G = panel1.CreateGraphics();//创建panel1控件的Graphics类 //如果游戏正在运行 if ((pause == false && ifStart == true) && (!Snake.isGame)) { G.DrawString("游戏暂停!", new Font("宋体", 50, FontStyle.Bold), new SolidBrush(Color.Yellow), new PointF(100, 150)); timer1.Stop();//停止当前计时器 pause = true;//当前游戏暂停 } //如果游戏正在运行 else if (pause == true && ifStart == true && (!Snake.isGame)) { G.FillRectangle(Snake.SolidBack, 0, 0, panel1.Width, panel1.Height); ProtractTable(G); timer1.Start();//启动计时器 pause = false;//开始游戏 } } private void 结束F4ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame))//如果游戏正在运行 { if (DialogResult.Yes == MessageBox.Show("是否退出游戏?", "提示", MessageBoxButtons.YesNo)) { timer1.Stop(); //停止计时器 Application.Exit(); //停止运行,退出游戏 } } else MessageBox.Show("游戏正在运行中,暂时不允许退出!", "提示"); } private void 游戏说明ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } //控制游戏的开始、暂停、和退出 public void NoviceControl(int n) { Graphics G = panel1.CreateGraphics();//创建panel1控件的Graphics类 switch (n) { case 1://开始游戏 { ifStart = false; //刷新游戏场地 G.FillRectangle(Snake.SolidBack, 0, 0, panel1.Width, panel1.Height); ProtractTable(G);//绘制游戏场地 ifStart = true;//开始游戏 //初始化场地以及贪吃蛇信息 Snake.GamePlaceSet(panel1, Snake.SnakeBone); timer1.Interval = speed;//设置贪吃蛇的移动速度 timer1.Start();//启动计时器 pause = false;//暂停游戏 Food.Buildfood(Snake.hasFood, Snake.co);//生成食物 label.Text = "0";//显示当前分数 break; } case 2://暂停游戏 {//如果游戏正在运行 if ((pause == false && ifStart == true) && (!Snake.isGame)) { G.DrawString("游戏暂停!", new Font("宋体", 50, FontStyle.Bold), new SolidBrush(Color.Yellow), new PointF(100, 150)); timer1.Stop();//停止当前计时器 pause = true;//当前游戏暂停 } //如果游戏正在运行 else if (pause == true && ifStart == true && (!Snake.isGame)) { G.FillRectangle(Snake.SolidBack, 0, 0, panel1.Width, panel1.Height); ProtractTable(G); timer1.Start();//启动计时器 pause = false;//开始游戏 } break; } case 3://退出游戏 { if ((ifStart == false) || (Snake.isGame))//如果游戏正在运行 { timer1.Stop(); //停止计时器 Application.Exit(); //停止运行,退出游戏 } break; } // case 4://打开帮助信息 // ' { if ((ifStart == false) || (Snake.isGame))//如果游戏正在运行 { // help temp = new help();//初始 一个 help实例 // if (temp.ShowDialog(this) == DialogResult.OK) // temp.Dispose(); // } // break; // } // } } } } private void Form1_KeyDown(object sender, KeyEventArgs e) { int tem_p = -1;//记录控件键值 if (e.KeyCode == Keys.F2)//如果按F2 tem_p = 1;//开始游戏 if (e.KeyCode == Keys.Space)//如果按F3 tem_p = 2;//暂停游戏或者继续游戏 if (e.KeyCode == Keys.F4)//如果按F4 tem_p = 3;//关闭游戏 if (e.KeyCode==Keys.F3) tem_p = 4;//打开帮助信息 if (tem_p != -1)//如果当前是操作标识 NoviceControl(tem_p);     //控制游戏的暂停和关闭 if (ifStart == true && pause == false)//如果游戏启动,键盘方向键才起作用 { int tem_n = -1;//记录移动键值 if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)//按下右键向右移动 tem_n = 0; if (e.KeyCode == Keys.Left || e.KeyCode == Keys.A)//按下左键向左移动 tem_n = 1; if (e.KeyCode == Keys.Up || e.KeyCode == Keys.W)//按下上键向上移动 tem_n = 2; if (e.KeyCode == Keys.Down || e.KeyCode == Keys.S)//按下键向下移动 tem_n = 3; if (tem_n != -1 && tem_n != Snake.Direct) { if (Snake.isGame == false) {//如果移动的方向不是相反方向 if (!((tem_n == 0 && Snake.Direct == 1 || tem_n == 1 && Snake.Direct == 0) || (tem_n == 2 && Snake.Direct == 3 || tem_n == 3 && Snake.Direct == 2))) { Snake.Direct = tem_n;//记录移动的方向 snake.SnakeMove(tem_n, this);//移动贪吃蛇 } } } } } private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 绿色ToolStripMenuItem.Checked = false; 红色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黑色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SolidSnake = new SolidBrush(Color.Green); } } private void 红色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 绿色ToolStripMenuItem.Checked = false; 红色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黑色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SolidSnake = new SolidBrush(Color.Red); } } private void 蓝色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 绿色ToolStripMenuItem.Checked = false; 红色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黑色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SolidSnake = new SolidBrush(Color.Blue); } } private void 黑色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 绿色ToolStripMenuItem.Checked = false; 红色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黑色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SolidSnake = new SolidBrush(Color.Black); } } private void 原始大小ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 原始大小ToolStripMenuItem.Checked = false; 放大两倍ToolStripMenuItem.Checked = false; 放大三倍ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SnakeBone = 10; } } private void 放大两倍ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 原始大小ToolStripMenuItem.Checked = false; 放大两倍ToolStripMenuItem.Checked = false; 放大三倍ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SnakeBone = 20; } } private void 放大三倍ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 原始大小ToolStripMenuItem.Checked = false; 放大两倍ToolStripMenuItem.Checked = false; 放大三倍ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Snake.SnakeBone = 30; } } private void 橙色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) {橙色ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; 紫色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Food.SolidFood = new SolidBrush(Color.Orange); } } private void 黄色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 橙色ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; 紫色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Food.SolidFood = new SolidBrush(Color.Yellow); } } private void 紫色ToolStripMenuItem_Click(object sender, EventArgs e) { if ((ifStart == false) || (Snake.isGame)) { 橙色ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; 紫色ToolStripMenuItem.Checked = false; ((ToolStripMenuItem)sender).Checked = true;//设置当前项选中 Food.SolidFood = new SolidBrush(Color.Purple); } } private void timer1_Tick(object sender, EventArgs e) { snake.SnakeMove(Snake.Direct, this);//移动贪吃蛇 } private void textBox1_TextChanged(object sender, EventArgs e) { } private void label_Click(object sender, EventArgs e) { } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: snake
 输入关键字,在本站1000多万海量源码库中尽情搜索: