首页 > 代码库 > 贪吃蛇小游戏的初步尝试制作

贪吃蛇小游戏的初步尝试制作

这里利用二维数组做为地图,利用集合来存放蛇头和蛇尾的位置坐标,初步实现一个需要键盘控制的贪吃蛇小游戏。

首先,Main函数下面需要一个大循环来让游戏失败的时候能够重新开始,这里我们用了定义了一个bool型的sc,判断sc的true和flase来确定游戏是否开始;

static void Main(string[] args)
        {

          bool sc = true;

while (sc) //大循环,用于游戏失败重新开始
            {

下面是定义一个集合snake_x来存放蛇的x坐标,定义一个集合snake_y来存放蛇的y坐标,;然后对x,y进行初始化;定义一个蛇身的长度len=1,后续用来判断长度达到设定值之后游戏成功通过,这里我们设定的值为30;

                //贪吃蛇
                ArrayList snake_x = new ArrayList();//定义x坐标的集合
                ArrayList snake_y = new ArrayList();//定义y坐标的集合

                snake_x.Add(1);//x坐标初始化
                snake_y.Add(1);//y坐标初始化
                int x, y, a, b;
                int i; int j;
                int len = 1; //定义蛇的长度为1,用来判断游戏是否成功

之后开始定义一个10*10的二维数组,作为地图,数字3代表墙壁,数字2代表蛇身,数字1代表食物,数字0代表路。
                //定义地图
                int[,] map = new int[10, 10] {
            {3,3,3,3,3,3,3,3,3,3},
            {3,2,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,0,0,0,0,0,0,0,0,3},
            {3,3,3,3,3,3,3,3,3,3}
            };

地图定义完之后,进行提示,玩家需要按空格键开始,这里用了一个while循环来判断输入的按键是不是空格键,是的话break出循环,开始下面的程序(即开始游戏)。

 Console.WriteLine("按空格键开始");
               
                while (true) //循环,用于按空格键开始
                {
                   

                    ConsoleKeyInfo kf = Console.ReadKey(true);
                    string k = kf.Key.ToString().ToLower();
                    if (k == "spacebar")
                    {
                        break;
                    }
                }

利用随机数生成器,生成一组范围在0-9之间的x,y;判断x,y坐标的数值是不是0,如果是0代表这个坐标可以生成食物,否则继续随机生成。

 //随机出食物
                while (true)
                {
                    Random c = new Random();
                    Random d = new Random();
                    int e = c.Next(9);
                    int f = d.Next(9);
                    if (map[e, f] == 0)
                    {
                        map[e, f] = 1;
                        break;
                    }

                }

食物生成之后,可以进行游戏开始的第一次显示,这时屏幕上会有1个食物,蛇身子也为1。五星代表食物,如下图:

//显示
                Console.Clear();

                for (i = 0; i < 10; i++)
                {
                    for (j = 0; j < 10; j++)
                    {
                        if (map[i, j] == 0)
                        {
                            Console.Write("  ");
                        }
                        else if (map[i, j] == 1)
                        {
                            Console.Write("☆");
                        }
                        else if (map[i, j] == 2)
                        {
                            Console.Write("◎");
                        }
                        else if (map[i, j] == 3)
                        {
                            Console.Write("■");
                        }

                    }
                    Console.Write("\n");
                }

 

地图显示完成,下面开始读取按键,需要一直循环着读取,所以用了个while循环。

while (true) //读取按键循环
                {

                    ConsoleKeyInfo kf = Console.ReadKey(true);
                    string k = kf.Key.ToString().ToLower();

下面开始判断按下的键是哪个,以下为按“下”键时的操作程序,从xy集合中取出坐标,判断蛇的下面是什么,如果是食物,食物变成蛇头,蛇尾不变,集合中压入蛇头的坐标,蛇身的长度+1,并且随机生成一个新的食物;如果是路,蛇下一格变为蛇头,蛇尾变为路,相当于蛇向下移动了一格;如果是其他的,跳出循环,并且游戏结束。

if (k == "downarrow")
                    {
                        x = (int)snake_x[0];
                        y = (int)snake_y[0];


                        if (map[x + 1, y] == 1)
                        {
                            map[x + 1, y] = 2;

                            snake_x.Insert(0, x + 1);
                            snake_y.Insert(0, y);
                            len++;

                            //吃掉之后随机出食物
                            while (true)
                            {
                                Random c = new Random();
                                Random d = new Random();
                                int e = c.Next(9);  //限制范围
                                int f = d.Next(9);
                                if (map[e, f] == 0)
                                {
                                    map[e, f] = 1;
                                    break;
                                }

                            }
                        }
                        else if (map[x + 1, y] == 0)
                        {

                            a = (int)snake_x[snake_x.Count - 1];
                            b = (int)snake_y[snake_y.Count - 1];

                            map[a, b] = 0;
                            map[x + 1, y] = 2;

                            snake_x.Insert(0, x + 1);  //将x+1放进0位置
                            snake_x.RemoveAt(snake_x.Count - 1);//将末端位置的数移除
                            snake_y.Insert(0, y);
                            snake_y.RemoveAt(snake_y.Count - 1);

                        }
                        else
                        {
                            Console.Write("\a");

                            break;
                        }
                    }

下面分别为向上,向右,向左时的程序,道理同向下一样,这里不说了。

 else if (k == "uparrow")
                    {
                        x = (int)snake_x[0];
                        y = (int)snake_y[0];


                        if (map[x - 1, y] == 1)
                        {
                            map[x - 1, y] = 2;

                            snake_x.Insert(0, x - 1);
                            snake_y.Insert(0, y);
                            len++;

                            //吃掉之后随机出食物
                            while (true)
                            {
                                Random c = new Random();
                                Random d = new Random();
                                int e = c.Next(9);
                                int f = d.Next(9);
                                if (map[e, f] == 0)
                                {
                                    map[e, f] = 1;
                                    break;
                                }

                            }
                        }
                        else if (map[x - 1, y] == 0)
                        {

                            a = (int)snake_x[snake_x.Count - 1];
                            b = (int)snake_y[snake_y.Count - 1];

                            map[a, b] = 0;
                            map[x - 1, y] = 2;

                            snake_x.Insert(0, x - 1);
                            snake_x.RemoveAt(snake_x.Count - 1);
                            snake_y.Insert(0, y);
                            snake_y.RemoveAt(snake_y.Count - 1);

                        }
                        else
                        {
                            Console.Write("\a");

                            break;
                        }
                    }
                    else if (k == "rightarrow")
                    {
                        x = (int)snake_x[0];
                        y = (int)snake_y[0];


                        if (map[x, y + 1] == 1)
                        {
                            map[x, y + 1] = 2;

                            snake_x.Insert(0, x);
                            snake_y.Insert(0, y + 1);
                            len++;

                            //吃掉之后随机出食物
                            while (true)
                            {
                                Random c = new Random();
                                Random d = new Random();
                                int e = c.Next(9);
                                int f = d.Next(9);
                                if (map[e, f] == 0)
                                {
                                    map[e, f] = 1;
                                    break;
                                }

                            }
                        }
                        else if (map[x, y + 1] == 0)
                        {
                            a = (int)snake_x[snake_x.Count - 1];
                            b = (int)snake_y[snake_y.Count - 1];

                            map[a, b] = 0;
                            map[x, y + 1] = 2;

                            snake_x.Insert(0, x);
                            snake_x.RemoveAt(snake_x.Count - 1);
                            snake_y.Insert(0, y + 1);
                            snake_y.RemoveAt(snake_y.Count - 1);

                        }
                        else
                        {
                            Console.Write("\a");

                            break;
                        }
                    }
                    else if (k == "leftarrow")
                    {
                        x = (int)snake_x[0];
                        y = (int)snake_y[0];


                        if (map[x, y - 1] == 1)
                        {
                            map[x, y - 1] = 2;

                            snake_x.Insert(0, x);
                            snake_y.Insert(0, y - 1);
                            len++;

                            //吃掉之后随机出食物
                            while (true)
                            {
                                Random c = new Random();
                                Random d = new Random();
                                int e = c.Next(9);
                                int f = d.Next(9);
                                if (map[e, f] == 0)
                                {
                                    map[e, f] = 1;
                                    break;
                                }

                            }
                        }
                        else if (map[x, y - 1] == 0)
                        {

                            a = (int)snake_x[snake_x.Count - 1];
                            b = (int)snake_y[snake_y.Count - 1];

                            map[a, b] = 0;
                            map[x, y - 1] = 2;

                            snake_x.Insert(0, x);
                            snake_x.RemoveAt(snake_x.Count - 1);
                            snake_y.Insert(0, y - 1);
                            snake_y.RemoveAt(snake_y.Count - 1);


                        }
                        else
                        {
                            Console.Write("\a");

                            break;
                        }
                    }

读取完按键,并且操作完地图之后,显示一遍地图。

 //显示

                    Console.Clear();

                    for (i = 0; i < 10; i++)
                    {
                        for (j = 0; j < 10; j++)
                        {
                            if (map[i, j] == 0)
                            {
                                Console.Write("  ");
                            }
                            else if (map[i, j] == 1)
                            {
                                Console.Write("☆");
                            }
                            else if (map[i, j] == 2)
                            {
                                Console.Write("◎");
                            }
                            else if (map[i, j] == 3)
                            {
                                Console.Write("■");
                            }

                        }
                        Console.Write("\n");
                    }

这里用一个if语句判断蛇的长度是否达到了设定的值,是的话游戏成功通过,跳出循环结束游戏。
                    //如果蛇的长度为30,结束游戏
                    if (len == 30)
                    {
                        break;
                    }

                }


   用一个while循环显示结束后的提示,判断一下如果是游戏成功之后结束,则提示"你成功了!按ESC退出",按下ESC之后将sc的值变为flase,,跳出本循环,并且大循环结束,退出游戏;   如果判断到不是游戏成功时结束的,则说明是游戏失败,这里提示“ 游戏失败!请按R键重新开始 ”,按下R键跳出本循环,重新开始大循环,即重新开始游戏。              

  //游戏结束之后提醒

             while (true)   
                    {
                        //如果成功,按ESC退出
                        if (len == 30)
                        {
                            Console.WriteLine("你成功了!按ESC退出");
                            ConsoleKeyInfo kf = Console.ReadKey(true);
                            string k = kf.Key.ToString().ToLower();
                            if (k == "escape")
                            {
                                sc = flase;
                                break;
                            }
                        }


                        else
                        {
                            //如果失败,按R重新开始
                            Console.Clear();
                            Console.WriteLine("游戏失败!请按R键重新开始");
                            ConsoleKeyInfo kf = Console.ReadKey(true);
                            string k = kf.Key.ToString().ToLower();
                            if (k == "r")
                            {
                                break;
                            }
                        }

                    }
               
            }
        }

        public static bool flase { get; set; }
    }
}

到这里一个简单的需要键盘控制的贪吃蛇游戏就制作完成了。

下面是游戏文件和程序文件网盘地址,可以下载试玩,这个游戏也是适用与WIN7及以上版本或者安装了.net framework3.5及以上的电脑:

http://pan.baidu.com/s/1jG0ykJW

 

贪吃蛇小游戏的初步尝试制作