但直接以来奈何总是没有另外思路。但直接以来奈何总是没外思路。

关于贪吃蛇,一直还想做一个,但一直以来奈何总是没其余思路,经过及时几上之沉思,总算是吃来出来了

关于贪吃蛇,一直还惦记做一个,但一直以来奈何总是没有其余思路,经过这几乎天之思维,总算是被来出来了

要害行使的是 Graphics 

重大用的凡 Graphics 

兹来拘禁即张图,这张图如每一个单元格是10*10的,那么当就张图里,蛇的各级一样省便是10*10的大小

今昔来拘禁即张图,这张图如每一个单元格是10*10的,那么在当时张图里,蛇的各国一样节约便是10*10的大小

 

 

图片 1

图片 2

 

 

可看看时蛇的长短是”3″节,假得这一个单元格是10像从的话语,

好望时蛇的长短是”3″节,假得这一个单元格是10诸如从的言语,

当即3节蛇身的坐标分别是  0,0    10,0    20,0

顿时3节省蛇身的坐标分别是  0,0    10,0    20,0

默认情况下蛇应该是为右侧前进的,那么怎么学蛇身在动吗?

默认情况下蛇应该是通往右侧前进的,那么怎么套蛇身在移动吗?

思路是如此的,首先以蛇头也就算是无与伦比右边位置(坐标20,0)
的前头(坐标30,0)位置还打一个蓝色的独元格

思路是这么的,首先以蛇头也不怕是极度右侧边位置(坐标20,0)
的面前(坐标30,0)位置再度打一个蓝色之唯有元格

就算像这样

就是比如这么

图片 3

图片 4

然后擦掉最左边的蛇尾(坐标0,0)

然后擦掉最左边的蛇尾(坐标0,0)

便成为这样了

虽变成这样了

图片 5

图片 6

现今蛇身是匪是向阳右前进一步了

兹蛇身是未是望右前进一步了

实在就是将左右坐标每次+10,而即便坐标不转移

骨子里就算是将左右坐标每次+10,而即使坐标不换

然后径直重复这过程,就成为了蛇一直当通向右侧前进

接下来直接还是历程,就改为了蛇一直当为右侧前进

———————————————————–顽皮的分割线——————————————————–

———————————————————–顽皮的分割线——————————————————–

 

 

随之是前后横的题材

随后是左右横底问题

先是来套向下

率先来拟向下

望下之言语,就是当时之蛇头位置,例如当前是30,0
这个职位的江湖还打一个蓝色单元格

朝下之言语,就是当脚下的蛇头位置,例如当前凡30,0
这个职位的下方还打一个蓝色单元格

即像这么的

就比如这么的

图片 7

图片 8

这会儿蛇头的坐标就当是(30,10),  其实就算是拿新加之蛇头的纵坐标加了10

这会儿蛇头的坐标就应是(30,10),  其实就是是用新加之蛇头的纵坐标加了10

然后擦掉尾部的10,0坐标

接下来擦掉尾部底10,0坐标

图片 9

图片 10

下一场径直重复是过程(也即是横坐标不转移,纵坐标一直加10),就模仿出蛇身向下移动了

然后直接还这历程(也尽管是反正坐标不换,纵坐标一直加10),就模仿出蛇身向下走了

 

 

下一场向左的语句,就是横坐标-10 而即便坐标不转换

下一场为左的言语,就是横坐标-10 而就坐标不转移

前进的讲话就是是,横坐标不变换,纵为标-10

迈入的语句虽是,横坐标不换,纵为标-10

 

 

主导的笔触就是这么,在蛇身运动的经过被未更换的充实蛇头擦除蛇尾

基本的笔触就是是这么,在蛇身运动的进程中未移的多蛇头擦除蛇尾

倘以改动方向的下即便转该对应之横纵坐标.

若当改方向的时节便变更该对应之横纵坐标.

 

 

————————————————————————————俏皮的分割线————————————————————–

————————————————————————————俏皮的分割线————————————————————–

 

 

就说下食物的排放

继之说生食物的下

食吧,采取随机投放

食品吧,采取随机投放

而是排放的区域是来珍惜的

