第一版:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>#define WIDTH 20
#define HEIGHT 20int gameOver;
int score;
int x, y; // 蛇头的坐标
int fruitX, fruitY; // 食物的坐标
int tailX[100], tailY[100]; // 蛇身的坐标
int tailLength;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;void Setup()
{gameOver = 0;score = 0;x = WIDTH / 2;y = HEIGHT / 2;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;dir = STOP;
}void Draw()
{system("cls"); // 清屏for (int i = 0; i < WIDTH + 2; i++)printf("#");printf("\n");for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (j == 0)printf("#");if (i == y && j == x)printf("O");else if (i == fruitY && j == fruitX)printf("F");else{int printTail = 0;for (int k = 0; k < tailLength; k++){if (tailX[k] == j && tailY[k] == i){printf("o");printTail = 1;}}if (!printTail)printf(" ");}if (j == WIDTH - 1)printf("#");}printf("\n");}for (int i = 0; i < WIDTH + 2; i++)printf("#");printf("\n");printf("Score: %d\n", score);
}void Input()
{if (_kbhit()){switch (_getch()){case 'a':dir = LEFT;break;case 'd':dir = RIGHT;break;case 'w':dir = UP;break;case 's':dir = DOWN;break;case 'x':gameOver = 1;break;}}
}void Logic()
{int prevX = tailX[0];int prevY = tailY[0];int prev2X, prev2Y;tailX[0] = x;tailY[0] = y;for (int i = 1; i < tailLength; i++){prev2X = tailX[i];prev2Y = tailY[i];tailX[i] = prevX;tailY[i] = prevY;prevX = prev2X;prevY = prev2Y;}switch (dir){case LEFT:x--;break;case RIGHT:x++;break;case UP:y--;break;case DOWN:y++;break;}if (x >= WIDTH)x = 0;else if (x < 0)x = WIDTH - 1;if (y >= HEIGHT)y = 0;else if (y < 0)y = HEIGHT - 1;for (int i = 0; i < tailLength; i++){if (tailX[i] == x && tailY[i] == y)gameOver = 1;}if (x == fruitX && y == fruitY){score += 10;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;tailLength++;}
}int main()
{Setup();while (!gameOver){Draw();Input();Logic();Sleep(10); // 控制游戏速度}return 0;
}
第二版:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>#define WIDTH 30
#define HEIGHT 20int gameOver;
int score;
int x, y; // 蛇头的坐标
int fruitX, fruitY; // 食物的坐标
int tailX[100], tailY[100]; // 蛇身的坐标
int tailLength;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;int aiX, aiY; // AI蛇头的坐标
int aiTailX[100], aiTailY[100]; // AI蛇身的坐标
int aiTailLength;
enum eDirection aiDir;void Setup()
{gameOver = 0;score = 0;x = WIDTH / 2;y = HEIGHT / 2;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;dir = STOP;aiX = WIDTH / 2 - 5;aiY = HEIGHT / 2;aiDir = STOP;aiTailLength = 0;
}void Draw()
{system("cls"); // 清屏for (int i = 0; i < WIDTH + 2; i++)printf("#");printf("\n");for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (j == 0)printf("#");if (i == y && j == x)printf("O");else if (i == fruitY && j == fruitX)printf("F");else if (i == aiY && j == aiX)printf("A");else{int printTail = 0;for (int k = 0; k < tailLength; k++){if (tailX[k] == j && tailY[k] == i){printf("o");printTail = 1;}}for (int k = 0; k < aiTailLength; k++){if (aiTailX[k] == j && aiTailY[k] == i){printf("a");printTail = 1;}}if (!printTail)printf(" ");}if (j == WIDTH - 1)printf("#");}printf("\n");}for (int i = 0; i < WIDTH + 2; i++)printf("#");printf("\n");printf("Score: %d\n", score);
}void Input()
{if (_kbhit()){switch (_getch()){case 'a':dir = LEFT;break;case 'd':dir = RIGHT;break;case 'w':dir = UP;break;case 's':dir = DOWN;break;case 'x':gameOver = 1;break;}}
}void AI()
{if (aiX < fruitX)aiDir = RIGHT;else if (aiX > fruitX)aiDir = LEFT;else if (aiY < fruitY)aiDir = DOWN;else if (aiY > fruitY)aiDir = UP;
}void Logic()
{int prevX = tailX[0];int prevY = tailY[0];int prev2X, prev2Y;tailX[0] = x;tailY[0] = y;for (int i = 1; i < tailLength; i++){prev2X = tailX[i];prev2Y = tailY[i];tailX[i] = prevX;tailY[i] = prevY;prevX = prev2X;prevY = prev2Y;}int aiPrevX = aiTailX[0];int aiPrevY = aiTailY[0];int aiPrev2X, aiPrev2Y;aiTailX[0] = aiX;aiTailY[0] = aiY;for (int i = 1; i < aiTailLength; i++){aiPrev2X = aiTailX[i];aiPrev2Y = aiTailY[i];aiTailX[i] = aiPrevX;aiTailY[i] = aiPrevY;aiPrevX = aiPrev2X;aiPrevY = aiPrev2Y;}switch (dir){case LEFT:x--;break;case RIGHT:x++;break;case UP:y--;break;case DOWN:y++;break;}switch (aiDir){case LEFT:aiX--;break;case RIGHT:aiX++;break;case UP:aiY--;break;case DOWN:aiY++;break;}if (x >= WIDTH || x < 0 || y >= HEIGHT || y < 0)gameOver = 1;for (int i = 0; i < tailLength; i++){if (tailX[i] == x && tailY[i] == y)gameOver = 1;}if (aiX >= WIDTH || aiX < 0 || aiY >= HEIGHT || aiY < 0)gameOver = 1;for (int i = 0; i < aiTailLength; i++){if (aiTailX[i] == aiX && aiTailY[i] == aiY)gameOver = 1;}if (x == fruitX && y == fruitY){score += 10;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;tailLength++;}if (aiX == fruitX && aiY == fruitY){score += 10;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;aiTailLength++;}
}int main()
{srand(time(NULL)); // 随机数种子Setup();while (!gameOver){Draw();Input();AI();Logic();Sleep(100); // 控制游戏速度}printf("GAME OVER\n");return 0;
}
制作不易,喜欢的话,给个赞吧。