不多说, 上代码, 拿去玩吧
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <windows.h> 4 #include <conio.h> 5 6 #define High 30 7 #define Width 30 8 9 int direction; 10 int length_snake; 11 int x_food, y_food; 12 int x_snake, y_snake; 13 int /*score,*/ speed, speedTop; 14 int canvas[High][Width] = {0}; 15 16 void startup(); 17 void show(); 18 void updateWithoutInput(); 19 void updateWithInput(); 20 void moveSnake(); 21 void gotoxy(int x, int y) 22 { 23 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); 24 COORD pos; 25 pos.X = x; 26 pos.Y = y; 27 SetConsoleCursorPosition(handle, pos); 28 } 29 void HideCursor() 30 { 31 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 32 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); 33 } 34 35 int main() 36 { 37 startup(); 38 while(1) 39 { 40 show(); 41 updateWithoutInput(); 42 updateWithInput(); 43 } 44 } 45 // 0Space, 1Snake_Head, 2 - (+∞)Snake_Body, -1Boundary, -2food 46 void startup() 47 { 48 int i, j; 49 direction = 4; //向右移动 50 length_snake = 4; 51 x_snake = High/2, y_snake = Width/2; 52 /*score = 0, */speed = 0, speedTop = 15; 53 x_food = rand()%(High - 4) + 2, y_food = rand()%(Width - 4) + 2; 54 for(i = 0; i <= High; i++) 55 { 56 for(j = 0; j <= Width; j++) 57 { 58 if(i == 0 || i== High || j == 0 || j== Width) 59 canvas[i][j] = -1; 60 else if(i == x_food && j == y_food) 61 canvas[i][j] = -2; 62 else if(i == x_snake && j == y_snake) 63 canvas[i][j] = 1; 64 } 65 } 66 //为身体赋值 67 for(i = 1; i < length_snake; i++) 68 canvas[x_snake][y_snake-i] = i + 1; 69 } 70 // 0Space, 1Snake_Head, 2 - (+∞)Snake_Body, -1Boundary, -2food 71 void show() 72 { 73 int i, j; 74 gotoxy(0, 0); 75 HideCursor(); 76 for(i = 0; i <= High; i++) 77 { 78 for(j = 0; j <= Width; j++) 79 { 80 if(canvas[i][j] == 0) 81 printf(" "); 82 else if(canvas[i][j] == 1) 83 printf("@"); 84 else if(canvas[i][j] == -1) 85 printf("#"); 86 else if(canvas[i][j] == -2) 87 printf("F"); 88 else if(canvas[i][j] > 1) 89 printf("*"); 90 } 91 printf("\n"); 92 } 93 } 94 95 void updateWithoutInput() 96 { 97 moveSnake(); 98 Sleep(200); 99 } 100 // 上1 下3 左2 右4 101 void updateWithInput() 102 { 103 char input; 104 if(kbhit()) //保证游戏实时更新 105 { 106 input = getch(); 107 if(input == 'a' && direction != 2 && direction != 4) 108 { 109 direction = 2; 110 moveSnake(); 111 } 112 if(input == 'w' && direction != 1 && direction != 3) 113 { 114 direction = 1; 115 moveSnake(); 116 } 117 if(input == 'd' && direction != 2 && direction != 4) 118 { 119 direction = 4; 120 moveSnake(); 121 } 122 if(input == 's' && direction != 1 && direction != 3) 123 { 124 direction = 3; 125 moveSnake(); 126 } 127 } 128 } 129 130 void moveSnake() 131 { 132 int oldhead_x, oldhead_y, oldtail_x, oldtail_y; 133 int max = 0; 134 int i, j; 135 //蛇的身体和头都先加1 136 for(i = 0; i < High; i++) 137 for(j = 0; j < Width; j++) 138 if(canvas[i][j] > 0) 139 canvas[i][j]++; 140 //开始寻找蛇的头和尾巴 141 for(i = 0; i < High; i++) 142 for(j = 0; j < Width; j++) 143 { 144 if(canvas[i][j] == 2) 145 { 146 oldhead_x = i; 147 oldhead_y = j; 148 } 149 if(max < canvas[i][j]) 150 { 151 max = canvas[i][j]; 152 oldtail_x = i; 153 oldtail_y = j; 154 } 155 } 156 //设置一个变量来存储新的蛇头 157 int newhead_x, newhead_y; 158 //开始设置新的舌头 159 // 上1 下3 左2 右4 160 if(direction == 1) 161 { 162 newhead_x = oldhead_x - 1; 163 newhead_y = oldhead_y; 164 } 165 if(direction == 2) 166 { 167 newhead_x = oldhead_x; 168 newhead_y = oldhead_y - 1; 169 } 170 if(direction == 3) 171 { 172 newhead_x = oldhead_x + 1; 173 newhead_y = oldhead_y; 174 } 175 if(direction == 4) 176 { 177 newhead_x = oldhead_x; 178 newhead_y = oldhead_y + 1; 179 } 180 //吃到食物的结果 181 if(canvas[newhead_x][newhead_y] == -2) 182 { 183 do 184 { 185 x_food = rand()%(High - 4) + 2; 186 y_food = rand()%(Width - 4) + 2; 187 } 188 while(canvas[x_food][y_food] > 0); 189 canvas[x_food][y_food] = -2; 190 } 191 else 192 canvas[oldtail_x][oldtail_y] = 0; 193 //判断游戏结束 194 if(canvas[newhead_x][newhead_y] > 1 || canvas[newhead_x][newhead_y] == -1) 195 { 196 system("cls"); 197 printf("You lose!"); 198 system("pause"); 199 exit(0); 200 } 201 else 202 canvas[newhead_x][newhead_y] = 1; 203 }
加油, 你是最长的!