但是投的区域是出尊重的

比如说项目的地形图是一个720*720之方,蛇身一节省是10*10底言语,那么食物的深浅为理应是10*10

诸如项目之地形图是一个720*720底正方,蛇身一节约是10*10之语句,那么食物的大大小小也应有是10*10

若是食品的投区域为非应过地图,不然蛇根本吃不至怎么为是吧

若食品的投区域也非应过地图,不然蛇根本吃不顶怎么抓是吧

自己同样恰巧开头思念的是创建以机器,然后轻易在0-720为内展开取值

自我平刚好开头思念的是创立以机器,然后轻易在0-720坐内展开取值

可,因为你是10*10之尺寸,如果任意出来了底横坐标是3,纵坐标是5的说话

唯独,因为您是10*10底大小,如果任意出来了之横坐标是3,纵坐标是5底口舌

这就是说这个食物就是见面出现不以蛇身的运动轨迹及

这就是说是食物就见面起不以蛇身的位移轨迹及

就此于食品的投资区域自是(
0-720/食物宽度)*食品宽度,这样出来的坐标总是会让他的宽窄整除

从而对食品的投资区域自是(
0-720/食物宽度)*食物宽度,这样出来的坐标总是能被他的增幅整除

譬如说如果任意出来的星星点点单坐标数字是3跟5

比如如果任意出来的少独坐标数字是3以及5

这就是说通过以上处理下的坐标就相应是30,50,蛇是肯定能全凭着到之

那么通过以上处理下的坐标就应当是30,50,蛇是迟早会一心凭着到之

 

 

————————————————–没用之分割线—————————————————————————

————————————————–没用之分割线—————————————————————————

 

 

好了,现在始发制作代码方面的事

吓了,现在开头做代码方面的从

新建一个窗体程序后,我是打算直接用一个图纸框当地图的,图片框的高低设置为720*720

新建一个窗体程序后,我是打算直接用一个图纸框当地图的,图片框的分寸设置为720*720

一经蛇身和食的尺寸都安为10*10

只要蛇身和食品的轻重都安装为10*10

界面搭建

界面搭建

图片 11

图片 12

 

 

= =| 额,不见面搭界面,就这么吧

= =| 额,不会见搭界面,就这样吧

接下来,直接贴全部之代码

然后,直接贴全部的代码

 ===================元素分割线================================

 ===================元素分割线================================

(之前发过代码,但是盖觉得还有不足,以及臃肿的远在,所以一直编辑来修去的修改,这次以变更了,所以决定拿前的代码完全删掉再还粘贴平不好)

(之前发过代码,但是以觉得还有不足,以及臃肿的处在,所以一直编辑来修去的改,这次又改了,所以决定用前的代码完全删掉再重复粘贴平不成)

(有不足之处,请看官们多原谅)

