我想了很久了,新手,给点意见,贪吃蛇还是没做出来
程序代码:
#include <stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> #include<conio.h> #define N 200 int map[21][11] = { 0 }; struct Snake { int x[N]; int y[N]; int node; }snake; void gotoxy(int x, int y) { COORD c; c.X = x - 1; c.Y = y - 1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); } void draw(int show[21][11]) { int i, j; for (i = 0; i < 21; i++) { for (j = 0; j < 11; j++) { if (i == 0 && j == 0 || i == 0 && j == 10 || i == 20 && j == 0 || i == 20 && j == 10) show[i][j] = 0; else if (j == 0 || j == 10) show[i][j] = 1; else if (i == 0 || i == 20) show[i][j] = 2; else show[i][j] = 0; if (show[i][j] == 0)printf(" "); if (show[i][j] == 1) printf("|"); if (show[i][j] == 2)printf("--"); if (show[i][j] == 3)printf("■"); if (show[i][j] == 4)printf("●"); } printf("\n"); } } void gameover() { int i, j; for (i = 1; i < 19;i++) for (j = 1; j < 9; j++) printf("□"); gotoxy(4,10); printf("game over"); gotoxy(1,20); exit(0); } void main() { int i, a, b; snake.x[0] = 10; snake.y[0] = 6; snake.x[1] = 9; snake.y[1] = 6; snake.node = 2; map[snake.x[0]][snake.y[0]] = 3; map[snake.x[1]][snake.y[1]] = 3; draw(map); while (1) switch (_getch()) { case'A':while (!_kbhit()) { if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]][snake.y[0] - 1] == 4) { srand(time(NULL)); a = rand() % 18 + 1; b = rand() % 8 + 1; snake.node++; map[snake.x[0]][snake.y[0] - 1] = 0; if (snake.y[0] > 1 && map[snake.x[0]][snake.y[0] - 1] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); } else if (snake.y[0] > 1 && map[snake.x[0]][snake.y[0] - 1] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); }break; case'D':while (!_kbhit()) { if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]][snake.y[0] + 1] == 4) { srand(time(NULL)); a = rand() % 18 + 1; b = rand() % 8 + 1; snake.node++; map[snake.x[0]][snake.y[0] + 1] = 0; if (snake.y[0] < 9 && map[snake.x[0]][snake.y[0] + 1] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); } else if (snake.y[0] < 9 && map[snake.x[0]][snake.y[0] + 1] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); }break; case'W':while (!_kbhit()) { if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]-1][snake.y[0]] == 4) { srand(time(NULL)); a = rand() % 18 + 1; b = rand() % 8 + 1; snake.node++; map[snake.x[0]-1][snake.y[0]] = 0; if (snake.x[0] > 1 && map[snake.x[0]-1][snake.y[0]] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); } else if (snake.x[0] > 1 && map[snake.x[0]-1][snake.y[0]] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); }break; case'S':while (!_kbhit()) { if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]+1][snake.y[0]] == 4) { srand(time(NULL)); a = rand() % 18 + 1; b = rand() % 8 + 1; snake.node++; map[snake.x[0]+1][snake.y[0]] = 0; if (snake.x[0] < 19 && map[snake.x[0]+1][snake.y[0]] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); } else if (snake.x[0] < 19 && map[snake.x[0]+1][snake.y[0]] == 0) { for (i = snake.node - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } for (i = 0; i < snake.node; i++) map[snake.x[0]][snake.y[0]] = 3; } else gameover(); }break; default:printf("error\n"); } }