|
应用界面
菜单栏

游戏说明

关于界面

游戏界面



截了几张图(名字有点中二哈哈)
游戏结束时的排名界面

最后附上代码
头文件 snake.h
#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#define MAP_HEIGHT 20
#define MAP_WIDTH 40
#define UP &#39;w&#39;
#define DOWN &#39;s&#39;
#define LEFT &#39;a&#39;
#define RIGHT &#39;d&#39;
typedef struct {
int x;
int y;
}Food, StrongFood, Snakenode, Barrier;
typedef struct {
Snakenode snakeNode[1000];
int length;
int speed;
}Snake;
void GotoXY(int, int);
void Hide();
int Menu();
void Help();
void About();
void InitMap();
void PrintFood();
void PrintStrongFood();
void PrintBarrier();
int MoveSnake();
int IsCorrect();
void SpeedControl();源文件 snake.c
#include&#34;snake.h&#34;
Snake snake;
Food food;
StrongFood strongfood;
Barrier barrier;
char now_Dir = RIGHT;
char direction = RIGHT;
int a[10] = { 0 };
int Menu() {
GotoXY(45, 8);
printf(&#34;贪吃蛇 Greedy Snake&#34;);
GotoXY(45, 10);
printf(&#34;(仅支持键盘操作)&#34;);
GotoXY(43, 14);
printf(&#34;1.开始游戏&#34;);
GotoXY(43, 16);
printf(&#34;2.帮助&#34;);
GotoXY(43, 18);
printf(&#34;3.关于&#34;);
GotoXY(43, 22);
printf(&#34;其他任意位置退出游戏&#34;);
Hide();
char ch;
int result = 0;
ch = _getch();
switch (ch) {
case&#39;1&#39;: result = 1; break;
case&#39;2&#39;: result = 2; break;
case&#39;3&#39;: result = 3; break;
}
system(&#34;cls&#34;);
return result;
}
void GotoXY(int x, int y) {
HANDLE hout;
COORD cor;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
cor.X = x;
cor.Y = y;
SetConsoleCursorPosition(hout, cor);
}
void Hide() {
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cor_info = { 1,0 };
SetConsoleCursorInfo(hout, &cor_info);
}
void About() {
GotoXY(44, 12);
printf(&#34;杭州电子科技大学&#34;);
GotoXY(46, 14);
printf(&#34;Made By Ding&#34;);
GotoXY(43, 16);
printf(&#34;按任意键返回上级菜单&#34;);
Hide();
char ch = _getch();
system(&#34;cls&#34;);
}
void Help() {
GotoXY(40, 6);
printf(&#34;w 上&#34;);
GotoXY(40, 8);
printf(&#34;s 下&#34;);
GotoXY(40, 10);
printf(&#34;a 左&#34;);
GotoXY(40, 12);
printf(&#34;d 右&#34;);
GotoXY(40, 16);
printf(&#34;当蛇撞到墙或障碍物时游戏结束&#34;);
GotoXY(40, 18);
printf(&#34;吃到$时蛇身增加一级&#34;);
GotoXY(40, 20);
printf(&#34;吃到€时蛇身增加两级&#34;);
GotoXY(40, 22);
printf(&#34;吃到食物时地图上出现障碍物\&#34;X\&#34;&#34;);
GotoXY(40, 25);
printf(&#34;按任意键返回上级菜单&#34;);
Hide();
char ch = _getch();
system(&#34;cls&#34;);
}
void InitMap() {
Hide();
GotoXY(14, 21);
printf(&#34;Greedy Snake&#34;);
snake.snakeNode[0].x = MAP_WIDTH / 2 - 1;
snake.snakeNode[0].y = MAP_HEIGHT / 2 - 1;
GotoXY(snake.snakeNode[0].x, snake.snakeNode[0].y);
printf(&#34;@&#34;);
snake.length = 3;
snake.speed = 250;
now_Dir = RIGHT;
for (int i = 1; i < snake.length; i++) {
snake.snakeNode.y = snake.snakeNode[i - 1].y;
snake.snakeNode.x = snake.snakeNode[i - 1].x - 1;
GotoXY(snake.snakeNode.x, snake.snakeNode.y);
printf(&#34;O&#34;);
}
for (int i = 0; i <= MAP_WIDTH; i++) {
GotoXY(i, 0);
printf(&#34;_&#34;);
GotoXY(i, MAP_HEIGHT - 1);
printf(&#34;_&#34;);
}
for (int i = 1; i < MAP_HEIGHT; i++) {
GotoXY(0, i);
printf(&#34;|&#34;);
GotoXY(MAP_WIDTH, i);
printf(&#34;|&#34;);
}
PrintFood();
PrintStrongFood();
GotoXY(50, 4);
printf(&#34;当前得分:0 &#34;);
GotoXY(50, 8);
printf(&#34;当前速度:I &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:蛇胚 &#34;);
}
void PrintFood() {
int flag = 1;
while (flag) {
flag = 0;
food.x = rand() % (MAP_WIDTH - 2) + 1;
food.y = rand() % (MAP_HEIGHT - 2) + 1;
for (int k = 0; k <= snake.length - 1; k++) {
if (snake.snakeNode[k].x == food.x && snake.snakeNode[k].y == food.y) {
flag = 1;
break;
}
}
}
GotoXY(food.x, food.y);
printf(&#34;$&#34;);
}
void PrintStrongFood() {
int flag = 1;
while (flag) {
flag = 0;
strongfood.x = rand() % (MAP_WIDTH - 2) + 1;
strongfood.y = rand() % (MAP_HEIGHT - 2) + 1;
for (int k = 0; k <= snake.length - 1; k++) {
if (snake.snakeNode[k].x == strongfood.x && snake.snakeNode[k].y == strongfood.y) {
flag = 1;
break;
}
}
if (strongfood.x == food.x && strongfood.y == food.y) {
flag = 1;
}
}
GotoXY(strongfood.x, strongfood.y);
printf(&#34;€&#34;);
}
void PrintBarrier() {
int flag = 1;
GotoXY(barrier.x, barrier.y);
printf(&#34; &#34;);
while (flag) {
flag = 0;
barrier.x = rand() % (MAP_WIDTH - 2) + 1;
barrier.y = rand() % (MAP_HEIGHT - 2) + 1;
for (int k = 0; k <= snake.length - 1; k++) {
if (snake.snakeNode[k].x == barrier.x && snake.snakeNode[k].y == barrier.y) {
flag = 1;
break;
}
}
if (barrier.x == food.x && barrier.y == food.y || barrier.x == strongfood.x && barrier.y == strongfood.y) {
flag = 1;
}
}
GotoXY(barrier.x, barrier.y);
printf(&#34;X&#34;);
}
int MoveSnake() {
Snakenode temp;
int flag = 0;
temp = snake.snakeNode[snake.length - 1];
for (int i = snake.length - 1; i >= 1; i--)
snake.snakeNode = snake.snakeNode[i - 1];
GotoXY(snake.snakeNode[1].x, snake.snakeNode[1].y);
printf(&#34;O&#34;);
if (_kbhit()) {
direction = _getch();
switch (direction) {
case UP:
if (now_Dir != DOWN)
now_Dir = direction;
break;
case DOWN:
if (now_Dir != UP)
now_Dir = direction;
break;
case LEFT:
if (now_Dir != RIGHT)
now_Dir = direction;
break;
case RIGHT:
if (now_Dir != LEFT)
now_Dir = direction;
break;
}
}
switch (now_Dir) {
case UP:snake.snakeNode[0].y--; break;
case DOWN:snake.snakeNode[0].y++; break;
case LEFT:snake.snakeNode[0].x--; break;
case RIGHT:snake.snakeNode[0].x++; break;
}
GotoXY(snake.snakeNode[0].x, snake.snakeNode[0].y);
printf(&#34;@&#34;);
if (snake.snakeNode[0].x == food.x && snake.snakeNode[0].y == food.y) {
snake.length++;
flag = 1;
snake.snakeNode[snake.length - 1] = temp;
}
if (snake.snakeNode[0].x == strongfood.x && snake.snakeNode[0].y == strongfood.y) {
snake.length += 2;
flag = 2;
snake.snakeNode[snake.length] = temp;
}
if (!flag) {
GotoXY(temp.x, temp.y);
printf(&#34; &#34;);
}
else if(flag == 1) {
PrintFood();
PrintBarrier();
GotoXY(50, 4);
printf(&#34;当前得分:%d &#34;, snake.length - 3);
}
else if (flag == 2) {
PrintStrongFood();
PrintBarrier();
GotoXY(temp.x, temp.y);
printf(&#34; &#34;);
GotoXY(50, 4);
printf(&#34;当前得分:%d &#34;, snake.length - 3);
}
if (!IsCorrect()) {
system(&#34;cls&#34;);
GotoXY(30, 10);
printf(&#34;最终得分:%d &#34;, snake.length - 3);
int i = 0;
while (i < 10) {
if ((snake.length - 3) > a) {
int j = 9;
while (j > i) {
a[j] = a[j - 1];
j--;
}
a = snake.length - 3;
break;
}
i++;
}
GotoXY(30, 12);
printf(&#34;不错的!&#34;);
GotoXY(30, 14);
printf(&#34;按任意键返回主菜单&#34;);
GotoXY(60, 2);
printf(&#34;得分前十排行榜&#34;);
for (int i = 0; i < 10; i++) {
GotoXY(63, (i + 2)*2);
printf(&#34;%02d: %d 分&#34;, i + 1, a);
}
char c = _getch();
system(&#34;cls&#34;);
return 0;
}
SpeedControl();
Sleep(snake.speed);
return 1;
}
int IsCorrect() {
if (snake.snakeNode[0].x == 0 || snake.snakeNode[0].y == 0 || snake.snakeNode[0].x == MAP_WIDTH - 1 || snake.snakeNode[0].y == MAP_HEIGHT - 1)
return 0;
for (int i = 1; i < snake.length; i++) {
if (snake.snakeNode[0].x == snake.snakeNode.x && snake.snakeNode[0].y == snake.snakeNode.y) {
for (int j = i + 1; j < snake.length; j++) {
GotoXY(snake.snakeNode[j].x, snake.snakeNode[j].y);
printf(&#34; &#34;);
}
snake.length = i + 1;
}
}
if (snake.snakeNode[0].x == barrier.x && snake.snakeNode[0].y == barrier.y) {
return 0;
}
return 1;
}
void SpeedControl() {
switch (snake.length) {
case 4:
case 5:
case 6:
case 7:
snake.speed = 200;
GotoXY(50, 8);
printf(&#34;当前速度:II &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:小白蛇 &#34;);
break;
case 8:
case 9:
case 10:
case 11:
snake.speed = 180;
GotoXY(50, 8);
printf(&#34;当前速度:III &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:珊瑚蛇 &#34;);
break;
case 12:
case 13:
case 14:
case 15:
snake.speed = 160;
GotoXY(50, 8);
printf(&#34;当前速度:IV &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:眼镜王蛇 &#34;);
break;
case 16:
case 17:
case 18:
case 19:
snake.speed = 140;
GotoXY(50, 8);
printf(&#34;当前速度:V &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:亚马逊森蟒 &#34;);
break;
case 20:
case 21:
case 22:
case 23:
snake.speed = 120;
GotoXY(50, 8);
printf(&#34;当前速度:VI &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:日本八岐大蛇 &#34;);
break;
case 24:
case 25:
case 26:
case 27:
snake.speed = 100;
GotoXY(50, 8);
printf(&#34;当前速度:VII &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:冥蛇-阿波菲斯 &#34;);
break;
case 29:
case 30:
case 31:
case 32:
snake.speed = 80;
GotoXY(50, 8);
printf(&#34;当前速度:VIII&#34;);
GotoXY(50, 12);
printf(&#34;当前形态:尘世巨蟒-约尔曼冈德 &#34;);
break;
case 33:
case 34:
case 35:
case 36:
snake.speed = 60;
GotoXY(50, 8);
printf(&#34;当前速度:IX &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:羽蛇神-魁札科亚特尔 &#34;);
break;
case 37:
case 38:
case 39:
case 40:
snake.speed = 40;
GotoXY(50, 8);
printf(&#34;当前速度:X &#34;);
GotoXY(50, 12);
printf(&#34;当前形态:永恒之蛇-乌洛波洛斯 &#34;);
break;
default:break;
}
}源文件 snake_main.c
#include&#34;snake.h&#34;
int main() {
srand((unsigned int)time(0));
int end = 1, result;
while (end) {
result = Menu();
switch (result) {
case 1:
InitMap();
while (MoveSnake());
break;
case 2:
Help();
break;
case 3:
About();
break;
default:
end = 0;
break;
}
}
return 0;
} |
|