(有不足之处,请看官们何其原谅)

 1     /// <summary>
 2     /// 蛇身或食物的基本块(父类)
 3     /// </summary>
 4     public class Cell
 5     {
 6         /// <summary>
 7         /// 块的顶点X座标
 8         /// </summary>
 9         public int X { get; set; }
10 
11         /// <summary>
12         /// 块的顶点Y座标
13         /// </summary>
14         public int Y { get; set; }
15 
16         /// <summary>
17         /// 块的宽度
18         /// </summary>
19         public int Width { get; set; }
20 
21         /// <summary>
22         /// 块的高度
23         /// </summary>
24         public int Height { get; set; }
25 
26         /// <summary>
27         /// 所在地图宽度
28         /// </summary>
29         public int MapWidth { get; set; }
30 
31         /// <summary>
32         /// 所在地图高度
33         /// </summary>
34         public int MapHeight { get; set; }
35 
36         public Cell(int x,int y)
37         {
38             this.X = x;
39             this.Y = y;
40         }
41 
42         public Cell() { }
43 
44         /// <summary>
45         /// 绘制单元格
46         /// </summary>
47         /// <param name="g"></param>
48         /// <param name="c"></param>
49         public virtual void Draw(Graphics g, Color c)
50         {
51             SolidBrush sb = new SolidBrush(c);
52             g.FillRectangle(sb, this.X, this.Y, this.Width, this.Height);
53         }
54     }
 1     /// <summary>
 2     /// 蛇身或食物的基本块(父类)
 3     /// </summary>
 4     public class Cell
 5     {
 6         /// <summary>
 7         /// 块的顶点X座标
 8         /// </summary>
 9         public int X { get; set; }
10 
11         /// <summary>
12         /// 块的顶点Y座标
13         /// </summary>
14         public int Y { get; set; }
15 
16         /// <summary>
17         /// 块的宽度
18         /// </summary>
19         public int Width { get; set; }
20 
21         /// <summary>
22         /// 块的高度
23         /// </summary>
24         public int Height { get; set; }
25 
26         /// <summary>
27         /// 所在地图宽度
28         /// </summary>
29         public int MapWidth { get; set; }
30 
31         /// <summary>
32         /// 所在地图高度
33         /// </summary>
34         public int MapHeight { get; set; }
35 
36         public Cell(int x,int y)
37         {
38             this.X = x;
39             this.Y = y;
40         }
41 
42         public Cell() { }
43 
44         /// <summary>
45         /// 绘制单元格
46         /// </summary>
47         /// <param name="g"></param>
48         /// <param name="c"></param>
49         public virtual void Draw(Graphics g, Color c)
50         {
51             SolidBrush sb = new SolidBrush(c);
52             g.FillRectangle(sb, this.X, this.Y, this.Width, this.Height);
53         }
54     }

 

 

 1     /// <summary>
 2     /// 食物类
 3     /// </summary>
 4     public class Food:Cell
 5     {
 6         public Food(int x, int y) : base(x, y)
 7         {
 8 
 9         }
10 
11         public Food() { }
12 
13         /// <summary>
14         /// 创建食物
15         /// </summary>
16         /// <returns></returns>
17         public Food GetFood()
18         {
19 
20             Random r = new Random();
21 
22             ///-------------------------------------------------------
23             ///--因为本例中所使用的地图是正方形,每一个食物都是在地图的小正方形中
24             ///其生成的坐标点必须要能被整除,所以下面才这样获取随机数
25             int rX = (r.Next(0, this.MapWidth / Width)) * Width;
26             int rY = (r.Next(0, this.MapHeight / Width)) * Width;            
27 
28             //生成新的食物,并给各项赋值
29             Food Newfood = new Food(rX, rY);
30             Newfood.Width = this.Width;
31             Newfood.Height = this.Height;
32             return Newfood;
33         }           
34     }
 1     /// <summary>
 2     /// 食物类
 3     /// </summary>
 4     public class Food:Cell
 5     {
 6         public Food(int x, int y) : base(x, y)
 7         {
 8 
 9         }
10 
11         public Food() { }
12 
13         /// <summary>
14         /// 创建食物
15         /// </summary>
16         /// <returns></returns>
17         public Food GetFood()
18         {
19 
20             Random r = new Random();
21 
22             ///-------------------------------------------------------
23             ///--因为本例中所使用的地图是正方形,每一个食物都是在地图的小正方形中
24             ///其生成的坐标点必须要能被整除,所以下面才这样获取随机数
25             int rX = (r.Next(0, this.MapWidth / Width)) * Width;
26             int rY = (r.Next(0, this.MapHeight / Width)) * Width;            
27 
28             //生成新的食物,并给各项赋值
29             Food Newfood = new Food(rX, rY);
30             Newfood.Width = this.Width;
31             Newfood.Height = this.Height;
32             return Newfood;
33         }           
34     }

 

 

 

 

  1     /// <summary>
  2     /// 蛇类
  3     /// </summary>
  4     public class Snake: Cell
  5     {
  6         /// <summary>
  7         /// 创建食物对象
  8         /// </summary>
  9         Food food = new Food();
 10       
 11         /// <summary>
 12         /// 创建蛇身集合
 13         /// </summary>
 14         public List<Snake> Snakes = new List<Snake>();
 15 
 16         /// <summary>
 17         /// 创建头部坐标
 18         /// </summary>
 19         public Point Heade;
 20 
 21         public Snake() { }
 22 
 23         public Snake(int x, int y) : base(x, y)
 24         {
 25         }
 26 
 27         /// <summary>
 28         /// 位置枚举
 29         /// </summary>
 30         public enum Direction { left, right, up, down }
 31              
 32         /// <summary>
 33         /// 创建枚举对象
 34         /// </summary>
 35         public Direction de = new Direction();
 36 
 37         /// <summary>
 38         /// 画出蛇的形状
 39         /// </summary>
 40         /// <param name="g"></param>
 41         /// <param name="c"></param>
 42         public override void Draw(Graphics g, Color c)
 43         {
 44             //创建单色刷子
 45             SolidBrush sb = new SolidBrush(c);
 46 
 47             foreach (var item in Snakes)
 48             {
 49                 //将蛇身每一节画出来 
 50                 g.FillRectangle(sb, item.X, item.Y,this.Width,this.Height);
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// 清除蛇尾的那节
 56         /// </summary>
 57         /// <param name="g"></param>
 58         /// <param name="c"></param>
 59         public void Clear(Graphics g, Color c)
 60         {
 61             //将尾部一个给涂掉
 62             Snake s = Snakes[0];
 63             SolidBrush sb = new SolidBrush(c);
 64             g.FillRectangle(sb, s.X, s.Y, Width, Height);
 65         }
 66 
 67         /// <summary>
 68         /// 移动的方法
 69         /// </summary>
 70         /// <param name="g"></param>
 71         /// <param name="c"></param>
 72         /// <param name="ph">当前的朝向</param>
 73         public void Move(Graphics g,Color c,Direction ph)
 74         {
 75             //根据朝向更改对应的坐标
 76             switch (ph)
 77             {
 78                 case Direction.left:
 79                     Heade.X -= Width;
 80                     break;
 81                 case Direction.right:
 82                     Heade.X += Width;
 83                     break;
 84                 case Direction.up:
 85                     Heade.Y -= Height;
 86                     break;
 87                 case Direction.down:
 88                     Heade.Y += Height;
 89                     break;
 90             }
 91 
 92             //将新蛇头加入到集合中
 93             Snakes.Add(new Snake(Heade.X, Heade.Y));
 94         }
 95 
 96         /// <summary>
 97         /// 判断当前是否吃到食物
 98         /// </summary>
 99         /// <param name="food"></param>
100         /// <returns></returns>
101         public bool EatFood(Food food)
102         {
103             //拿到食物座标
104             Point foodPos = new Point(food.X, food.Y);
105 
106             //如果当前的蛇头座标与当前的食物座标重叠
107             if (Heade.X == foodPos.X && Heade.Y == foodPos.Y)
108             {
109                 //已经吃到食物了
110                 return true;
111             }
112             else
113             {
114                 return false;
115             }
116         }
117 
118         /// <summary>
119         /// 判断 是否蛇头撞到了自己
120         /// </summary>
121         /// <returns>返回true则为撞到</returns>
122         public bool HitBOdy()
123         {
124             bool falg = false;
125             
126             //判断蛇头是否在除蛇头之外的蛇身内
127             for (int i = 0; i < Snakes.Count-1; i++)
128             {
129                 //如果在
130                 if (Snakes[i].X == Heade.X && Snakes[i].Y == Heade.Y)//Snakes[i].X == snakeHead.X && Snakes[i].Y == snakeHead.Y)
131                 {
132                     //撞到自己
133                     falg = true;
134                     break;
135                 }
136                 else
137                 {
138                     falg = false;
139                 }
140             }
141             return falg;
142         }
143 
144         /// <summary>
145         /// 判断是否跑出了地图
146         /// </summary>
147         /// <returns></returns>
148         public bool HitMap()
149         {
150             ///判断当前蛇头位置是否在地图外面
151             if (Heade.X< 0 || Heade.X >= MapWidth || Heade.Y < 0 || Heade.Y >= MapHeight)
152             {
153                 return true;
154             }
155             else
156             {
157                 return false;
158             }
159         }
160     }
  1     /// <summary>
  2     /// 蛇类
  3     /// </summary>
  4     public class Snake: Cell
  5     {
  6         /// <summary>
  7         /// 创建食物对象
  8         /// </summary>
  9         Food food = new Food();
 10       
 11         /// <summary>
 12         /// 创建蛇身集合
 13         /// </summary>
 14         public List<Snake> Snakes = new List<Snake>();
 15 
 16         /// <summary>
 17         /// 创建头部坐标
 18         /// </summary>
 19         public Point Heade;
 20 
 21         public Snake() { }
 22 
 23         public Snake(int x, int y) : base(x, y)
 24         {
 25         }
 26 
 27         /// <summary>
 28         /// 位置枚举
 29         /// </summary>
 30         public enum Direction { left, right, up, down }
 31              
 32         /// <summary>
 33         /// 创建枚举对象
 34         /// </summary>
 35         public Direction de = new Direction();
 36 
 37         /// <summary>
 38         /// 画出蛇的形状
 39         /// </summary>
 40         /// <param name="g"></param>
 41         /// <param name="c"></param>
 42         public override void Draw(Graphics g, Color c)
 43         {
 44             //创建单色刷子
 45             SolidBrush sb = new SolidBrush(c);
 46 
 47             foreach (var item in Snakes)
 48             {
 49                 //将蛇身每一节画出来 
 50                 g.FillRectangle(sb, item.X, item.Y,this.Width,this.Height);
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// 清除蛇尾的那节
 56         /// </summary>
 57         /// <param name="g"></param>
 58         /// <param name="c"></param>
 59         public void Clear(Graphics g, Color c)
 60         {
 61             //将尾部一个给涂掉
 62             Snake s = Snakes[0];
 63             SolidBrush sb = new SolidBrush(c);
 64             g.FillRectangle(sb, s.X, s.Y, Width, Height);
 65         }
 66 
 67         /// <summary>
 68         /// 移动的方法
 69         /// </summary>
 70         /// <param name="g"></param>
 71         /// <param name="c"></param>
 72         /// <param name="ph">当前的朝向</param>
 73         public void Move(Graphics g,Color c,Direction ph)
 74         {
 75             //根据朝向更改对应的坐标
 76             switch (ph)
 77             {
 78                 case Direction.left:
 79                     Heade.X -= Width;
 80                     break;
 81                 case Direction.right:
 82                     Heade.X += Width;
 83                     break;
 84                 case Direction.up:
 85                     Heade.Y -= Height;
 86                     break;
 87                 case Direction.down:
 88                     Heade.Y += Height;
 89                     break;
 90             }
 91 
 92             //将新蛇头加入到集合中
 93             Snakes.Add(new Snake(Heade.X, Heade.Y));
 94         }
 95 
 96         /// <summary>
 97         /// 判断当前是否吃到食物
 98         /// </summary>
 99         /// <param name="food"></param>
100         /// <returns></returns>
101         public bool EatFood(Food food)
102         {
103             //拿到食物座标
104             Point foodPos = new Point(food.X, food.Y);
105 
106             //如果当前的蛇头座标与当前的食物座标重叠
107             if (Heade.X == foodPos.X && Heade.Y == foodPos.Y)
108             {
109                 //已经吃到食物了
110                 return true;
111             }
112             else
113             {
114                 return false;
115             }
116         }
117 
118         /// <summary>
119         /// 判断 是否蛇头撞到了自己
120         /// </summary>
121         /// <returns>返回true则为撞到</returns>
122         public bool HitBOdy()
123         {
124             bool falg = false;
125             
126             //判断蛇头是否在除蛇头之外的蛇身内
127             for (int i = 0; i < Snakes.Count-1; i++)
128             {
129                 //如果在
130                 if (Snakes[i].X == Heade.X && Snakes[i].Y == Heade.Y)//Snakes[i].X == snakeHead.X && Snakes[i].Y == snakeHead.Y)
131                 {
132                     //撞到自己
133                     falg = true;
134                     break;
135                 }
136                 else
137                 {
138                     falg = false;
139                 }
140             }
141             return falg;
142         }
143 
144         /// <summary>
145         /// 判断是否跑出了地图
146         /// </summary>
147         /// <returns></returns>
148         public bool HitMap()
149         {
150             ///判断当前蛇头位置是否在地图外面
151             if (Heade.X< 0 || Heade.X >= MapWidth || Heade.Y < 0 || Heade.Y >= MapHeight)
152             {
153                 return true;
154             }
155             else
156             {
157                 return false;
158             }
159         }
160     }

 

 

 

 

    public partial class 贪吃蛇 : Form
    {

        public 贪吃蛇()
        {
            InitializeComponent();

        }       

        /// <summary>
        /// 初始化蛇
        /// </summary>
        Snake snake = new Snake();

        /// <summary>
        /// 创建食物
        /// </summary>
        Food food = null;

        /// <summary>
        /// 创建对象,用作地图(也就是大图片框)
        /// </summary>
        Graphics gp;

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 贪吃蛇_Load(object sender, EventArgs e)
        {
            ///创建画板
            gp = pictureBox1.CreateGraphics();

            //初始化蛇身朝向(默认为向右)
            snake.de = Snake.Direction.right;

            timer1.Interval = hScrollBar1.Value;
            lbSpeed.Text = hScrollBar1.Value.ToString();
            lbSnakeCount.Text = snake.Snakes.Count.ToString();

            //--初始化蛇的各项数值
            snake.Width = 10;
            snake.Height = 10;
            snake.MapHeight = pictureBox1.Height;
            snake.MapWidth = pictureBox1.Width;

            //--增加蛇身
            snake.Snakes.Add(new Snake(0, 0));
            snake.Snakes.Add(new Snake(10, 0));
            snake.Snakes.Add(new Snake(20, 0));
            snake.Snakes.Add(new Snake(30, 0));

            //--初始化食物的各项数值
            food = new SnakeGame.Food(100, 100);
            food.Width = 10;
            food.Height = 10;
            food.MapWidth = pictureBox1.Width;
            food.MapHeight = pictureBox1.Height;            
        }

        private void 贪吃蛇_Paint(object sender, PaintEventArgs e)
        {
            //创建一张位图,大小就为图片框的大小
            Bitmap bitmap = new Bitmap(this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);
            Graphics g = Graphics.FromImage(bitmap);

            //画出初始蛇身
            snake.Draw(g, Color.White);

            //画出初始食物
            food.Draw(g, Color.Red);

            this.pictureBox1.Image = bitmap;
        }

        /// <summary>
        ///  开始按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            //点击开始按钮后,启动计时器
            timer1.Enabled = true;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            ///  逻辑为:
            ///  模拟蛇身在运动
            ///  即先让蛇尾那一格涂成和背景色一样,然后判断方向,
            ///  在相应的方向增加蛇头位置,之后再将新蛇头画出来

            //每次刷新的时候都取一次蛇头位置(给蛇头赋值)
            snake.Heade = new Point(snake.Snakes.Last().X, snake.Snakes.Last().Y);

            //清除蛇尾
            snake.Clear(gp, pictureBox1.BackColor);

            //判断是否吃到食物,吃到了就不再移除蛇尾,当做是吃了食物增长了一节
            if (snake.EatFood(food))
            {
                //已经吃了就再获取一次食物
                food = food.GetFood();

                //将重新获取到食物对象赋值
                food.MapHeight = pictureBox1.Height;
                food.MapWidth = pictureBox1.Width;

                //画出重新获取到的食物
                food.Draw(gp, Color.Red);

                //每增长5节数减10速度
                timer1.Interval = Level.levelUp(snake.Snakes.Count, 5, timer1.Interval, 10);

                lbSpeed.Text = timer1.Interval.ToString();
            }
            else
            {
                //移除蛇尾
                snake.Snakes.RemoveAt(0);
            }
//只用直接调用蛇类的行走方法,传入当前的朝向就可以了
            snake.Move(gp, Color.Red, snake.de);

            //画出整个蛇身
            snake.Draw(gp, Color.White);

            //判断边界
            if (snake.HitBOdy())
            {
                timer1.Enabled = false;
                MessageBox.Show("撞到自己了,游戏结束");

                //将开始按钮禁用
                btnStart.Enabled = false;
                return;
            }
            if (snake.HitMap())
            {
                timer1.Enabled = false;
                MessageBox.Show("撞到墙了,游戏结束");

                //将开始按钮禁用
                btnStart.Enabled = false;
                return;
            }

            lbSnakeCount.Text = snake.Snakes.Count.ToString();
        }

        /// <summary>
        /// 开始按钮点击后,判断WSDA
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_KeyDown(object sender, KeyEventArgs e)
        {
            //根据当前的按键将朝向对应起来
            switch (e.KeyCode)
            {
                case Keys.W:
                    snake.de = Snake.Direction.up;    
                    break;
                case Keys.S:
                    snake.de = Snake.Direction.down;   
                    break;
                case Keys.D:
                    snake.de = Snake.Direction.right;  
                    break;
                case Keys.A:
                    snake.de = Snake.Direction.left;  
                    break;
            }
        }

        /// <summary>
        /// 暂停按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            //将计时器不可用
            timer1.Enabled = false;
        }

        /// <summary>
        /// 拉动速度条时改变蛇的速度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            this.timer1.Interval = hScrollBar1.Value;
            lbSpeed.Text = hScrollBar1.Value.ToString();
            btnStart.Focus();
        }

        /// <summary>
        /// 重新开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAgain_Click(object sender, EventArgs e)
        {
            //重新开始,初始化各项数值
            snake.Snakes.Clear();

            snake.de = Snake.Direction.right;  

            this.贪吃蛇_Paint(sender, null);
            this.贪吃蛇_Load(sender, null);
            btnStart.Focus();
            btnStart.Enabled = true;
        }

        /// <summary>
        /// 画布点击,只是为了增加改变地图的背景色而已,没多大用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.pictureBox1.BackColor = colorDialog1.Color;
            }

        }

        /// <summary>
        /// 窗体点击,只是为了增加改变窗体的背景色而已,没多大用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 贪吃蛇_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.BackColor = colorDialog1.Color;
            }
        }

        /// <summary>
        /// 背景改变,只是为了增加改变美女图片框的背景色而已,让其和窗体背景色保持一致,没多大用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 贪吃蛇_BackColorChanged(object sender, EventArgs e)
        {
            pictureBox2.BackColor = this.BackColor;
        }       
    }
    public partial class 贪吃蛇 : Form
    {

        public 贪吃蛇()
        {
            InitializeComponent();

        }       

        /// <summary>
        /// 初始化蛇
        /// </summary>
        Snake snake = new Snake();

        /// <summary>
        /// 创建食物
        /// </summary>
        Food food = null;

        /// <summary>
        /// 创建对象,用作地图(也就是大图片框)
        /// </summary>
        Graphics gp;

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 贪吃蛇_Load(object sender, EventArgs e)
        {
            ///创建画板
            gp = pictureBox1.CreateGraphics();

            //初始化蛇身朝向(默认为向右)
            snake.de = Snake.Direction.right;

            timer1.Interval = hScrollBar1.Value;
            lbSpeed.Text = hScrollBar1.Value.ToString();
            lbSnakeCount.Text = snake.Snakes.Count.ToString();

            //--初始化蛇的各项数值
            snake.Width = 10;
            snake.Height = 10;
            snake.MapHeight = pictureBox1.Height;
            snake.MapWidth = pictureBox1.Width;

            //--增加蛇身
            snake.Snakes.Add(new Snake(0, 0));
            snake.Snakes.Add(new Snake(10, 0));
            snake.Snakes.Add(new Snake(20, 0));
            snake.Snakes.Add(new Snake(30, 0));

            //--初始化食物的各项数值
            food = new SnakeGame.Food(100, 100);
            food.Width = 10;
            food.Height = 10;
            food.MapWidth = pictureBox1.Width;
            food.MapHeight = pictureBox1.Height;            
        }

        private void 贪吃蛇_Paint(object sender, PaintEventArgs e)
        {
            //创建一张位图,大小就为图片框的大小
            Bitmap bitmap = new Bitmap(this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);
            Graphics g = Graphics.FromImage(bitmap);

            //画出初始蛇身
            snake.Draw(g, Color.White);

            //画出初始食物
            food.Draw(g, Color.Red);

            this.pictureBox1.Image = bitmap;
        }

        /// <summary>
        ///  开始按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            //点击开始按钮后,启动计时器
            timer1.Enabled = true;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            ///  逻辑为:
            ///  模拟蛇身在运动
            ///  即先让蛇尾那一格涂成和背景色一样,然后判断方向,
            ///  在相应的方向增加蛇头位置,之后再将新蛇头画出来

            //每次刷新的时候都取一次蛇头位置(给蛇头赋值)
            snake.Heade = new Point(snake.Snakes.Last().X, snake.Snakes.Last().Y);

            //清除蛇尾
            snake.Clear(gp, pictureBox1.BackColor);

            //判断是否吃到食物,吃到了就不再移除蛇尾,当做是吃了食物增长了一节
            if (snake.EatFood(food))
            {
                //已经吃了就再获取一次食物
                food = food.GetFood();

                //将重新获取到食物对象赋值
                food.MapHeight = pictureBox1.Height;
                food.MapWidth = pictureBox1.Width;

                //画出重新获取到的食物
                food.Draw(gp, Color.Red);

                //每增长5节数减10速度
                timer1.Interval = Level.levelUp(snake.Snakes.Count, 5, timer1.Interval, 10);

                lbSpeed.Text = timer1.Interval.ToString();
            }
            else
            {
                //移除蛇尾
                snake.Snakes.RemoveAt(0);
            }
//只用直接调用蛇类的行走方法,传入当前的朝向就可以了
            snake.Move(gp, Color.Red, snake.de);

            //画出整个蛇身
            snake.Draw(gp, Color.White);

            //判断边界
            if (snake.HitBOdy())
            {
                timer1.Enabled = false;
                MessageBox.Show("撞到自己了,游戏结束");

                //将开始按钮禁用
                btnStart.Enabled = false;
                return;
            }
            if (snake.HitMap())
            {
                timer1.Enabled = false;
                MessageBox.Show("撞到墙了,游戏结束");

                //将开始按钮禁用
                btnStart.Enabled = false;
                return;
            }

            lbSnakeCount.Text = snake.Snakes.Count.ToString();
        }

        /// <summary>
        /// 开始按钮点击后,判断WSDA
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_KeyDown(object sender, KeyEventArgs e)
        {
            //根据当前的按键将朝向对应起来
            switch (e.KeyCode)
            {
                case Keys.W:
                    snake.de = Snake.Direction.up;    
                    break;
                case Keys.S:
                    snake.de = Snake.Direction.down;   
                    break;
                case Keys.D:
                    snake.de = Snake.Direction.right;  
                    break;
                case Keys.A:
                    snake.de = Snake.Direction.left;  
                    break;
            }
        }

        /// <summary>
        /// 暂停按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            //将计时器不可用
            timer1.Enabled = false;
        }

        /// <summary>
        /// 拉动速度条时改变蛇的速度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            this.timer1.Interval = hScrollBar1.Value;
            lbSpeed.Text = hScrollBar1.Value.ToString();
            btnStart.Focus();
        }

        /// <summary>
        /// 重新开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAgain_Click(object sender, EventArgs e)
        {
            //重新开始,初始化各项数值
            snake.Snakes.Clear();

            snake.de = Snake.Direction.right;  

            this.贪吃蛇_Paint(sender, null);
            this.贪吃蛇_Load(sender, null);
            btnStart.Focus();
            btnStart.Enabled = true;
        }

        /// <summary>
        /// 画布点击,只是为了增加改变地图的背景色而已,没多大用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.pictureBox1.BackColor = colorDialog1.Color;
            }

        }

        /// <summary>
        /// 窗体点击,只是为了增加改变窗体的背景色而已,没多大用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 贪吃蛇_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.BackColor = colorDialog1.Color;
            }
        }

        /// <summary>
        /// 背景改变,只是为了增加改变美女图片框的背景色而已,让其和窗体背景色保持一致,没多大用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 贪吃蛇_BackColorChanged(object sender, EventArgs e)
        {
            pictureBox2.BackColor = this.BackColor;
        }       
    }

 

 

相关文章