利用二维数组编写简单贪吃蛇小游戏,由于是初学C#,用的是单线程,所以蛇不会自动前进
代码及简要分析如下:
1 //定义地图,0为空,1为墙,2为蛇,3为食物 2 int[,] map = new int[15, 15]{ 3 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 4 {1,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, 5 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 6 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 7 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 8 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 9 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 10 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 11 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 12 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 13 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 14 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 15 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 16 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 17 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; 18 19 20 //确认蛇头位置,创建一个集合放蛇经过的位置 21 int x = 1, y = 1; 22 ArrayList snake = new ArrayList(); 23 snake.Add(x); 24 snake.Add(y); 25 26 for (; ; ) 27 {//for1 28 //判断蛇身长,长度为10,过关 29 if (snake.Count == 20) 30 { 31 Console.Clear(); 32 Console.WriteLine("过关!"); 33 break; 34 } 35 36 //随机生成食物 37 bool z = true; 38 for (; z; ) 39 { 40 Random food = new Random(); 41 int p = food.Next(15); //p,q为生成的食物的行列坐标 42 int q = food.Next(15); 43 for (int m = 0; m < 15; m++) 44 { 45 for (int n = 0; n < 15; n++) 46 { 47 if (map[p, q] == 0) //在空位生成食物 48 { 49 map[p, q] = 3; 50 z = false; 51 break; 52 } 53 } 54 } 55 } 56 57 //显示及操作部分 58 for (; ; ) 59 {//for2 60 //System.Threading.Thread.Sleep(1000); 61 //显示 62 Console.Clear(); 63 for (int m = 0; m < 15; m++) 64 { 65 for (int n = 0; n < 15; n++) 66 { 67 if (map[m, n] == 0) 68 { 69 Console.Write(" "); 70 } 71 if (map[m, n] == 1) 72 { 73 Console.Write("■"); 74 } 75 if (map[m, n] == 2) 76 { 77 Console.Write("◆"); 78 } 79 if (map[m, n] == 3) 80 { 81 Console.Write("¤"); 82 } 83 //Console.Write(map[m,n]); 84 } 85 Console.Write("\n"); 86 } 87 88 //判断是否吃掉食物 89 int food = 0; 90 for (int m = 0; m < 15; m++) 91 { 92 for (int n = 0; n < 15; n++) 93 { 94 if (map[m, n] == 3) //用来判断食物个数 95 { 96 food++; 97 break; 98 } 99 } 100 } 101 if (food == 0) //如果食物个数为0,跳出for2循环 102 { 103 break; 104 } 105 106 //操作部分 107 ConsoleKeyInfo input = Console.ReadKey(); //读取按键信息 108 string move = input.Key.ToString().ToLower(); 109 int a = (int)snake[0], b = (int)snake[1]; //从集合snake中读取蛇尾行列坐标 110 x = (int)snake[snake.Count - 2]; //x,y为蛇头行列坐标 111 y = (int)snake[snake.Count - 1]; 112 113 if (move == "uparrow") //判断是否是向上按键 114 { 115 if (map[x - 1, y] == 0) //上方为空位 116 { 117 map[x - 1, y] = 2; //上方位置变为蛇头 118 map[a, b] = 0; //蛇经过,蛇尾位置重新变成空位 119 snake.Add(x - 1); //以下两行为:把蛇头经过位置的坐标放入集合 120 snake.Add(y); 121 snake.RemoveAt(0); //以下两行为:把蛇尾经过位置坐标抛出集合 122 snake.RemoveAt(0); 123 } 124 else if (map[x - 1, y] == 3) //上方为食物 125 { 126 map[x - 1, y] = 2; 127 snake.Add(x - 1); 128 snake.Add(y); 129 } 130 else 131 Console.Write("\a"); 132 } 133 else if (move == "downarrow") 134 { 135 if (map[x + 1, y] == 0) 136 { 137 map[x + 1, y] = 2; 138 map[a, b] = 0; 139 snake.Add(x + 1); 140 snake.Add(y); 141 snake.RemoveAt(0); 142 snake.RemoveAt(0); 143 } 144 else if (map[x + 1, y] == 3) 145 { 146 map[x + 1, y] = 2; 147 snake.Add(x + 1); 148 snake.Add(y); 149 } 150 else 151 Console.Write("\a"); 152 } 153 else if (move == "leftarrow") 154 { 155 if (map[x, y - 1] == 0) 156 { 157 map[x, y - 1] = 2; 158 map[a, b] = 0; 159 snake.Add(x); 160 snake.Add(y - 1); 161 snake.RemoveAt(0); 162 snake.RemoveAt(0); 163 } 164 else if (map[x, y - 1] == 3) 165 { 166 map[x, y - 1] = 2; 167 snake.Add(x); 168 snake.Add(y - 1); 169 } 170 else 171 Console.Write("\a"); 172 } 173 else if (move == "rightarrow") 174 { 175 if (map[x, y + 1] == 0) 176 { 177 map[x, y + 1] = 2; 178 map[a, b] = 0; 179 snake.Add(x); 180 snake.Add(y + 1); 181 snake.RemoveAt(0); 182 snake.RemoveAt(0); 183 } 184 else if (map[x, y + 1] == 3) 185 { 186 map[x, y + 1] = 2; 187 snake.Add(x); 188 snake.Add(y + 1); 189 } 190 else 191 Console.Write("\a"); 192 } 193 else 194 Console.Write("\a"); //如果按错按键,发出提示音 195 }//for2 196 }//for1
游戏截图